use of org.apache.pdfbox.printing.PDFPageable in project pdfbox by apache.
the class PrintPDF method main.
/**
* Infamous main method.
*
* @param args Command line arguments, should be one and a reference to a file.
* @throws PrinterException if the specified service cannot support the Pageable and Printable interfaces.
* @throws IOException if there is an error parsing the file.
*/
public static void main(String[] args) throws PrinterException, IOException {
try {
// force KCMS (faster than LCMS) if available
Class.forName("sun.java2d.cmm.kcms.KcmsServiceProvider");
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
} catch (ClassNotFoundException e) {
LOG.debug("KCMS service not found - using LCMS", e);
}
// suppress the Dock icon on OS X
System.setProperty("apple.awt.UIElement", "true");
String password = "";
String pdfFile = null;
boolean silentPrint = false;
String printerName = null;
Orientation orientation = Orientation.AUTO;
boolean showPageBorder = false;
int dpi = 0;
Map<String, Orientation> orientationMap = new HashMap<>();
orientationMap.put("auto", Orientation.AUTO);
orientationMap.put("landscape", Orientation.LANDSCAPE);
orientationMap.put("portrait", Orientation.PORTRAIT);
for (int i = 0; i < args.length; i++) {
switch(args[i]) {
case PASSWORD:
i++;
if (i >= args.length) {
usage();
}
password = args[i];
break;
case PRINTER_NAME:
i++;
if (i >= args.length) {
usage();
}
printerName = args[i];
break;
case SILENT:
silentPrint = true;
break;
case ORIENTATION:
i++;
if (i >= args.length) {
usage();
}
orientation = orientationMap.get(args[i]);
if (orientation == null) {
usage();
}
break;
case BORDER:
showPageBorder = true;
break;
case DPI:
i++;
if (i >= args.length) {
usage();
}
dpi = Integer.parseInt(args[i]);
break;
default:
pdfFile = args[i];
break;
}
}
if (pdfFile == null) {
usage();
}
try (PDDocument document = PDDocument.load(new File(pdfFile), password)) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(new File(pdfFile).getName());
if (printerName != null) {
PrintService[] printService = PrinterJob.lookupPrintServices();
boolean printerFound = false;
for (int i = 0; !printerFound && i < printService.length; i++) {
if (printService[i].getName().contains(printerName)) {
printJob.setPrintService(printService[i]);
printerFound = true;
}
}
}
printJob.setPageable(new PDFPageable(document, orientation, showPageBorder, dpi));
if (silentPrint || printJob.printDialog()) {
printJob.print();
}
}
}
use of org.apache.pdfbox.printing.PDFPageable in project pdfbox by apache.
the class Printing method printWithDialogAndAttributes.
/**
* Prints with a print preview dialog and custom PrintRequestAttribute values.
*/
private static void printWithDialogAndAttributes(PDDocument document) throws IOException, PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
// pages 1 to 1
attr.add(new PageRanges(1, 1));
PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();
if (vp != null && vp.getDuplex() != null) {
String dp = vp.getDuplex();
if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp)) {
attr.add(Sides.TWO_SIDED_LONG_EDGE);
} else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp)) {
attr.add(Sides.TWO_SIDED_SHORT_EDGE);
} else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp)) {
attr.add(Sides.ONE_SIDED);
}
}
if (job.printDialog(attr)) {
job.print(attr);
}
}
use of org.apache.pdfbox.printing.PDFPageable in project pdfbox by apache.
the class Printing method printWithPaper.
/**
* Prints using a custom page size and custom margins.
*/
private static void printWithPaper(PDDocument document) throws IOException, PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
// define custom paper
Paper paper = new Paper();
// 1/72 inch
paper.setSize(306, 396);
// no margins
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
// custom page format
PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(paper);
// override the page format
Book book = new Book();
// append all pages
book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages());
job.setPageable(book);
job.print();
}
use of org.apache.pdfbox.printing.PDFPageable in project pdfbox by apache.
the class Printing method printWithAttributes.
/**
* Prints using custom PrintRequestAttribute values.
*/
private static void printWithAttributes(PDDocument document) throws IOException, PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
// pages 1 to 1
attr.add(new PageRanges(1, 1));
job.print(attr);
}
use of org.apache.pdfbox.printing.PDFPageable in project BoofCV by lessthanoptimal.
the class CreateCalibrationTargetGenerator method close.
private void close() throws IOException {
pcs.close();
try {
if (sendToPrinter) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
if (job.printDialog()) {
job.print();
}
} else {
document.save(documentName);
}
} catch (PrinterException e) {
throw new IOException(e);
} finally {
document.close();
}
}
Aggregations