using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using bioPDF.PdfWriter;
using System.Reflection;
namespace Converter
{
class Program
{
static void Main(string[] args)
{
string applicationFolder =
new Uri(Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program)).CodeBase)).LocalPath;
string inputFolder = Path.Combine(applicationFolder, "input");
string outputFolder = Path.Combine(applicationFolder, "output");
string doneFolder = Path.Combine(applicationFolder, "done");
string errorFolder = Path.Combine(applicationFolder, "errors");
// Check that folders exist
if (!Directory.Exists(inputFolder)) Directory.CreateDirectory(inputFolder);
if (!Directory.Exists(outputFolder)) Directory.CreateDirectory(outputFolder);
if (!Directory.Exists(doneFolder)) Directory.CreateDirectory(doneFolder);
if (!Directory.Exists(errorFolder)) Directory.CreateDirectory(errorFolder);
// Get the printer name
string printerName = PdfUtil.DefaultPrinterName;
// Get file names from input folder
string[] inputFileNames = Directory.GetFiles(inputFolder);
foreach (string inputFileName in inputFileNames)
{
bool isError = false;
string errorMessage = null;
Console.Write(string.Format("Printing {0}... ", Path.GetFileName(inputFileName)));
string outputFileName =
Path.Combine(outputFolder, Path.GetFileName(inputFileName) + ".pdf");
PdfSettings settings = new PdfSettings();
settings.PrinterName = printerName;
// Set the output file name
settings.SetValue("Output", outputFileName);
// Disable all dialogs
settings.SetValue("ShowSettings", "never");
settings.SetValue("ShowSaveAS", "never");
settings.SetValue("ShowProgress", "no");
settings.SetValue("ShowProgressFinished", "no");
settings.SetValue("ShowPDF", "no");
settings.SetValue("ConfirmOverwrite", "no");
// Get the name of a status file and delete it if it already exist
string statusFileName = Path.Combine(Path.GetTempPath(), "converter_status.ini");
if (File.Exists(statusFileName))
File.Delete(statusFileName);
// Make the printer write a status file
settings.SetValue("StatusFile", statusFileName);
// Write the settings to the printer
settings.WriteSettings(PdfSettingsFileType.RunOnce);
try
{
// Print the file using the associated program for the specific file type
PdfUtil.PrintFile(inputFileName, printerName);
// Wait for the status file to appear. This means that the print has finished
PdfUtil.WaitForFile(statusFileName, 60000);
// Check if output file exists
isError = !File.Exists(outputFileName);
}
catch (Exception ex)
{
isError = true;
errorMessage = ex.Message;
}
// Move the input file
if (isError)
File.Move(inputFileName, Path.Combine(errorFolder, Path.GetFileName(inputFileName)));
else
File.Move(inputFileName, Path.Combine(doneFolder, Path.GetFileName(inputFileName)));
// Write a status
if (isError)
{
if (string.IsNullOrEmpty(errorMessage))
Console.WriteLine("Error");
else
Console.WriteLine(errorMessage);
}
else
Console.WriteLine("Done");
}
}
}
}