News Category

Print

Print (7)

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 PDF pages to booklet in C#

Print a PDF in Greyscale in C#

2018-03-20 09:35:03 Written by support iceblue

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:

Print a PDF in Black and White in C#

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:

Print a PDF in Black and White in C#

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();
        }
    }
}

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();
        }
    }
}

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:

Show Print Preview of PDF file 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:

Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#

Screenshot after printing to XPS:

Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#

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:

Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#

Screenshot after printing to XPS:

Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#

C#/VB.NET: Print PDF Documents

2022-03-17 07:38:00 Written by support iceblue

Sending PDF to a physical printer is one of the most common tasks in our daily lives. For example, you may need to print contracts, invoices or resumes on paper so people are able to view them without the use of a device. This article shows you how to print PDF documents in C# and VB.NET using Spire.PDF for .NET with the following seven examples.

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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Print PDF with the Default Printer

The following are the steps to print PDF documents with the default printer in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Call PdfDocument.Print() method to directly print the document with the default printer.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintWithDefaultPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Print with default printer 
            doc.Print();
        }
    }
}

Print Selected Pages with a Specified Printer

The following are the steps to print PDF documents with a specified printer in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Select discontinuous pages or continuous pages to print using PrintSettings.SelectSomePages() method or PrintSettings.SelectPageRange() method.
  • Call PdfDocument.Print() method to execute printing.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintWithSpecifiedPrinter
{
    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\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Select a page range to print
            doc.PrintSettings.SelectPageRange(1, 5);

            //Select discontinuous pages to print
            //doc.PrintSettings.SelectSomePages(new int[] { 1, 3, 5, 7 });

            //Print document
            doc.Print();
        }
    }
}

Print PDF to XPS using Microsoft XPS Document Writer

The following are the steps to print PDF to XPS using Microsoft XPS Document Writer in C# and VB.NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Set the printer name to Microsoft XPS Document Writer through PrintSettings.PrinterName property.
  • Set the printed file’s path and name using PrintSettings.PrintToFile() method.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Set printer name to Microsoft XPS Document Writer
            doc.PrintSettings.PrinterName = "Microsoft XPS Document Writer";

            //Set the printed file path and name
            doc.PrintSettings.PrintToFile("PrintToXps.xps");

            //Execute printing
            doc.Print();
        }
    }
}

Print PDF Silently

The following are the steps to silently print PDF documents in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Set the value of PrintSettings.PrintController property to an instance of StandardPrintController class, which will prevent the printing process from displaying.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing.Printing;

namespace PrintPdfSilently
{
    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\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Silent printing
            doc.PrintSettings.PrintController = new StandardPrintController();

            //Print document
            doc.Print();
        }
    }
}

Print PDF in Duplex Mode

The following are the steps to print PDF documents in duplex mode in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Determine if the printer supports duplex printing through PrintSettings.CanDuplex property. If yes, set PrintSettings.Duplex property to Duplex.Defatult.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing.Printing;

namespace PrintInDuplexMode
{
    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\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Determine if the printer supports duplex printing 
            if (doc.PrintSettings.CanDuplex)
            {
                //Set to duplex printing mode
                doc.PrintSettings.Duplex = Duplex.Default;

                //Print document
                doc.Print();
            }
        }
    }
}

Print PDF in Grayscale

The following are the steps to print PDF documents in grayscale in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Specify the printer name through the PrintSettings.PrinterName property.
  • Set the PrintSettings.Color property to false, which will print a color PDF in black and white.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PrintInGrayscale
{
    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\sample.pdf");

            //Specify printer name
            doc.PrintSettings.PrinterName = "HP LaserJet P1007";

            //Print in black and white
            doc.PrintSettings.Color = false;

            //Print document
            doc.Print();
        }
    }
}

Print Different Page Ranges to Different Trays

The following are the steps to print different page ranges to different trays in C# and VB.NET using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Register the PrintSettings.PaperSettings event with a custom delegate which provides data for paper setting event.
  • Set the paper source of the tray 1 and tray 2.
  • Execute printing using PdfDocument.Print() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Print;

namespace PrintToDifferentTrays
{
    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\sample.pdf");

            //Register the PaperSettings event with a delegate that handles paper settings event
            doc.PrintSettings.PaperSettings += delegate (object sender, PdfPaperSettingsEventArgs e)
            {
                // Set the paper source of tray 1 to 1-10 pages
                if (1 <= e.CurrentPaper && e.CurrentPaper <= 10)
                {
                    e.CurrentPaperSource = e.PaperSources[0];
                }

                //Set the paper source of tray 2 to the rest pages
                else
                {
                    e.CurrentPaperSource = e.PaperSources[1];
                }
            };

            //Print document
            doc.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 files in C#

2014-09-22 09:10:35 Written by support iceblue

PDF files can't be edited easily and for this reason, it is the most popular file format in business field. Printing PDF files becomes a widely asked requirement as a result. This tutorial focus on introducing how to print PDF files via a .NET PDF API. With the help of Spire.PDF for .NET, developers can finish the print function in a few lines codes to print the PDF files with the default printer or any other network connected printer. You can also print all the PDF pages or only print the selected pages you want.

ATTENTION THAT, if you are using the Spire.PDF Version 3.9.360 or above, please refer to tutorial here.

Step 1: Create a new PDF document and load a PDF from file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");

If you want to print all the pages in PDF file with the default printer, please go to Step 2. If you want to set the printer and only print some pages in the PDF file, please go to Step 3 directly.

Step 2: Print the PDF file with the default printer to print all the pages

doc.PrintDocument.Print();

Step 3: Set the Printer and select the pages you want to print in the PDF file.

PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
          
if (dialogPrint.ShowDialog() == DialogResult.OK)
   {
     //Set the pagenumber which you choose as the start page to print
     doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
     //Set the pagenumber which you choose as the final page to print
     doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
     //Set the name of the printer which is to print the PDF
     doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

     PrintDocument printDoc = doc.PrintDocument;
     printDoc.Print();
  }

By using the Step 2 method to print all the pages with the default printer, it will start to print the PDF files automatically when you process it. If you select the printer and the pages you choose to print, then you will get a printer dialog as below:

How to print PDF files in C#

The full codes of how to print PDF file in C#:

[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using System.Windows.Forms;
using Spire.Pdf.Annotations;
using Spire.Pdf.Widget;
using System.Drawing.Printing;


namespace PrintPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            //Use the default printer to print all the pages
            //doc.PrintDocument.Print();

            //Set the printer and select the pages you want to print

            PrintDialog dialogPrint = new PrintDialog();
            dialogPrint.AllowPrintToFile = true;
            dialogPrint.AllowSomePages = true;
            dialogPrint.PrinterSettings.MinimumPage = 1;
            dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
            dialogPrint.PrinterSettings.FromPage = 1;
            dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
          
            if (dialogPrint.ShowDialog() == DialogResult.OK)
            {
                doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
                doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
                doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

                PrintDocument printDoc = doc.PrintDocument;
                printDoc.Print();
            }

        }
    }
}