ComponentOne Document Library for WinForms
Print PDF
PdfDocumentSource for WinForms > 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 form for exporting PDF.
  2. Drag and drop C1PdfDocumentSource and PrintDialog components from the Toolbox on the form to add them to the component tray.
  3. Add the following namespace in the code view.
    Imports C1.Win.C1Document
    
    using C1.Win.C1Document;
    
  4. Add a PDF file to the project. In our case, we have used PDF file named DefaultDocument.pdf from the product sample.
  5. Load the PDf file into the object of C1PdfDocumentSource using LoadFromFile method:
    C1PdfDocumentSource1.LoadFromFile("..\..\DefaultDocument.pdf")
    
    c1PdfDocumentSource1.LoadFromFile(@"..\..\DefaultDocument.pdf");
    
  6. Add the following code to the button's click event to print the PDF file using Print method.
    If PrintDialog1.ShowDialog(Me) <> DialogResult.OK Then
        Return
    End If
    
    Try
        Dim po As New C1PrintOptions()
        po.PrinterSettings = PrintDialog1.PrinterSettings
        'Print PDF
        C1PdfDocumentSource1.Print(po)
        MessageBox.Show(Me, "Document was successfully printed.",
                        "Information", MessageBoxButtons.OK,
                        MessageBoxIcon.Information)
    Catch ex As Exception
        MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.[Error])
    End Try
    
    if (printDialog1.ShowDialog(this) != DialogResult.OK)
        return;
    
    try
    {
        C1PrintOptions po = new C1PrintOptions();
        po.PrinterSettings = printDialog1.PrinterSettings;
        //Print PDF
        c1PdfDocumentSource1.Print(po);
        MessageBox.Show(this, "Document was successfully printed.",
                        "Information", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
    }
    
See Also