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