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) | |
getPagesRemaining() | Gets the number of pages remaining in the currently active print job. |
getPrintable(int pageIndex) | |
print(Graphics graphics, PageFormat pageFormat, int pageIndex) | |
setColorMode(int value) | Sets how non-colored pages are printed if the device supports color 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
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
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. |