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.