AsposeWordsPrintDocument
Inheritance: java.lang.Object
All Implemented Interfaces: java.awt.print.Pageable, java.awt.print.Printable
public class AsposeWordsPrintDocument implements Pageable, Printable
Provides a default implementation for printing of a Document within the Java printing framework.
To learn more, visit the Printing a Document Programmatically or Using Dialogs documentation article.
Remarks:
AsposeWordsPrintDocument overrides both java.awt.print.Printable and java.awt.print.Pageable.
A single Aspose.Words document can consist of multiple sections that specify pages with different sizes, orientation and paper trays. AsposeWordsPrintDocument should be used as java.awt.print.Pageable to properly print each of the different paper size, orientation, etc.
On the other hand, if the document consists of a single section only, the developer can use AsposeWordsPrintDocument as java.awt.print.Printable to improve printing performance.
Examples:
Shows how to monitor printing progress.
Document doc = new Document(getMyDir() + "Rendering.docx");
// Create a special Aspose.Words implementation of the Java PrintDocument class
AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(doc);
// In Java, printer settings are handled through PrinterJob.
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(printDoc);
// Initialize the custom printing tracker.
PrintTracker printTracker = new PrintTracker(printDoc);
printerJob.print();
// Write the event log.
for (String eventString : printTracker.getEventLog()) {
System.out.println(eventString);
}
Shows an example class for monitoring the progress of printing.
{@code
///
/// Tracks printing progress of an Aspose.Words document and logs printing events.
///
/**
Tracks printing progress of an Aspose.Words document and logs printing events.
Note: Java version doesn't have the same event system as .NET, so this implementation
wraps the AsposeWordsPrintDocument to provide similar functionality.
/
class PrintTracker implements Printable {
private final AsposeWordsPrintDocument printDocument;
private int printingPage = -1;
private int totalPages = 0;
private final List eventLog = new ArrayList<>();
private boolean isPrinting = false;
/**
Initializes a new instance of the PrintTracker class
and wraps the specified Aspose.Words print document.
Constructors
| Constructor | Description |
|---|---|
| AsposeWordsPrintDocument(Document document) | Initializes a new instance of this class. |
Methods
| Method | Description |
|---|---|
| getColorMode() | Gets how non-colored pages are printed if the device supports color printing. |
| getColorPagesPrinted() | Gets the number of pages printed in color (i.e. |
| getNumberOfPages() | |
| getPageFormat(int pageIndex) | |
| getPageIndexFilter() | Gets the zero-based index filter used to determine which pages to skip during printing. |
| getPagesRemaining() | Gets the number of pages remaining in the currently active print job. |
| getPrintable(int pageIndex) | |
| getTotalPagesPrinted() | Gets the total number of pages actually printed during the print session. |
| print(Graphics graphics, PageFormat pageFormat, int pageIndex) | |
| setColorMode(int value) | Sets how non-colored pages are printed if the device supports color printing. |
| setPageIndexFilter(IIndexFilter value) | Sets the zero-based index filter used to determine which pages to skip during printing. |
AsposeWordsPrintDocument(Document document)
public AsposeWordsPrintDocument(Document document)
Initializes a new instance of this class.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| document | Document | The document to print. |
getColorMode()
public int getColorMode()
Gets how non-colored pages are printed if the device supports color printing.
Remarks:
Doesn’t affect booklet printing.
Returns: int - How non-colored pages are printed if the device supports color printing. The returned value is one of ColorPrintMode constants.
getColorPagesPrinted()
public int getColorPagesPrinted()
Gets the number of pages printed in color (i.e. with PageSettings#getColor().getColor() / PageSettings#setColor(boolean).setColor(boolean) set to true).
Returns: int - The number of pages printed in color (i.e.
getNumberOfPages()
public int getNumberOfPages()
Returns: int
getPageFormat(int pageIndex)
public PageFormat getPageFormat(int pageIndex)
Parameters:
| Parameter | Type | Description |
|---|---|---|
| pageIndex | int |
Returns: java.awt.print.PageFormat
getPageIndexFilter()
public IIndexFilter getPageIndexFilter()
Gets the zero-based index filter used to determine which pages to skip during printing.
Remarks:
Please note that if some pages are skipped, the actual total number of pages to print will not be known until the printing process is complete. This may impact print process tracking if the initial expected number of pages is greater than the number actually printed.
Examples:
Shows how to filtering pages using a page number list.
{@code
public void pageIndexFilter() throws Exception
{
Document doc = new Document("Rendering.docx");
// Configure printer settings and create print document.
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(printerJob.getPrintService());
// Create Aspose.Words print document.
AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(doc);
// Set the printer name.
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (PrintService service : printServices) {
if (service.getName().equalsIgnoreCase("Microsoft Print to PDF")) {
printerJob.setPrintService(service);
break;
}
}
// The test document has 5 pages. To skip pages 2, 4, and 5,
// specify the zero-based indices of pages to exclude.
HashSet pagesToSkip = new HashSet<>(Arrays.asList(1, 3, 4));
// Apply the page filter to skip specified pages.
printDoc.setPageIndexFilter(new PrintPagesFilter(pagesToSkip));
// Initialize custom printing tracker (optional).
PrintTracker printTracker = new PrintTracker(printDoc);
// Print the document (only pages 1 and 3 will be printed).
printerJob.setPrintable(printDoc);
printerJob.print();
}
///
/// Filter for skipping specified pages during printing.
///
public final class PrintPagesFilter implements IIndexFilter {
private final HashSet pagesToSkip;
///
/// Initializes a new instance of the class.
///
/// The collection of page indices to skip.
public PrintPagesFilter(HashSet pagesToSkip) {
if (pagesToSkip == null)
throw new IllegalArgumentException("pagesToSkip cannot be null.");
this.pagesToSkip = pagesToSkip;
}
Returns: IIndexFilter - true if the page should be skipped; otherwise, false. /
getPagesRemaining()
public int getPagesRemaining()
Gets the number of pages remaining in the currently active print job.
Remarks:
This value is updated automatically as pages are printed, reflecting the current print job’s progress. Outside of print time and in case of print job errors or interruptions, the value may not reflect the actual number of pages pending.
Examples:
Shows how to monitor printing progress.
Document doc = new Document(getMyDir() + "Rendering.docx");
// Create a special Aspose.Words implementation of the Java PrintDocument class
AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(doc);
// In Java, printer settings are handled through PrinterJob.
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(printDoc);
// Initialize the custom printing tracker.
PrintTracker printTracker = new PrintTracker(printDoc);
printerJob.print();
// Write the event log.
for (String eventString : printTracker.getEventLog()) {
System.out.println(eventString);
}
Shows an example class for monitoring the progress of printing.
{@code
///
/// Tracks printing progress of an Aspose.Words document and logs printing events.
///
/**
Tracks printing progress of an Aspose.Words document and logs printing events.
Note: Java version doesn't have the same event system as .NET, so this implementation
wraps the AsposeWordsPrintDocument to provide similar functionality.
/
class PrintTracker implements Printable {
private final AsposeWordsPrintDocument printDocument;
private int printingPage = -1;
private int totalPages = 0;
private final List eventLog = new ArrayList<>();
private boolean isPrinting = false;
/**
Initializes a new instance of the PrintTracker class
and wraps the specified Aspose.Words print document.
Returns: int - The current page number being printed. / public int getPrintingPage() { return printingPage; } /** Gets the total number of pages to print. Returns 0 when no printing is in progress.
getPrintable(int pageIndex)
public Printable getPrintable(int pageIndex)
Parameters:
| Parameter | Type | Description |
|---|---|---|
| pageIndex | int |
Returns: java.awt.print.Printable
getTotalPagesPrinted()
public int getTotalPagesPrinted()
Gets the total number of pages actually printed during the print session.
Remarks:
Updated after printing completes. Returns 0 before printing starts.
Examples:
Shows how to filtering pages base on page color.
{@code
public void colorMode() throws Exception
{
// Load the document with 3 color pages and 2 black and white pages.
Document doc = new Document("Colored pages.docx");
// Print color pages to 'color' printer.
int colorPagesPrinted = printPages(doc, "Microsoft Print to PDF", true);
// Print black-and-white pages to 'black-and-white' printer.
int nonColorPagesPrinted = printPages(doc, "Microsoft XPS Document Writer", false);
// Verify that correct number of pages were printed in each case.
Assert.assertEquals(3, colorPagesPrinted);
Assert.assertEquals(3, nonColorPagesPrinted);
}
///
/// Prints document pages filtered by color requirements.
///
/// The document to print.
/// The name of the target printer.
///
/// true to print only color pages;
/// false to print only black and white pages.
///
/// The number of pages actually printed.
private int printPages(Document doc, String printerName, boolean colored) throws Exception
{
// Configure printer settings.
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Select target printer.
for (PrintService service : PrinterJob.lookupPrintServices()) {
if (service.getName().equalsIgnoreCase(printerName)) {
printerJob.setPrintService(service);
break;
}
}
// Create print document with color mode set to Normal.
AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(doc);
printDoc.setColorMode(ColorPrintMode.NORMAL);
// Filter pages: skip color pages when printing black and white, and vice versa.
printDoc.setPageIndexFilter(new ColorPagesFilter(doc, !colored));
printerJob.setPrintable(printDoc);
printerJob.print();
return printDoc.getTotalPagesPrinted();
}
///
/// A filter that selectively skips color or black-and-white pages during printing
/// based on the document's page information and specified filtering mode.
///
///
/// This filter implements the IIndexFilter interface to provide custom page selection
/// logic for printing operations. It can be configured to either skip color pages
/// (when printing only black-and-white content) or skip black-and-white pages
/// (when printing only color content).
///
static class ColorPagesFilter implements IIndexFilter
{
private final Document doc;
private final boolean skipColorPages;
/**
Initializes a new instance of the ColorPagesFilter class.
Returns: int - true if the page should be skipped; otherwise, false. /
print(Graphics graphics, PageFormat pageFormat, int pageIndex)
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
Parameters:
| Parameter | Type | Description |
|---|---|---|
| graphics | java.awt.Graphics | |
| pageFormat | java.awt.print.PageFormat | |
| pageIndex | int |
Returns: int
setColorMode(int value)
public void setColorMode(int value)
Sets how non-colored pages are printed if the device supports color printing.
Remarks:
Doesn’t affect booklet printing.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| value | int | How non-colored pages are printed if the device supports color printing. The value must be one of ColorPrintMode constants. |
setPageIndexFilter(IIndexFilter value)
public void setPageIndexFilter(IIndexFilter value)
Sets the zero-based index filter used to determine which pages to skip during printing.
Remarks:
Please note that if some pages are skipped, the actual total number of pages to print will not be known until the printing process is complete. This may impact print process tracking if the initial expected number of pages is greater than the number actually printed.
Examples:
Shows how to filtering pages using a page number list.
{@code
public void pageIndexFilter() throws Exception
{
Document doc = new Document("Rendering.docx");
// Configure printer settings and create print document.
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(printerJob.getPrintService());
// Create Aspose.Words print document.
AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(doc);
// Set the printer name.
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (PrintService service : printServices) {
if (service.getName().equalsIgnoreCase("Microsoft Print to PDF")) {
printerJob.setPrintService(service);
break;
}
}
// The test document has 5 pages. To skip pages 2, 4, and 5,
// specify the zero-based indices of pages to exclude.
HashSet pagesToSkip = new HashSet<>(Arrays.asList(1, 3, 4));
// Apply the page filter to skip specified pages.
printDoc.setPageIndexFilter(new PrintPagesFilter(pagesToSkip));
// Initialize custom printing tracker (optional).
PrintTracker printTracker = new PrintTracker(printDoc);
// Print the document (only pages 1 and 3 will be printed).
printerJob.setPrintable(printDoc);
printerJob.print();
}
///
/// Filter for skipping specified pages during printing.
///
public final class PrintPagesFilter implements IIndexFilter {
private final HashSet pagesToSkip;
///
/// Initializes a new instance of the class.
///
/// The collection of page indices to skip.
public PrintPagesFilter(HashSet pagesToSkip) {
if (pagesToSkip == null)
throw new IllegalArgumentException("pagesToSkip cannot be null.");
this.pagesToSkip = pagesToSkip;
}
Parameters:
| Parameter | Type | Description |
|---|---|---|
| value | IIndexFilter | The zero-based index filter used to determine which pages to skip during printing. |