Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||







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.
Suggested Reading:
---o0O0o---
| Our Developer Tools | |
|---|---|
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
XtremePDFConverter VCLA Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents. |
|
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
XtremeDocumentStudio .NETMulti-format document-processing component suite for .NET developers |
PDFOne (for Java™)A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications. |
XtremeFontEngine (for Java)Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts |
| Our Office Productivity Applications |
|---|
Free PDF ReaderA free, fast, and portable application for viewing, printing and converting PDF documents. |
| Privacy | Legal | Feedback | Newsletter | Resellers | © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved. |
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).