Print PDF files C#

In today’s digital landscape, many organizations strive to reduce paper consumption. However, certain scenarios still necessitate printing—such as dispatching online orders that require printing order details stored in PDF format. Manually printing each document can be tedious, especially for large-scale operations. Thankfully, the Aspose.PDF for .NET API offers an efficient solution for printing PDF files programmatically.

In this guide, you will learn how to effectively print PDF files in .NET applications using C#. Here’s what you can expect to explore:

Automating the printing of PDF files in your .NET applications using C# is straightforward. Follow these simple steps to print a PDF file:

  1. Create an object of the PdfViewer class.
  2. Load the input PDF document.
  3. Print the PDF file.

Here’s a code snippet demonstrating how to print a PDF file using C#:

If your business requires printing multiple PDF files, the Aspose.PDF for .NET API can help streamline the process. Instead of printing each file individually, you can use a list to print multiple files efficiently. Here’s how to do it:

  1. Initialize a List<string> to hold the file names.
  2. Add the PDF files to the list.
  3. Load each PDF file.
  4. Print the multiple PDF files.

The following code snippet illustrates how to print multiple PDF files:

The Aspose.PDF API allows you to print specific pages from a PDF document. To achieve this, you must specify the page ranges you want to print. Follow these steps:

  • Set the input and output file paths.
  • Define the page range you wish to print.
  • Specify the printing parameters.
  • Execute the print command.

Here’s a code snippet that demonstrates how to print specific pages of a PDF document:

PDF files can be secured with passwords, which may include user or owner passwords. User passwords are necessary to open and view the PDF, while owner passwords are required for modifications. Here’s how to print a secured PDF file:

  1. Load the secured PDF with the password.
  2. Create a PdfViewer object.
  3. Print the secured PDF file.

The following code snippet illustrates how to print a secured PDF file using C#:

You can direct your PDF prints to specific paper trays using the Aspose.PDF for .NET API. For example, you might want to print image-heavy PDFs from one tray and text-based PDFs from another. Follow these steps:

  1. Load the input PDF file.
  2. Set the printing attributes.
  3. Specify PageSettings and PaperSource.
  4. Call the PrintDocumentWithSettings method.

Here’s a code snippet that demonstrates how to print to a specific paper tray:

Print Page Range to Different Paper Sources using C#

In some cases, you may want to print different pages of the same PDF document to different paper sources. For instance, you might print the cover page from one tray and the subsequent pages from another. Here’s how:

  1. Initialize an object of the PdfViewer class.
  2. Use the PdfQueryPageSettings event handler delegate.
  3. Set the page and printer settings.
  4. Call the PrintDocumentWithSettings method.

Here’s a code snippet that shows how to print different page ranges to different paper sources:

Check Print Job Status while Printing PDF with C#

When printing PDF files to various printers, such as Microsoft Print to PDF or any physical printer, monitoring the print job status is crucial. This is especially important for large documents or if there’s a chance the printing could fail. Here’s how to check the print job status:

  1. Load the input PDF file.
  2. Specify the page settings.
  3. Set the printer name.
  4. Print the PDF document using the PrintDocumentWithSettings method.

The following code snippet demonstrates how to check the print job status:

This code snippet will output the printing status of the PDF file to the console of your .NET application:

C# Print Pdf

Conclusion

In this article, we explored various methods for printing both secured and unsecured PDF files using C# and the Aspose Plugin. We covered how to print entire PDF documents, specific page ranges, and how to direct prints to specific paper trays or bins. Additionally, we learned how to monitor the print status of documents, ensuring a smooth printing experience in your applications.