VB .NET: Dateien mit PDFCreator drucken

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

VB .NET ReportViewer

Mit der folgende Methoden lässt sich ein Microsoft ReportViewer mit Daten aus einem XML File befüllen und anzeigen.
Den Report an sich kann man bequem im Visual Studio erstellen und abspeichern. Databindings und dergleichen sind nicht notwendig.

Die untere Methode ermöglicht das Speichern eines Reports als PDF-Datei.

Public Sub ShowReportViewer(ByVal aXMLFilename As String, ByVal aReportFilename As String, _
                              ByVal aDatasetName As String, ByVal aTableName As String, _
                              ByVal aDataTableIndex As Integer, _
                              Optional ByVal DoSaveReport As Boolean = False, Optional ByVal SaveReportAsFilename As String = "")
 
    If Not System.IO.File.Exists(aXMLFilename) Then
      MsgBox(String.Format("Die XML Datenquelle ""{0}"" wurde nicht gefunden!", aXMLFilename), MsgBoxStyle.Critical)
      Exit Sub
    End If
 
    If Not System.IO.File.Exists(aReportFilename) Then
      MsgBox(String.Format("Die Report Vorlage ""{0}"" wurde nicht gefunden!", aReportFilename), MsgBoxStyle.Critical)
      Exit Sub
    End If
 
    ' Create new DataSet and load Data from aXMLFilename into it
    Dim ds As New DataSet()
    ds.DataSetName = aDatasetName
    ds.ReadXml(aXMLFilename)
 
    ' Create Form
    Dim frm As New Windows.Forms.Form
    frm.StartPosition = Windows.Forms.FormStartPosition.CenterParent
    frm.Height = 400
    frm.Width = 400
 
    ' Create Report Data Source
    ' The most important part here is aDatasetName & "_" & aTablename
    Dim rds As New Microsoft.Reporting.WinForms.ReportDataSource(aDatasetName & "_" & aTableName, ds.Tables(aDataTableIndex))
 
    ' Create ReportViewer
    Dim rv As New Microsoft.Reporting.WinForms.ReportViewer
    rv.Dock = Windows.Forms.DockStyle.Fill
 
    ' Add ReportViewer to Form
    frm.Controls.Add(rv)
 
    ' Load Report Definition File
    Dim fs As New System.IO.FileStream(aReportFilename, IO.FileMode.Open)
    rv.LocalReport.LoadReportDefinition(fs)
 
    ' Add Report Data Source
    rv.LocalReport.DataSources.Clear()
    rv.LocalReport.DataSources.Add(rds)
    rv.RefreshReport()
    ' Save Report as File?
    If DoSaveReport Then
      SaveReport(rv.LocalReport, SaveReportAsFilename)
    End If
    ' Finally Show Form
    frm.ShowDialog()
  End Sub
Public Sub SaveReport(ByVal aLocalReport As Microsoft.Reporting.WinForms.LocalReport, ByVal aFilename As String, Optional ByVal aRenderFormat As String = "PDF")
    ' Todo: Check if aFilename already exists and prompt user to overwrite/skip
    Dim warnings As Microsoft.Reporting.WinForms.Warning() = Nothing
    Dim streamids As String() = Nothing
    Dim mimeType As String = Nothing
    Dim encoding As String = Nothing
    Dim extension As String = Nothing
    Dim bytes As Byte()
    Try
      bytes = aLocalReport.Render(aRenderFormat, Nothing, mimeType, encoding, extension, streamids, warnings)
      Dim fs As New IO.FileStream(aFilename, System.IO.FileMode.Create)
      fs.Write(bytes, 0, bytes.Length)
      fs.Close()
    Catch ex As Exception
      ' AddToLog(ex.Message)
    End Try
  End Sub

Beispielaufruf:

Public Sub Test()
    ShowReportViewer("C:\test.xml", "C:\repMain.rdlc", "NewDataSet", "Table", 0, True, "C:\test.pdf")
  End Sub

Ergänzung vom 17.02.2012:

Hier noch ein Beispiel, wie man einen Microsoft Report als eingebettete Ressource verwendet, den Report also nicht per Dateinamen angeben muss:

Dim bs As New System.Windows.Forms.BindingSource
    bs.DataMember = "dtReportDetails"
    bs.DataSource = Me
 
    Dim rds As New Microsoft.Reporting.WinForms.ReportDataSource("dsReport_dtReportDetails", bs)
 
    Dim rvMain As New Microsoft.Reporting.WinForms.ReportViewer
    rvMain.LocalReport.DataSources.Add(rds)
    rvMain.LocalReport.ReportEmbeddedResource = "Controlling.Neukunden.repSantaFuck.rdlc"
    rvMain.Dock = System.Windows.Forms.DockStyle.Fill
    rvMain.TabIndex = 0
    rvMain.RefreshReport()
 
    Dim frm As New Windows.Forms.Form
    frm.Controls.Add(rvMain)
    frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
    frm.WindowState = Windows.Forms.FormWindowState.Maximized
    frm.ShowDialog()
 1