Document Library for WinForms | ComponentOne
PdfDocumentSource / Print Document
In This Topic
    Print Document
    In This Topic

    PdfDocumentSource allows you to print a PDF document programmatically. It provides support for printing using the Print method of the C1DocumentSource abstract class. The Print method has two overloads, Print (PrinterSettings printerSettings) and Print (C1PrintOptions options). So, you can add the method using C1PdfDocumentSource to print a document without even the need of a viewer.

    Image showing user printing the files

    The following sections explain how the Print method can be used. The code snippets in this topic use the Print (C1PrintOptions options) method overload.

    1. Add a button control to the form for exporting PDF documents.
    2. Drag and drop PdfDocumentSource 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.
      C#
      Copy Code
      using C1.Win.C1Document;
      
    4. Add a PDF document to the project.
    5. Load the PDF document into the object of C1PdfDocumentSource class using LoadFromFile method:
      C#
      Copy Code
      c1PdfDocumentSource1.LoadFromFile(@"..\..\DefaultDocument.pdf");
      
    6. Add the following code to the button's click event to print the PDF document using Print method.
      C#
      Copy Code
      private void btnPrint_Click(object sender, EventArgs e)
      {
                 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);
                  }
      }