Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
This article is in a series of articles across our product platforms showing how to print different pages on different printer trays. We recently provided the PDFOne .NET version. Here is the Document Studio .NET version. While PDFOne can only print PDF documents, Document Studio can print DOCX, DOC and images in addition to PDF. As this article was being written, some customers wanted to know how to print documents with a print preview. So, there is now a small print preview and document preview option as well.
When using Document Studio, printing operations can be done by the DocumentPrinter
control. (The DocumentViewer
control also has a print method but the DocumentPrinter
provides more features.)
When you call DocumentPrinter.Print()
method, the printer control will print to the current default printer. To change current print settings, you should access the DocumentPrinter.PrintDocument
property.
This DocumentPrinter.PrintDocument
property wraps a System.Drawing.Printing.PrintDocument
object. The PrintDocument
exposes the printing subsystem available to the DocumentPrinter
instance. The DocumentPrinter.PrintDocument.PrintSettings.InstalledPrinters
array, for example, will provide you with the names of the currently installed printers.
To change current default printer, set DocumentPrinter.PrintDocument.PrinterSettings.PrinterName
property to one of the printer names in DocumentPrinter.PrintDocument.PrintSettings.InstalledPrinters
.
If you directly set the System.Drawing.Printing.PrintDocument.PrinterSettings.PrinterName
property, DocumentPrinter
will not be making any change. This is because DocumentPrinter
creates its own snapshot of the System.Drawing.Printing.PrintDocument
object when you call its constructor.
After a printer has been selected, the number of trays in that printer can be obtained from PrintDocument.PrinterSettings.PaperSources
collection.
To make DocumentPrinter
print to a particular tray, you need to set the PrintDocument.DefaultPageSettings.PaperSource
property to one of the trays in PrintDocument.PrinterSettings.PaperSources
collection.
You can set the paper tray in the DocumentPrinter.BeginPreparePage
event handler. The event arguments parameter for this handler includes the current page number.
To identify the selected document, you can use a DocumentViewer
control. However, there is a small issue in this. Currently, the DocumentViewer
control locks the document preventing the printer control from reading it. In our other products, a loaded document can be reused in other controls. This feature has not yet been introduced in XtremeDocumentStudio .NET. So, for now, the viewer needs to close the document so that the printer can load it. (We will update this article when the feature is added.) The
DocumentPrinter.PageScaling
options can be mapped to the
DocumentViewer.ZoomType
property.
To print a document, you need to call
DocumentPrinter.LoadDocument()
with the pathname or stream containing the document and then call DocumentPrinter.Print()
method. After the document has been printed, call the DocumentPrinter.CloseDocument()
.
There is a small issue here. The DocumentViewer
control used for the preview feature has already loaded the document needs to be printed. Currently, a file lock prevents the printer control from loading the document. In our other products, a printer control can reuse a document already loaded by a viewer control. This feature has not yet been introduced in Document Studio .NET. So, for now, the preview control needs to close the document so that the latter can be loaded by the printer control. We will add this feature in the next update and update this article subsequently.
In the following example, the form load event queries installed printers and makes them available in a list box.
When a printer is selected, its trays are listed in two list boxes - one for the first page and the other for the rest of the pages. The idea is you keep the glossy paper in the first tray and regular printer sheets in the other tray. (It was a user requirement.)
When a printer does not have more than one tray, DocumentPrinter.BeginPreparePage
event handler is not set.
When a file is selected, the document viewer control is used to provide a preview. The viewer control's ZoomType
property is used to provide alternate previews for the DocumentPrinter.PageScaling
setting.
Here is the code. (You can also find it in this sample project archive:
XDocDotNET-PrintToTrays.zip. To get this project working, please replace the PrinterTraySelection.xdoc_license_key
variable with your registered/trial license key. You also have to add references to the DLLs from your XtremeDocumentStudio installation folder.)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Gnostice.Documents.Controls.WinForms; using System.Drawing.Printing; namespace DocumentStudio_Examples { public partial class PrintToTraysForm : Form { DocumentPrinter dp; DialogResult dr; public PrintToTraysForm() { InitializeComponent(); Gnostice.Documents.Framework.ActivateLicense(PrinterTraySelection.xdoc_license_key); } private void Form1_Load(object sender, EventArgs e) { dp = new DocumentPrinter(); // Load all installed printers in to list box if (PrinterSettings.InstalledPrinters.Count > 0) { foreach (String sPrinterName in PrinterSettings.InstalledPrinters) { lbPrinters.Items.Add(sPrinterName); } lbPrinters.SelectedIndex = 0; dp.PrintDocument.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters[lbPrinters.SelectedIndex]; } // Load page scaling options from enumeration lbScaling.DataSource = System.Enum.GetValues(typeof(PageScalingOptions)); lbScaling.SelectedIndex = 1; } private void btnSelectFile_Click(object sender, EventArgs e) { dr = openFileDialog1.ShowDialog(); if (dr.Equals(DialogResult.OK)) { // Display the selected document in preview documentViewer1.LoadDocument(openFileDialog1.FileName); documentViewer1.ZoomType = StandardZoomType.FitPage; lbScaling.SelectedIndex = 1; } } // Always print first page of a document in a special tray void dp_BeginPreparePage(object sender, PrinterBeginPreparePageEventArgs e) { if (e.DocumentPageNumber == 1) { dp.PrintDocument.DefaultPageSettings.PaperSource = dp.PrintDocument.PrinterSettings.PaperSources[lbFirstPageTray.SelectedIndex]; } else { dp.PrintDocument.DefaultPageSettings.PaperSource = dp.PrintDocument.PrinterSettings.PaperSources[lbOtherPagesTray.SelectedIndex]; } } // Load trays available in the selected printer private void lbPrinters_SelectedIndexChanged(object sender, EventArgs e) { if (lbPrinters.SelectedIndex >= 0) { dp.PrintDocument.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters[lbPrinters.SelectedIndex]; lbFirstPageTray.Items.Clear(); lbOtherPagesTray.Items.Clear(); if (dp.PrintDocument.PrinterSettings.PaperSources.Count > 0) { foreach (PaperSource pps in dp.PrintDocument.PrinterSettings.PaperSources) { lbFirstPageTray.Items.Add(pps.SourceName); lbOtherPagesTray.Items.Add(pps.SourceName); } lbFirstPageTray.SelectedIndex = 0; lbOtherPagesTray.SelectedIndex = 0; } dp.PrintDocument.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters[lbPrinters.SelectedIndex]; } } private void btnPrintFile_Click(object sender, EventArgs e) { if (PrinterSettings.InstalledPrinters.Count > 0) { // Print first page in a different tray only when // there are more than one tray if (dp.PrintDocument.PrinterSettings.PaperSources.Count > 0) { dp.BeginPreparePage += dp_BeginPreparePage; } else { dp.BeginPreparePage -= dp_BeginPreparePage; } if (dr.Equals(DialogResult.OK)) { documentViewer1.CloseDocument(); dp.LoadDocument(openFileDialog1.FileName); dp.Print(); dp.CloseDocument(); documentViewer1.LoadDocument(openFileDialog1.FileName); } } else { MessageBox.Show("No printers are installed."); } } private void lbScaling_SelectedIndexChanged(object sender, EventArgs e) { if (documentViewer1.IsDocumentLoaded) { switch (lbScaling.SelectedIndex) { case 0: documentViewer1.ZoomType = StandardZoomType.ActualSize; dp.PageScaling = PageScalingOptions.Original; break; case 1: documentViewer1.ZoomType = StandardZoomType.FitPage; dp.PageScaling = PageScalingOptions.Fit; break; case 2: documentViewer1.ZoomType = StandardZoomType.FitWidth; dp.PageScaling = PageScalingOptions.ShrinkOverSizedPages; break; default: documentViewer1.ZoomType = StandardZoomType.FitPage; dp.PageScaling = PageScalingOptions.Fit; break; } } } } }
Here is the video demo. (We do not have a printer with multiple trays and we settled for software printers.)
To provide a print preview, as opposed to a document preview, you can use a System.Windows.Forms.PrintPreviewDialog
or a System.Windows.Forms.PrintPreviewControl
control. The Document
property of one of these controls needs to be set to the DocumentPrinter.PrintDocument
property and its done.
printPreviewControl1.Document = dp.PrintDocument; // or printPreviewDialog1.Document = dp.PrintDocument;
The installer/setup application for Document Studio .NET copies a source code project for a more elaborate print with preview project. It uses the System.Windows.Forms.PrintPreviewDialog
control, which wraps a preview, page navigation and print control. It also provides more UI to set print settings such as DocumentPrinter.PageScaling
.
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .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 DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
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. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-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 | |
---|---|
StarDocsCloud-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. |