# Print PDF

## Content

PdfDocumentSource allows you to print a PDF file. It provides support for printing through the [Print](/componentone/api/wpf/online-document/dotnet-framework-api/C1.WPF.Document.4.6.2/C1.WPF.Document.C1DocumentSource.Print.html) method of the [C1DocumentSource](/componentone/api/wpf/online-document/dotnet-framework-api/C1.WPF.Document.4.6.2/C1.WPF.Document.C1DocumentSource.html) 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.
<br>
    ```vbnet
    Imports C1.WPF.Document
    ```

    ```csharp
    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:
<br>
    ```vbnet
    Dim pds As New C1PdfDocumentSource()
    Dim pdialog As New PrintDialog()
    ```

    ```csharp
    C1PdfDocumentSource pds = new C1PdfDocumentSource();
    PrintDialog pdialog = new PrintDialog();
    ```
5. Load the PDf file into the object of C1PdfDocumentSource using [LoadFromFile](/componentone/docs/wpf/online-document/) method:
<br>
    ```vbnet
    pds.LoadFromFile("..\..\DefaultDocument.pdf")
    ```

    ```csharp
    pds.LoadFromFile(@"..\..\DefaultDocument.pdf");
    ```
6. Add the following code to the button's click event to print the PDF file using **Print** method.
<br>
    ```vbnet
    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
    ```

    ```csharp
    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

[Export PDF](/componentone/docs/wpf/online-document/PDFDocumentSource-for-WPF/Features/Export-PDF)
[Text Search](/componentone/docs/wpf/online-document/PDFDocumentSource-for-WPF/Features/Text-Search)