Example usage for org.apache.pdfbox.printing PDFPrintable PDFPrintable

List of usage examples for org.apache.pdfbox.printing PDFPrintable PDFPrintable

Introduction

In this page you can find the example usage for org.apache.pdfbox.printing PDFPrintable PDFPrintable.

Prototype

public PDFPrintable(PDDocument document) 

Source Link

Document

Creates a new PDFPrintable.

Usage

From source file:com.coast.PDFPrinter.java

License:Apache License

/**
 * Prints using a custom page size and custom margins.
 *///from   w ww  .j  av  a2  s . c  o  m
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();
    paper.setSize(306, 396); // 1/72 inch
    paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins

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

From source file:firmaapp.MainFormController.java

public void printPDFDocument(File file) throws Exception {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    Paper paper = new Paper();
    //paper.setSize(8.5 * 72, 11 * 72);
    double margin = 20;
    paper.setImageableArea(0, 0, paper.getWidth() - margin, paper.getHeight() - margin);
    pf.setPaper(paper);/*from  w w  w  .j a v  a2  s. c  om*/
    pf.setOrientation(PageFormat.LANDSCAPE);
    // PDFBox
    PDDocument doc = PDDocument.load(file);
    Book book = new Book();
    book.append(new PDFPrintable(doc), pf);
    //job.setPageable(new PDFPageable(doc));
    job.setPageable(book);
    //Book b = new Book();

    //job.setJobName("Job");
    if (job.printDialog()) {
        job.print();
    }

    doc.close();
}