Mit den folgenden Funktionen kann man mit Visual Basic .NET unter Verwendung der PDFCreator Library (Verweis!) PDF-Dateien drucken.
Die erste Funktion schickt dabei einfach alle gefundenen Dateien eines beliebigen Verzeichnisses an die Funktion "PrintPDFFile", welche letztendlich die PDF Datei erstellt.
Public Sub PrintDirectory(ByVal aSourceDirectory As String, ByVal aFileFilter As String) Dim lstFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = _ My.Computer.FileSystem.GetFiles(aSourceDirectory, FileIO.SearchOption.SearchAllSubDirectories, aFileFilter) Dim fi As System.IO.FileInfo Dim fnnew As String Dim dirnew As String Dim pdfjob As New PDFCreator.clsPDFCreator If pdfjob.cStart("/NoProcessingAtStartup") = False Then MsgBox("Can't initialize PDFCreator.", vbCritical & vbOKOnly) Exit Sub End If For Each fn As String In lstFiles ' Create new filename fi = My.Computer.FileSystem.GetFileInfo(fn) fnnew = fi.Name fnnew = Replace(fnnew, fi.Extension, "") ' Remove old extension. Don't add new extension! dirnew = fi.Directory.FullName & "\" Me.PDFPrintFile(pdfjob, fi.FullName, fnnew, dirnew) Next pdfjob.cClose() pdfjob = Nothing End Sub
Public Sub PDFPrintFile(ByRef aPDFJob As PDFCreator.clsPDFCreator, ByVal aFilename As String, ByVal aOutputFilename As String, ByVal aOutputPath As String) ' Exit it output filename already exists If System.IO.File.Exists(aOutputPath & aOutputFilename) Then Exit Sub End If ' Create Outputdirectory if not existant If Not System.IO.Directory.Exists(aOutputPath) Then System.IO.Directory.CreateDirectory(aOutputPath) End If With aPDFJob .cOption("UseAutosave") = 1 .cOption("UseAutosaveDirectory") = 1 .cOption("AutosaveDirectory") = aOutputPath .cOption("AutosaveFilename") = aOutputFilename .cOption("AutosaveFormat") = 0 ' 0 = PDF .cClearCache() End With 'Print the document to PDF aPDFJob.cPrintFile(aFilename) 'Wait until the print job has entered the print queue Do Until aPDFJob.cCountOfPrintjobs = 1 ' My.Application.DoEvents.DoEvents() Loop aPDFJob.cPrinterStop = False 'Wait until PDF creator is finished then release the objects Do Until aPDFJob.cCountOfPrintjobs = 0 ' My.Application.DoEvents() Loop End Sub
Aktualisierung vom 03.08.2009:
Wie ich heute leider mal wieder feststellen musste, funktioniert obiger Code bei einer großen Menge an umzuwandelnden Dateien nicht sonderlich.
Das unten aufgeführte "Modul" hat bei mir soeben 1400 Word-Dokumente erfolgreich in PDF umgenwandelt.
Public Module modPrintPDF Public Sub Main() Dim strDir As String = "C:\Word\Serienbriefe\Einzelbriefe" PrintDirectoryPDF(strDir, "*.doc", strDir) End Sub Public Function PrintDirectoryPDF(ByVal aInputDirectory As String, ByVal aFilter As String, ByVal aOutputDirectory As String) Dim lstFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = _ My.Computer.FileSystem.GetFiles(aInputDirectory, FileIO.SearchOption.SearchAllSubDirectories, aFilter) Dim pdfc As New PDFCreator.clsPDFCreator pdfc.cStart(, True) For Each fn As String In lstFiles ' Generate New Filename Dim fi As New IO.FileInfo(fn) Dim strNewFilename As String = fi.Name strNewFilename = Replace(strNewFilename, fi.Extension, ".pdf") ' Print File PrintPDF(pdfc, fn, aOutputDirectory, strNewFilename) Next Return True End Function Public Function PrintPDF(ByRef aPDFCreator As PDFCreator.clsPDFCreator, ByVal aPDFFile As String, ByVal aOutputPath As String, ByVal aOutputFilename As String) As Integer With aPDFCreator .cOption("UseAutosave") = 1 .cOption("UseAutosaveDirectory") = 1 .cOption("AutosaveFormat") = 0 ' 0 = PDF .cOption("AutosaveDirectory") = aOutputPath .cOption("AutosaveFilename") = aOutputFilename .cClearCache() .cPrintFile(aPDFFile) End With End Function End Module