printDialog()
printDialog(PrintRequestAttributeSet attributes)
PrintRequestAttributeSet
printDialog
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new HelloWorldPrinter());
HashPrintRequestAttributeSet printParams = new HashPrintRequestAttributeSet();
boolean ok = job.printDialog(printParams);
if (ok) {
PageRanges pageRanges = (PageRanges) printParams.get(PageRanges.class);
int pagesToBePrinted = getNumberOfPages(pageRanges);
try {
job.print(printParams);
} catch (PrinterException e) {
/* The job did not successfully complete */
}
}
printParams
print()
PageRanges
javadoc
int getNumberOfPages(PageRanges pageRanges) {
int pages = 0;
int[][] ranges = pageRanges.getMembers();
for (int i = 0; i < ranges.length; i++) {
pages += 1;
if (ranges[i].length == 2) {
pages += ranges[i][1] - ranges[i][0];
}
}
pages = Math.min(pages, totalPagesOfDocument);
return pages;
}