VB .NET: Dateien mit PDFCreator drucken

04. Aug 2008 21:18 (bearbeiten)


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.

  1.  
  2. Public Sub PrintDirectory(ByVal aSourceDirectory As String, ByVal aFileFilter As String)
  3.  
  4. Dim lstFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = _
  5. My.Computer.FileSystem.GetFiles(aSourceDirectory, FileIO.SearchOption.SearchAllSubDirectories, aFileFilter)
  6.  
  7. Dim fi As System.IO.FileInfo
  8. Dim fnnew As String
  9. Dim dirnew As String
  10.  
  11. Dim pdfjob As New PDFCreator.clsPDFCreator
  12.  
  13. If pdfjob.cStart("/NoProcessingAtStartup") = False Then
  14. MsgBox("Can't initialize PDFCreator.", vbCritical & vbOKOnly)
  15. Exit Sub
  16. End If
  17.  
  18. For Each fn As String In lstFiles
  19. ' Create new filename
  20. fi = My.Computer.FileSystem.GetFileInfo(fn)
  21. fnnew = fi.Name
  22. fnnew = Replace(fnnew, fi.Extension, "") ' Remove old extension. Don't add new extension!
  23. dirnew = fi.Directory.FullName & "\"
  24.  
  25. Me.PDFPrintFile(pdfjob, fi.FullName, fnnew, dirnew)
  26. Next
  27.  
  28. pdfjob.cClose()
  29. pdfjob = Nothing
  30. End Sub
  31.  
  1.  
  2. Public Sub PDFPrintFile(ByRef aPDFJob As PDFCreator.clsPDFCreator, ByVal aFilename As String, ByVal aOutputFilename As String, ByVal aOutputPath As String)
  3.  
  4. ' Exit it output filename already exists
  5. If System.IO.File.Exists(aOutputPath & aOutputFilename) Then
  6. Exit Sub
  7. End If
  8.  
  9. ' Create Outputdirectory if not existant
  10. If Not System.IO.Directory.Exists(aOutputPath) Then
  11. System.IO.Directory.CreateDirectory(aOutputPath)
  12. End If
  13.  
  14. With aPDFJob
  15. .cOption("UseAutosave") = 1
  16. .cOption("UseAutosaveDirectory") = 1
  17. .cOption("AutosaveDirectory") = aOutputPath
  18. .cOption("AutosaveFilename") = aOutputFilename
  19. .cOption("AutosaveFormat") = 0 ' 0 = PDF
  20. .cClearCache()
  21. End With
  22.  
  23. 'Print the document to PDF
  24. aPDFJob.cPrintFile(aFilename)
  25.  
  26. 'Wait until the print job has entered the print queue
  27. Do Until aPDFJob.cCountOfPrintjobs = 1
  28. ' My.Application.DoEvents.DoEvents()
  29. Loop
  30.  
  31. aPDFJob.cPrinterStop = False
  32.  
  33. 'Wait until PDF creator is finished then release the objects
  34. Do Until aPDFJob.cCountOfPrintjobs = 0
  35. ' My.Application.DoEvents()
  36. Loop
  37.  
  38. End Sub
  39.  

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.

  1. Public Module modPrintPDF
  2.  
  3. Public Sub Main()
  4.  
  5. Dim strDir As String = "C:\Word\Serienbriefe\Einzelbriefe"
  6. PrintDirectoryPDF(strDir, "*.doc", strDir)
  7.  
  8. End Sub
  9.  
  10. Public Function PrintDirectoryPDF(ByVal aInputDirectory As String, ByVal aFilter As String, ByVal aOutputDirectory As String)
  11.  
  12. Dim lstFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = _
  13. My.Computer.FileSystem.GetFiles(aInputDirectory, FileIO.SearchOption.SearchAllSubDirectories, aFilter)
  14.  
  15. Dim pdfc As New PDFCreator.clsPDFCreator
  16. pdfc.cStart(, True)
  17.  
  18. For Each fn As String In lstFiles
  19.  
  20. ' Generate New Filename
  21. Dim fi As New IO.FileInfo(fn)
  22. Dim strNewFilename As String = fi.Name
  23. strNewFilename = Replace(strNewFilename, fi.Extension, ".pdf")
  24.  
  25. ' Print File
  26. PrintPDF(pdfc, fn, aOutputDirectory, strNewFilename)
  27. Next
  28.  
  29. Return True
  30.  
  31. End Function
  32.  
  33. Public Function PrintPDF(ByRef aPDFCreator As PDFCreator.clsPDFCreator, ByVal aPDFFile As String, ByVal aOutputPath As String, ByVal aOutputFilename As String) As Integer
  34.  
  35. With aPDFCreator
  36. .cOption("UseAutosave") = 1
  37. .cOption("UseAutosaveDirectory") = 1
  38. .cOption("AutosaveFormat") = 0 ' 0 = PDF
  39. .cOption("AutosaveDirectory") = aOutputPath
  40. .cOption("AutosaveFilename") = aOutputFilename
  41. .cClearCache()
  42. .cPrintFile(aPDFFile)
  43. End With
  44.  
  45. End Function
  46.  
  47. End Module
  48.