public class AsposeWordsPrintDocument
extends java.lang.Object
implements java.awt.print.Pageable, java.awt.print.Printable
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. | Constructor and Description |
|---|
AsposeWordsPrintDocument(Document document)
Initializes a new instance of this class.
|
| Modifier and Type | Method and Description |
|---|---|
int |
getColorMode()
Gets how non-colored pages are printed if the device supports color printing.
|
int |
getColorPagesPrinted()
Gets the number of pages printed in color (i.e.
|
int |
getNumberOfPages() |
java.awt.print.PageFormat |
getPageFormat(int pageIndex) |
int |
getPagesRemaining()
Gets the number of pages remaining in the currently active print job.
|
java.awt.print.Printable |
getPrintable(int pageIndex) |
int |
print(java.awt.Graphics graphics,
java.awt.print.PageFormat pageFormat,
int pageIndex) |
void |
setColorMode(int value)
Sets how non-colored pages are printed if the device supports color printing.
|
public AsposeWordsPrintDocument(Document document)
document - The document to print.public int print(java.awt.Graphics graphics,
java.awt.print.PageFormat pageFormat,
int pageIndex)
throws java.awt.print.PrinterException
print in interface java.awt.print.Printablejava.awt.print.PrinterExceptionpublic int getNumberOfPages()
getNumberOfPages in interface java.awt.print.Pageablepublic java.awt.print.PageFormat getPageFormat(int pageIndex)
throws java.lang.IndexOutOfBoundsException
getPageFormat in interface java.awt.print.Pageablejava.lang.IndexOutOfBoundsExceptionpublic java.awt.print.Printable getPrintable(int pageIndex)
throws java.lang.IndexOutOfBoundsException
getPrintable in interface java.awt.print.Pageablejava.lang.IndexOutOfBoundsExceptionpublic int getColorMode()
Remarks:
Doesn't affect booklet printing.
ColorPrintMode constants.public void setColorMode(int value)
Remarks:
Doesn't affect booklet printing.
value - How non-colored pages are printed if the device supports color printing. The value must be one of ColorPrintMode constants.public int getColorPagesPrinted()
PageSettings#getColor() / PageSettings#setColor(boolean) set to true).public int getPagesRemaining()
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. printDoc - The Aspose.Words print document to track.document - The Aspose.Words document to track printing for.java.lang.IllegalArgumentException - Thrown when printDoc is null.
/
public PrintTracker(AsposeWordsPrintDocument printDoc) {
if (printDoc == null) {
throw new IllegalArgumentException("printDoc cannot be null");
}
this.printDocument = printDoc;
this.totalPages = printDoc.getNumberOfPages();
}
/**
Alternative constructor that creates AsposeWordsPrintDocument from Documentjava.lang.IllegalArgumentException - Thrown when document is null.
/
public PrintTracker(Document document) {
if (document == null) {
throw new IllegalArgumentException("document cannot be null");
}
this.printDocument = new AsposeWordsPrintDocument(document);
this.totalPages = printDocument.getNumberOfPages();
}
/**
Gets the current page being printed (1-based index).
Returns -1 when no printing is in progress.PrinterException - If printing fails.
/
public void startPrinting() throws PrinterException {
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(this);
if (printerJob.printDialog()) {
handleBeginPrint();
printerJob.print();
handleEndPrint();
}
}
/**
Starts printing without showing the print dialog.PrinterException - If printing fails.
/
public void printSilently() throws PrinterException {
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(this);
handleBeginPrint();
printerJob.print();
handleEndPrint();
}PrinterException - If printing fails.
/
public void printWithDetailedTracking() throws PrinterException {
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Create a custom printable that tracks each page
printerJob.setPrintable((graphics, pageFormat, pageIndex) -> {
if (pageIndex == 0 && !isPrinting) {
handleBeginPrint();
}
if (pageIndex >= totalPages) {
if (isPrinting) {
handleEndPrint();
}
return Printable.NO_SUCH_PAGE;
}
printingPage = pageIndex + 1;
handlePrintPage();
int result = printDocument.print(graphics, pageFormat, pageIndex);
// Check if this was the last page
if (pageIndex == totalPages - 1) {
handleEndPrint();
}
return result;
});
if (printerJob.printDialog()) {
printerJob.print();
}
}
}
}