Printing is a function that has been around in Aspose.Words for .NET for quite a while. We are proud to announce to that starting in Version 10.5, printing is also fully supported in Aspose.Words for Java. You can easily print all or only specified pages of a document with just a few lines of code.
TheAspose.Words for Java API provides the handy Document.Print method whose overloads allow you to print a document using various different options with just one line of code. You can choose to print using default settings, print to a specific printer by name and even printing options such as page range to print. (read more)
Pass Any Document Loaded into Aspose.Words to Standard Java Printing Devices
Aspose.Words provides the AsposeWordsPrintDocument class which allows any document loaded into Aspose.Words to be printed with the standard Java printing API. This class provides the “typical” one document page per physical page printing.
The AsposeWordsPrintDocument class implements the following print interfaces:
- Pageable
- Printable
This class can be passed directly to standard Java print classes such as PrinterJob using PrinterJob.setPageable or PrinterJob.setPrintable and allows you to easily print the document with dialogs such as the standard print dialog.
Previewing a Document before Printing
The standard Java API doesn’t provide any existing functions to print preview any Java on its own. With Aspose.Words, not only do we provide the facilities to print documents, we also provide sample code to preview documents before printing in the DocumentPreviewAndPrint sample. The preview provided by the print preview dialog is the same as what is expected when the document it printed to a real printer. Check out the images below for a small demonstration.
The input document to be printed.
Additionally, the full source document in DOC format is available for download from this link.
Next, the print preview of the above document generated using Aspose.Words for Java.
Finally, the physical output printed on a local printer using Aspose.Words for Java. As you can see Aspose.Words prints this document with high fidelity.
Print Multiple Document Pages onto a Single Sheet of Paper
You can utilize Aspose.Words for Java to fully customise how documents are printed. An example of this is printing multiple document pages onto a physical page by implementing your own print document class. Achieving this using Aspose.Words is a straight forward task. We have already provided the sample code ready for use in the MultiplePagesOnSheet sample.
The output of running this sample code on the same document as previous with settings of four pages on one sheet:
Implementing Custom Printing Logic
Finally, it’s worth mentioning you can use the built-in methods in the Aspose.Words API to implement your own printing classes based on your own logic. Without going into too much detail, this is the basis of how the previous sample of printing multiple pages on one sheet is implemented. Using such methods as Document.renderToSize, Document.getPageInfo you can use Aspose.Words to integrate with Java’s printing framework, customize the print process to your liking and custom print documents loaded into Aspose.Words.
You can check out an example of this in action by taking a look at the code for the previous sample. The full code for this sample can be downloaded from here: Program.java
Find out More
You can test out all these great features for yourself by downloading the latest version of Aspose.Words for Java from the Aspose.Words Downloads page.