You can download the complete source code of the DocumentPreviewAndPrint sample here.
When working with documents you will often require printing them to a selected printer. A helpful addition is the ability to visually preview how the document will look on paper through a print preview and to choose options which define the behavior of how the document is printed.
The Aspose.Words component has no built in dialogs or forms, but implements its own version of the Java Printable interface which can be passed to a PrinterJob instance and display print settings by invoking the printDialog method.
Aspose.Words defines a special class called AsposeWordsPrintDocument which implements the Printable and pageable interfaces. This allows a document to be passed to the standard Java Print API for printing. An instance of this object is passed to the PrinterJob class which defines the output to transmit to a printer.
This sample describes how to use these classes to print a document from Aspose.Words with print preview and settings dialog. The Java printing framework handles the actual printing, however it does not ship with any built-in document preview functionality. Instead, in this sample we provide our own custom print preview dialog to demonstrate this functionality.
To accomplish this task the following steps are used:
1. Load your document into the Document class.
2. Create the Print dialog, initialize it with the default parameters and display it on the screen.
3. Check the dialog result and proceed if the user accepted the printing dialog.
4. Create an instance of the AsposeWordsPrintDocument class.
5. Create the Print Preview dialog and specify the AsposeWordsPrintDocument as the target document, pass the user specified print settings and then show the dialog.
The following Word document is used in this sample, it consists of two portrait orientated pages:
The first step is the creation of the Print dialog and initializing the default settings.
Example
Creates the print dialog.
[Java]
PrinterJob pj = PrinterJob.getPrinterJob();
// Initialize the Print Dialog with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
Everything is quite simple and straightforward. The page range of the document is calculated and passed to the print dialog. These define the default values for the page range. By default all pages in the range are selected for printing.
After initialization the dialog is displayed and the result is processed. It makes sense to proceed with the document preview only if the instance of the PrintDialog returns true, as other results means that the user decided to cancel the print operation.
Example
Check if the user accepted the print and proceed to preview the document.
[Java]
// Proceed with print preview only if the user accepts the print dialog.
if (!pj.printDialog(attributes))
return;
The next step involves creating an instance of the AsposeWordsPrintDocument. This is the Aspose.Words' implementation of of the Pageable and Printable interface which allows you to pass an Aspose.Words Document to a printer function accepting a Printable or Printable interface.
Example
Creates a special Aspose.Words implementation of the Java Pageable interface.
[Java]
// This object is responsible for rendering our document for use with the Java Print API.
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
// Pass our document as pageable to the printer job.
pj.setPageable(awPrintDoc);
Finally, an instance of the PrintPreviewDialog is created and displayed. This is a custom class which is provided along with this sample which will acts like a standard print preview dialog. It will accept an object implementing either the Pagable or Printable interface. Through the use of the setPrinterAttributes method you can pass the desired page range contained in the PrinterRequestAttributeSet object. The display method is then invokved to display the preview of the document to the user. This preview dialog includes other useful controls to zoom in and out and to change pages.
If you pass the AsposeWordsPrintDocument instance or your own object to the constructor accepting a Printable interface then a Page Setup option becomes enabled on the print preview dialog. This is because an object which implements only the Printable interface does not define page settings for each page. This option provides a further dialog which allows you to choose your own page settings to be used for each page of the document.
On the other hand, if you pass the AsposeWordsPrintDocument instance or your own object to the constructor accepting a Pageable interface then the Page Setup option is disabled as the page settings for each page is taken from the object instead through the Pageable.getPageFormat method.
The code behind the PrintPreviewDialog class is not shown here as it is out of the scope of the article. However the full code is shipped with the samples archive.
Example
Creates a custom Print Preview dialog which accepts a Printable or Pageable object and displays a preview of the document.
[Java]
// Create an instance of the print preview dialog and pass the print dialog and our document.
// Note that AsposeWordsPrintDocument implements both the Pageable and Printable interfaces. If the pageable constructor for PrintPreviewDialog
// is used then the formatting of each page is taken from the document. If the printable constructor is used then Page Setup dialog becomes enabled
// and the desired page setting for all pages can be chosen there instead.
PrintPreviewDialog previewDlg = new PrintPreviewDialog(awPrintDoc);
// Pass the desired page range attributes to the print preview dialog.
previewDlg.setPrinterAttributes(attributes);
// Proceed with printing if the user accepts the print preview.
if(previewDlg.display())
pj.print(attributes);
When the sample code above is running the following results are displayed.
The Print dialog:
The Print Preview dialog: