Auto Convert Files in a Folder Using C# / PDF Printer
We often contacted by developers who are building a program that will look for document files in a folder and convert these files to PDF. To illustrate how this can be done we have made a small C# program that will do just that.
The example source code is not bullet proof. You will need to add your own configuration and error handling. Nevertheless it serves the purpose of showing how this problem can be solved.
When you run the program it will look for files in the input folder ..\bin\Debug\input
and convert them to PDF documents in the output folder ..\bin\Debug\input
. In case something goes wrong the input document is copied to the errors folder. If the conversion is a success, then the input document is copied to the done folder.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using pdf7.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"); } } } }
Example source files are included in the zip file that can be downloaded here.
Downloads
Attachment | Size |
---|---|
Example file | 321.4 KB |