ComponentOne Document Library for WPF
Print PDF
PdfDocumentSource for WPF > Features > Print PDF

PdfDocumentSource allows you to print a PDF file. It provides support for printing through the Print method of the C1DocumentSource abstract class. The Print method has two overloads, Print (PrinterSettings printerSettings) and Print (C1PrintOptions options). You can add the Print method using C1PdfDocumentSource to print a PDF without the need of a viewer. Following code explains how the method can be used. The code in the topic uses Print (C1PrintOptions options) method for printing a PDF file.

To print PDF

  1. Add a button control to the design view for exporting PDF.
  2. Add the following namespace in the code view.
    Imports C1.WPF.Document
    
    using C1.WPF.Document;
    
  3. Add a PDF file to the project. In our case, we have used PDF file named DefaultDocument.pdf from the product sample.
  4. Initialize the instances of C1PdfDocumentSource and PrintDialog class using the following code:
    Dim pds As New C1PdfDocumentSource()
    Dim pdialog As New PrintDialog()
    
    C1PdfDocumentSource pds = new C1PdfDocumentSource();
    PrintDialog pdialog = new PrintDialog();
    
  5. Load the PDf file into the object of C1PdfDocumentSource using LoadFromFile method:
    pds.LoadFromFile("..\..\DefaultDocument.pdf")
    
    pds.LoadFromFile(@"..\..\DefaultDocument.pdf");
    
  6. Add the following code to the button's click event to print the PDF file using Print method.
    pdialog.MaxPage = CUInt(pds.PageCount)
    Dim dr As System.Nullable(Of Boolean) = pdialog.ShowDialog()
    
    Try
       Dim po = New C1PrintOptions()
       po.PrintQueue = pdialog.PrintQueue
       po.PrintTicket = pdialog.PrintTicket
       If pdialog.PageRangeSelection = PageRangeSelection.UserPages Then
            po.OutputRange = New OutputRange(pdialog.PageRange.PageFrom, _
                                             pdialog.PageRange.PageTo)
       End If
    
       'Print PDF
       pds.Print(po)
       MessageBox.Show(Me, "Document was successfully printed.", _
                       "Information", MessageBoxButton.OK, _
                       MessageBoxImage.Information)
    Catch ex As Exception
       MessageBox.Show(Me, ex.Message, "Error", MessageBoxButton.OK, _
                       MessageBoxImage.[Error])
    End Try
    
    pdialog.MaxPage = (uint)pds.PageCount;
    bool? dr = pdialog.ShowDialog();
    
    try
    {
        var po = new C1PrintOptions();
        po.PrintQueue = pdialog.PrintQueue;
        po.PrintTicket = pdialog.PrintTicket;
        if (pdialog.PageRangeSelection == PageRangeSelection.UserPages)
            po.OutputRange = new OutputRange(pdialog.PageRange.PageFrom,
                                             pdialog.PageRange.PageTo);
    
        //Print PDF
        pds.Print(po);
        MessageBox.Show(this, "Document was successfully printed.",
                        "Information", MessageBoxButton.OK,
                        MessageBoxImage.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, ex.Message, "Error",
                        MessageBoxButton.OK, MessageBoxImage.Error);
    }
    
See Also