Search in sources :

Example 1 with PDFPageable

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();
        }
    }
}
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)

Example 2 with PDFPageable

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);
    }
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PageRanges(javax.print.attribute.standard.PageRanges) PDViewerPreferences(org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 3 with PDFPageable

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();
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PDFPrintable(org.apache.pdfbox.printing.PDFPrintable) PageFormat(java.awt.print.PageFormat) Book(java.awt.print.Book) Paper(java.awt.print.Paper) PrinterJob(java.awt.print.PrinterJob)

Example 4 with PDFPageable

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);
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PageRanges(javax.print.attribute.standard.PageRanges) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 5 with PDFPageable

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();
    }
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PrinterException(java.awt.print.PrinterException) IOException(java.io.IOException) PrinterJob(java.awt.print.PrinterJob)

Aggregations

PrinterJob (java.awt.print.PrinterJob)9 PDFPageable (org.apache.pdfbox.printing.PDFPageable)9 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)3 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)3 PrinterException (java.awt.print.PrinterException)2 PageRanges (javax.print.attribute.standard.PageRanges)2 PDViewerPreferences (org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences)2 Book (java.awt.print.Book)1 PageFormat (java.awt.print.PageFormat)1 Paper (java.awt.print.Paper)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 PrintService (javax.print.PrintService)1 COSString (org.apache.pdfbox.cos.COSString)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 Orientation (org.apache.pdfbox.printing.Orientation)1 PDFPrintable (org.apache.pdfbox.printing.PDFPrintable)1