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 VB.NET 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\output. 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.

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.IO
    Imports pdf7.PdfWriter
    Imports System.Reflection
    
    Module Converter
    
        Sub Main()
            Dim applicationFolder As String = New Uri(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath
            Dim inputFolder As String = Path.Combine(applicationFolder, "input")
            Dim outputFolder As String = Path.Combine(applicationFolder, "output")
            Dim doneFolder As String = Path.Combine(applicationFolder, "done")
            Dim errorFolder As String = Path.Combine(applicationFolder, "errors")
            If Not Directory.Exists(inputFolder) Then Directory.CreateDirectory(inputFolder)
            If Not Directory.Exists(outputFolder) Then Directory.CreateDirectory(outputFolder)
            If Not Directory.Exists(doneFolder) Then Directory.CreateDirectory(doneFolder)
            If Not Directory.Exists(errorFolder) Then Directory.CreateDirectory(errorFolder)
            Dim printerName As String = PdfUtil.DefaultPrinterName
            Dim inputFileNames As String() = Directory.GetFiles(inputFolder)
    
            For Each inputFileName As String In inputFileNames
                Dim isError As Boolean = False
                Dim errorMessage As String = Nothing
                Console.Write(String.Format("Printing {0}... ", Path.GetFileName(inputFileName)))
                Dim outputFileName As String = Path.Combine(outputFolder, Path.GetFileName(inputFileName) & ".pdf")
                Dim settings As PdfSettings = New PdfSettings()
                settings.PrinterName = printerName
                settings.SetValue("Output", outputFileName)
                settings.SetValue("ShowSettings", "never")
                settings.SetValue("ShowSaveAS", "never")
                settings.SetValue("ShowProgress", "no")
                settings.SetValue("ShowProgressFinished", "no")
                settings.SetValue("ShowPDF", "no")
                settings.SetValue("ConfirmOverwrite", "no")
                Dim statusFileName As String = Path.Combine(Path.GetTempPath(), "converter_status.ini")
                If File.Exists(statusFileName) Then File.Delete(statusFileName)
                settings.SetValue("StatusFile", statusFileName)
                settings.WriteSettings(PdfSettingsFileType.RunOnce)
    
                Try
                    PdfUtil.PrintFile(inputFileName, printerName)
                    PdfUtil.WaitForFile(statusFileName, 60000)
                    isError = Not File.Exists(outputFileName)
                Catch ex As Exception
                    isError = True
                    errorMessage = ex.Message
                End Try
    
                If isError Then
                    File.Move(inputFileName, Path.Combine(errorFolder, Path.GetFileName(inputFileName)))
                Else
                    File.Move(inputFileName, Path.Combine(doneFolder, Path.GetFileName(inputFileName)))
                End If
    
                If isError Then
    
                    If String.IsNullOrEmpty(errorMessage) Then
                        Console.WriteLine("Error")
                    Else
                        Console.WriteLine(errorMessage)
                    End If
                Else
                    Console.WriteLine("Done")
                End If
            Next
        End Sub
    
    End Module
    

Example source files are included in the zip file that can be downloaded here.

Downloads

Attachment Size
Example file 273.7 KB

LiveZilla Live Chat Software
Top