PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

Print different PDF pages to different paper trays in .NET

Learn to do this using PDFOn .NET.
By Lakshana

Our customers often ask if this is possible. And, here is the code showing how it can be done. The PrintOptions property of PDFOne's PDFPrinter component is a System.Drawing.Printing.PrinterSettings object. It is directly mapped to the current printer settings. To print different pages to different trays, you just need to know the correct index of the trays.

If you know only the names, then you will have to iterate the PrintOptions.PaperSources collection and find the correct paper tray.

namespace PrintToDifferentTrays {
  public partial class Form1 : Form {
    PDFPrinter prn;
    PaperSource GlossyPaperTray, OrdinaryPaperTray;

    public Form1() {      
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
      bool bTray1Found = false, bTray2Found = false;

      // Create new printer
      prn = new PDFPrinter(LICENSE_KEY);
      // Load a document for printing
      prn.LoadDocument("sample.pdf");

      // Get current printer settings
      PrinterSettings ps = prn.PrintOptions;

      // Iterate through all trays and find the ones named "tray1" and "tray2"
      // (Assuming that "tray1" contains ordinary paper and "tray2" contains
      // glossy paper. If either one of the trays are not found, then the
      // default paper tray will be used. Hopefully, it is not the one with 
      // the glossy paper!)
      if (ps.PaperSources.Count > 1) { 
        for (int i = 0; i < ps.PaperSources.Count; i++) {
          if (ps.PaperSources[i].SourceName.Equals("tray1")) { 
            bTray1Found = true; 
            OrdinaryPaperTray = ps.PaperSources[i]; 
          }
          if (ps.PaperSources[i].SourceName.Equals("tray2")) { 
            bTray1Found = true; 
            GlossyPaperTray = ps.PaperSources[i]; 
          }
        }
        if (bTray1Found && bTray2Found) {
          prn.ChangePaperSource += prn_ChangePaperSource;
        }
      }
      
      prn.Print();
      prn.CloseDocument();

    }


    void prn_ChangePaperSource(
        object sender, 
        int pageNumber, 
        ref System.Drawing.Printing.PaperSource paperSource) {

      // Print first page on glossy paper; others on ordinary
      if (pageNumber == 1) {  
        paperSource = GlossyPaperTray;
      } else {               
        paperSource = OrdinaryPaperTray;
      }
    }
  }
}

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.