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

How To Queue PDFPrinter Print Jobs In PDFOne .NET

Learn to print PDF documents asynchronously.

PDFPrinter is the PDF printer component in PDFOne .NET. In this article, you will learn how to queue PDFPrinter print jobs.

When a PDFPrinter instance starts a print job, control does not return until the job has completed, as the printer component's print method is a simple blocking (synchronous) method. It is not possible to give another print run of the same job until the original print command has returned. It also precludes the possibility of using the same PDFPrinter instance or a different instance in the same routine to load and print a different document, as control remains with a previously issued print command.

In the following code snippet, multiple print commands are issued in quick succession. After issuing the commands, print jobs get neatly queued with the physical printer. All thanks to the asynchronous printing.

// Delegate for asynchronous printing
public delegate void AsyncDelegate(string filename, string docname);

public partial class Form1 : Form {
  // Other form-related code omitted

  // Routine that does the actual printing
  public void PrintMe(string filename, string docname) {
    PDFPrinter p = new PDFPrinter();


    if (p.LoadDocument(filename) == PrinterErrors.Success) {
      // Set print job options - print only the first page.
      p.PrintOptions.PrintRange = PrintRange.Selection;
      p.SelectedPages = "1";

      // Start the print job
      p.Print(docname);
    }
   
    p.CloseDocument();
    p.Dispose();
  }

  // 'Print' button 'click' event handler.
  // Calls above routine asynchronously.
  private void button1_Click(object sender, EventArgs e) {
    AsyncDelegate ad = new AsyncDelegate(this.PrintMe);
    IAsyncResult[] ar = new IAsyncResult[10];
    string[] files = new string[10];

    // Code to populate 'files[]' array with names of
    // files omitted

    for (int i = 0; i < 10; i++) {
      ar[i] = ad.BeginInvoke(files[i], i.ToString(), null, null);
    }

    for (int j = 0; j < 10; j++) {
      ad.EndInvoke(ar[j]);
    }

  }
}

In the above code snippet, a synchronous or blocking routine does the actual printing. This routine creates a PDFPrinter and prints a document to the physical document. However, this routine is not called directly. The button-click event handler uses a delegate with the same signature as the blocking routine. The BeginInvoke method of the delegate calls the blocking routine asynchronously in a secondary thread. So, multiple instances of PDFPrinter are created and a print job is issued by each instance.

The results of the BeginInvoke calls are stored in an IAsyncResult array. The EndInvoke method of the delegate uses the IAsyncResult array in a for loop to obtain the results of individual print jobs.

A point to note here is that EndInvoke is a blocking method. It will not return until an asynchronous call referred to it has returned. So, the button click event handler will not return until all print jobs have finished.

If you wish to have the event handler to return control immediately, then declare the delegate and the IAsyncResult array outside the handler. More importantly, use the for loop calling EndInvoke outside the event handler.

---o0O0o---

Suggested Reading:

---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-2025 Gnostice Information Technologies Private Limited. All rights reserved.