C#: Print PDF at the Actual Size or Fit to Page
When printing a PDF, ensuring the content appears as intended is crucial. Depending on your needs, you may want to print the document at the actual size to maintain the original dimensions or scale it to fit the entire page for a better presentation.
To accommodate different printing needs, Spire.PDF for .NET provides flexible printing options that allow developers to control the output easily. This article will demonstrate how to print a PDF either at the actual size or fit to page in C# using the Spire.PDF for .NET library.
- Print a PDF to Fit the Page Size in C#
- Print a PDF at the Actual Size in C#
- Print a PDF at the Actual Size on Custom-Sized Paper
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Print a PDF to Fit the Page Size in C#
When printing a PDF to fit the page, the content is automatically scaled to match the dimensions of the paper. This ensures that the document fits within the printed area, regardless of its original size.
To fit the content to the page, you can use the PdfDocument.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode pageScalingMode, bool autoPortraitOrLandscape) method. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load the PDF file using the PdfDocument.LoadFromFile() method.
- Configure print settings to scale the PDF to fit the page size for printing using the PdfDocument.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode pageScalingMode, bool autoPortraitOrLandscape) method.
- Call the PdfDocument.Print() method to print the PDF file.
- C#
using Spire.Pdf; using Spire.Pdf.Print; namespace PrintPdfToFitPageSize { internal class Program { static void Main(string[] args) { // Create an instance of the PdfDocument class PdfDocument pdf = new PdfDocument(); // Load the specified PDF file into the PdfDocument object pdf.LoadFromFile("Sample.pdf"); // Configure print settings to scale the PDF to fit the page size for printing pdf.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, false); // Execute the print command to print the loaded PDF document pdf.Print(); } } }
Print a PDF at the Actual Size in C#
When printing a PDF at the actual size, the original document dimensions are preserved without scaling. This ensures that the printed output matches the PDF's defined measurements.
To print a PDF at its actual size, you can also use the PdfDocument.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode pageScalingMode, bool autoPortraitOrLandscape) method. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load the PDF file using the PdfDocument.LoadFromFile() method.
- Configure print settings to print the PDF at its actual size without scaling using the PdfDocument.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode pageScalingMode, bool autoPortraitOrLandscape) method.
- Call the PdfDocument.Print() method to print the PDF file.
- C#
using Spire.Pdf; using Spire.Pdf.Print; using System.Drawing.Printing; namespace PrintPdfAtActualSize { internal class Program { static void Main(string[] args) { // Create a new instance of the PdfDocument class PdfDocument pdf = new PdfDocument(); // Load the PDF file into the PdfDocument object pdf.LoadFromFile("Sample.pdf"); // Set paper margins as 0 pdf.PrintSettings.SetPaperMargins(0, 0, 0, 0); // Configure print settings to print the PDF at its actual size without scaling pdf.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize, false); // Execute the print command to print the loaded PDF document pdf.Print(); } } }
Print a PDF at the Actual Size on Custom-Sized Paper
In some cases, you may need to print a PDF at its actual size on a specific size of paper. Spire.PDF allows you to define a custom paper size using the PaperSize class and then you can assign it to the print settings of the document using the PdfDocument.PrintSettings.PaperSize property. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load the PDF file using the PdfDocument.LoadFromFile() method.
- Define a custom paper size for printing using the PaperSize class.
- Assign the custom paper size to the print settings of the file using the using the PdfDocument.PrintSettings.PaperSize property.
- Configure print settings to print the PDF at its actual size without scaling using the PdfDocument.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode pageScalingMode, bool autoPortraitOrLandscape) method.
- Call the PdfDocument.Print() method to print the PDF file.
- C#
using Spire.Pdf; using Spire.Pdf.Print; using System.Drawing.Printing; namespace PrintPdfOnCustomSizedPaper { internal class Program { static void Main(string[] args) { // Create a new instance of the PdfDocument class PdfDocument pdf = new PdfDocument(); // Load the specified PDF file into the PdfDocument object pdf.LoadFromFile("Sample.pdf"); //// Define an A3 paper size for printing //PaperSize paperSize = new PaperSize //{ // // Set paper size to A3 // RawKind = (int)PaperKind.A3 //}; // Define a custom paper size for printing PaperSize paperSize = new PaperSize { // Set the width of the paper Width = 283 * 100, //inch*100 // Set the height of the paper Height = 826 * 100, //inch*100 // Set paper size to custom RawKind = (int)PaperKind.Custom }; // Assign the custom paper size to the print settings of the PdfDocument pdf.PrintSettings.PaperSize = paperSize; // Set paper margins as 0 pdf.PrintSettings.SetPaperMargins(0, 0, 0, 0); // Set print settings to print the PDF at its actual size without scaling pdf.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize, false); // Execute the print command to print the loaded PDF document pdf.Print(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Print PDF pages to booklet in C#
Booklets are usually used when we print huge PDF files. It saves paper and make the pages tidy. Starts from Spire.PDF V5.12.3, Spire.PDF supports to print the PDF pages to booklet directly. This article demonstrates how to print PDF pages to booklet in C#.
using Spire.Pdf; using Spire.Pdf.Actions; using Spire.Pdf.General; using Spire.Pdf.Print; namespace PDFPrintBookLet { class Program { static void Main(string[] args) { //Load the sample document PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Sample.pdf"); //Set booklet layout when print the pdf files PdfBookletSubsetMode bookletSubset = PdfBookletSubsetMode.BothSides; PdfBookletBindingMode bookletBinding = PdfBookletBindingMode.Left; doc.PrintSettings.SelectBookletLayout(bookletSubset, bookletBinding); //Print PDF to virtual printer doc.PrintSettings.PrinterName = "Microsoft XPS Document Writer"; doc.PrintSettings.PrintToFile("XpsBooklet.xps"); doc.Print(); } } }
Screenshot after printing to XPS:
Print a PDF in Greyscale in C#
Spire.PDF supports to print a PDF in greyscale. This article is going to show you how to use Spire.PDF to accomplish this function.
Below is the example PDF file we used for demonstration:
Detail steps:
Step 1: Create a PdfDocument instance and load the PDF file.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(@"Stories.pdf");
Step 2: Set the PdfPrintSettings.Color property to false.
pdf.PrintSettings.Color = false;
Step 3: Print the document.
pdf.Print();
Screenshot after printing to xps:
Full code:
using Spire.Pdf; namespace Print_PDF_in_Black_and_White { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(@"Stories.pdf"); pdf.PrintSettings.Color = false; pdf.Print(); } } }
Print different PDF pages to different printer trays in C#
This article demonstrates how to print different pages of a PDF document to different printer trays using Spire.PDF and c#.
Code snippets:
Step 1: Initialize an object of PdfDocument class and Load the PDF document.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"F:\sample.pdf");
Step 2: Set different printer trays for different pages of the document.
doc.PrintSettings.PaperSettings += delegate(object sender, PdfPaperSettingsEventArgs e) { //Set the paper source of page 1-50 as tray 1 if (1 <= e.CurrentPaper && e.CurrentPaper <= 50) { e.CurrentPaperSource = e.PaperSources[0]; } //Set the paper source of the rest of pages as tray 2 else { e.CurrentPaperSource = e.PaperSources[1]; } };
Step 3: Print the document.
doc.Print();
Full code:
using Spire.Pdf; using Spire.Pdf.Print; namespace Print_pages_to_different_printer_trays { class Program { static void Main(string[] args) { //Initialize an object of PdfDocument class PdfDocument doc = new PdfDocument(); //Load the PDF document doc.LoadFromFile(@"F:\sample.pdf"); //Set Paper source doc.PrintSettings.PaperSettings += delegate(object sender, PdfPaperSettingsEventArgs e) { //Set the paper source of page 1-50 as tray 1 if (1 <= e.CurrentPaper && e.CurrentPaper <= 50) { e.CurrentPaperSource = e.PaperSources[0]; } //Set the paper source of the rest of pages as tray 2 else { e.CurrentPaperSource = e.PaperSources[1]; } }; //Print the document doc.Print(); } } }
Show Print Preview of PDF file in C#
At some point, we may want to display a PDF file as it will appear when printed. This article demonstrates how to show print preview of a PDF file in Windows Forms application using Spire.PDF and c#.
Before using the following code, we need to create a windows forms application, add a PrintPreviewControl control to the form and reference Spire.Pdf.dll into the application.
using System; using System.Windows.Forms; using Spire.Pdf; namespace PreviewPDF { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void printPreviewControl1_Click(object sender, EventArgs e) { //Load PDF file PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("New Zealand.pdf"); //Set the PrintPreviewControl.Rows and PrintPreviewControl.Columns properties to show multiple pages this.printPreviewControl1.Rows = 2; this.printPreviewControl1.Columns = 2; //Preview the pdf file pdf.Preview(this.printPreviewControl1); } } }
Screenshot:
Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#
When it comes to printing, Spire.PDF offers users a wide range of options to fulfil their printing needs. Two of these options are “print multiple PDF pages per sheet” and “print single PDF page to multiple sheets”. This article elaborates how to achieve these two options using Spire.PDF and C#.
Print Multiple PDF Pages per Sheet
Uses can invoke the SelectMultiPageLayout(int rows, int columns) method of the PdfPrintSettings class to print multiple PDF pages per sheet of paper.
using Spire.Pdf; namespace PrintPDF { class Program { static void Main(string[] args) { //Initialize a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load the pdf file pdf.LoadFromFile("Input.pdf"); //Print two PDF pages per sheet pdf.PrintSettings.SelectMultiPageLayout(1, 2); pdf.Print(); } } }
Below screenshot shows the sample pdf document which contains two pages:
Screenshot after printing to XPS:
Print Single PDF Page to Multiple Sheets
The SelectSplitPageLayout() method of the PdfPrintSettings class can be used when printing a large PDF page to multiple sheets. This method splits the PDF page to multiple pages according to the standard A4 paper size: 595pt*842pt.
using Spire.Pdf; namespace PrintPDF2 { class Program { static void Main(string[] args) { //Initialize a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load the pdf file pdf.LoadFromFile("Input1.pdf"); //Print the PDF page to multiple sheets pdf.PrintSettings.SelectSplitPageLayout(); pdf.Print(); } } }
Below screenshot shows the sample pdf document which contains a single page with a size of 2100pt*750pt:
Screenshot after printing to XPS:
How to Print PDF Documents in C# (Without Adobe)
Printing PDF documents in C# can be achieved without Adobe Acrobat. Using Spire.PDF, developers can easily incorporate powerful printing functionalities into their applications. This library provides a smooth, programmatic method to manage everything from simple printing tasks to advanced features such as duplex printing and silent printing. Whether you need to print a single page or an entire multi-page document, Spire.PDF ensures precision and efficiency.
In this article, we’ll explore how to leverage Spire.PDF for .NET to print PDFs directly from your C# applications , customize print settings, and resolve common issues.
Best C# .NET Library for Printing PDF
When it comes to PDF printing in C#, several libraries are available, but Spire.PDF stands out as one of the most robust and developer-friendly options. Spire.PDF offers:
- Comprehensive PDF manipulation capabilities
- Simple yet powerful printing functionality
- No dependency on Adobe Acrobat
- Support for both Windows Forms and Console applications
- Extensive customization options for print settings
The library handles all the low-level complexities of PDF rendering and printer communication, allowing developers to focus on implementing business logic rather than wrestling with printer APIs.
To begin, install the Spire.PDF for .NET library via NuGet Package Manager :
Install-Package Spire.PDF
Alternatively, you can download Spire.PDF directly from our official website and reference the DLLs in your project.
Basic PDF Printing in C#: Directly Print PDF to Default Printer
Now, let's start with the simplest scenario: printing a PDF document to the system's default printer. Spire.PDF makes this incredibly straightforward:
using Spire.Pdf;
namespace PrintWithDefaultPrinter
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object
PdfDocument doc = new PdfDocument();
// Load a PDF file
doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");
// Print to default printer
doc.Print();
// Clean up resources
doc.Dispose();
}
}
}
This basic example demonstrates the core workflow:
- Create a PdfDocument instance.
- Load an existing PDF file.
- Call the Print() method to send the document to the default printer.
- Clean up resources.
The simplicity of this approach makes it ideal for scenarios where you just need to quickly print a document without any special requirements.
Advanced Print Settings in Spire.PDF
While the basic printing method works for simple cases, most real-world applications require more control over the printing process. Spire.PDF provides extensive print customization through its PrintSettings property.
1. Specify Printer Name
Instead of using the default printer, you can target a specific printer by name:
doc.PrintSettings.PrinterName = "Your Printer Name";
2. Set Print Page Range
For large documents, you might want to print only specific pages. Use SelectPageRange() to define a start and end page:
doc.PrintSettings.SelectPageRange(1, 5); // Pages 1 to 5
For non-sequential pages, use SelectSomePages() :
doc.PrintSettings.SelectSomePages(newint[] { 1, 3, 5, 7 });
3. Adjust Number of Copies
Need multiple copies? Set the Copies property to the desired number:
doc.PrintSettings.Copies = 2;
4. Enable Duplex (Double-Sided) Printing
Duplex printing can save paper and is commonly required for professional documents. Check if the printer supports it first:
if (doc.PrintSettings.CanDuplex)
{
doc.PrintSettings.Duplex = Duplex.Default;
}
5. Black and White (Grayscale) Printing
For documents where color isn't necessary, you can force grayscale printing:
doc.PrintSettings.Color = false;
6. Silent Printing (Hide Dialog Box & Process)
For automated workflows, you often want to print without any user interaction:
doc.PrintSettings.PrintController = new StandardPrintController();
This suppresses all print dialogs, making the process completely silent. Use this carefully, as it removes the user's ability to confirm or adjust settings.
7. Print Different Pages to Different Trays
Advanced printers with multiple paper trays can handle complex document assembly automatically:
// Register event handler for paper settings
doc.PrintSettings.PaperSettings += delegate (object sender, PdfPaperSettingsEventArgs e)
{
// Use tray 1 for pages 1-10
if (1 <= e.CurrentPaper && e.CurrentPaper <= 10)
{
e.CurrentPaperSource = e.PaperSources[0];
}
// Use tray 2 for pages beyond 10
else
{
e.CurrentPaperSource = e.PaperSources[1];
}
};
This feature allows for professional document production where cover pages, inserts, or chapter dividers can automatically print on different paper stock.
8. Print Multiple Pages Per Sheet
Optimize paper usage by printing multiple PDF pages on a single sheet. The SelectMultiPageLayout() method lets you specify the grid layout (rows × columns) for page arrangement:
doc.PrintSettings.SelectMultiPageLayout(2, 2); // Prints 4 pages per sheet (2 rows × 2 columns)
This setting is ideal for printing booklets, handouts, or draft documents while conserving paper. The pages are automatically scaled to fit the specified layout.
Conclusion
Printing PDFs programmatically in C# doesn't require Adobe Acrobat or complex printer APIs . With Spire.PDF, you can implement everything from simple printing to advanced, professional-grade output with just a few lines of code. The library abstracts away the complexities while providing fine-grained control when needed.
Whether you're building a document management system, a reporting tool, or any application that needs PDF printing capabilities, Spire.PDF offers a comprehensive solution that balances ease of use with powerful features. Start exploring its capabilities today and transform your PDF printing workflows!
FAQs
Q1: How do I print a PDF in C# without Adobe?
Use Spire.PDF’s Print() method to send the document directly to the printer.
Q2: Can I print PDFs to a network printer?
Yes, as long as the printer is properly installed on your system, you can specify it by name just like a local printer.
Q3: How to print a PDF in WPF or WinFroms?
The code snippets provided in this guide work seamlessly in both WPF and WinForms applications. In WPF, consider adding printer selection dialogs for an enhanced user experience.
Q4: How can I improve print quality when printing PDFs programmatically in C#?
You can control print quality by setting the printer resolution using the PrinterResolutionKind property. For high-quality output, use:
doc.PrintSettings.PrinterResolutionKind = PdfPrinterResolutionKind.High;
Spire.PDF supports these resolution options:
- Low (Draft quality)
- Medium (Standard quality)
- High (Best quality)
- Custom (Requires additional DPI settings)
Note: Actual output depends on your printer's capabilities. For photo-quality prints, ensure your printer supports high DPI (e.g., 1200x1200) and use high-quality paper. Combine this with doc.PrintSettings.Color = true for color-critical documents.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.