Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / Visual Basic

Automated PDF Conversion using the PDF995 and FreePDF_XP Freeware Printers

Rate me:
Please Sign up or sign in to vote.
4.67/5 (10 votes)
16 Oct 2008BSD7 min read 128.7K   3.1K   89  
An object oriented class automating the creation of PDF files from any file using freeware tools.
Imports Microsoft.Win32
Imports System.IO

Public Class CPrinterPDF995 : Inherits CPrinter

    'the INI that we want to change in oder to "silently" use the PDF printer
    Private Const strOriginalIniFile As String = "pdf995.ini"
    'we want to recover the original INI file after every print
    Private Const strOriginalIniSave As String = "pdf995.ini_ORIGINAL"
    'used to locate PDF995 installation directory
    Private Const strRegPath As String = "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pdf995"

    Private Function RetrieveIniPath() As String

        Dim strPdf995IniPath As String = ""

        Dim regKey As RegistryKey = Nothing
        Dim regKeyPDF995 As RegistryKey = Nothing

        regKey = Registry.LocalMachine.OpenSubKey(strRegPath)

        'retrieve path using the "DisplayIcon" value
        strPdf995IniPath = regKey.GetValue("DisplayIcon", "")

        'icon is in pdf995path\setup.exe and INI-File is in pdf995path\res directory, so remove "setup.exe"
        strPdf995IniPath = Strings.Left(strPdf995IniPath, strPdf995IniPath.Length - 9) + "res\"

        regKey.Close()

        Return strPdf995IniPath

    End Function

    Property fileToPrint() As String
        Get
            Return MyBase.strFileToPrint
        End Get
        Set(ByVal value As String)
            MyBase.strFileToPrint = value
        End Set
    End Property

    Protected Overrides Sub pushSettings()

        Dim strIniPath As String = RetrieveIniPath()

        'save original INI file first
        FileSystem.Rename(strIniPath + strOriginalIniFile, strIniPath + strOriginalIniSave)

        Me.createCustomIni()

    End Sub

    Protected Overrides Sub popSettings()

        Dim strIniPath As String = RetrieveIniPath()

        'restore original INI file
        File.Delete(strIniPath + strOriginalIniFile)
        FileSystem.Rename(strIniPath + strOriginalIniSave, strIniPath + strOriginalIniFile)

    End Sub

    Private Sub createCustomIni()

        Dim strFileToPrint As String = MyBase.strFileToPrint
        Dim strIniPath As String = RetrieveIniPath()
        Dim fi As System.IO.FileInfo = New System.IO.FileInfo(strFileToPrint)
        Dim strPDFOutputDirectory As String = fi.DirectoryName + "\"
        Dim oWriter As StreamWriter = Nothing

        fi = Nothing

        'write INI file, create PDF from "strFileToPrint" and save it to "directory"
        oWriter = New StreamWriter(strIniPath + "pdf995.ini", False, System.Text.Encoding.Unicode)

        oWriter.WriteLine("[Parameters]")
        oWriter.WriteLine("Install = 0")
        oWriter.WriteLine("Quiet = 1")
        oWriter.WriteLine("AutoLaunch = 0")
        oWriter.WriteLine("Document Name = " + strFileToPrint)
        oWriter.WriteLine("User File = " + strFileToPrint + ".pdf")
        oWriter.WriteLine("Output File = " + strFileToPrint + ".pdf")
        oWriter.WriteLine("Initial Dir = " + strPDFOutputDirectory)
        oWriter.WriteLine("Use GPL Ghostcript = 1")

        oWriter.Close()

        oWriter = Nothing

        System.Threading.Thread.Sleep(1000) ' give Pdf995 some time

    End Sub

    Public Overrides Sub printFile()

        Me.pushSettings()
        MyBase.PrintFile()
        Me.popSettings()

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions