Search in sources :

Example 1 with Orientation

use of org.apache.pdfbox.printing.Orientation 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();
        }
    }
}
Also used : HashMap(java.util.HashMap) Orientation(org.apache.pdfbox.printing.Orientation) PrinterJob(java.awt.print.PrinterJob) PrintService(javax.print.PrintService) PDFPageable(org.apache.pdfbox.printing.PDFPageable) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) File(java.io.File)

Aggregations

PrinterJob (java.awt.print.PrinterJob)1 File (java.io.File)1 HashMap (java.util.HashMap)1 PrintService (javax.print.PrintService)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 Orientation (org.apache.pdfbox.printing.Orientation)1 PDFPageable (org.apache.pdfbox.printing.PDFPageable)1