Example usage for org.apache.pdfbox.pdmodel PDDocument load

List of usage examples for org.apache.pdfbox.pdmodel PDDocument load

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument load.

Prototype

public static PDDocument load(byte[] input) throws IOException 

Source Link

Document

Parses a PDF.

Usage

From source file:fi.riista.feature.permit.invoice.pdf.PermitHarvestInvoicePdfBuilder.java

static byte[] getPdf(final PermitHarvestInvoicePdfModel model) throws IOException {
    try (final InputStream is = PDF_TEMPLATE.getInputStream();
            final PDDocument pdfTemplate = PDDocument.load(is)) {

        return new PermitHarvestInvoicePdfBuilder(pdfTemplate, model).build();
    }//w  ww  .ja  v  a2 s  .  c  om
}

From source file:FileIOAux.PrintAux.java

public PrintAux(File file, Langs.Locale lingua, javax.swing.JDialog dialogo) {
    super();//from   w  w  w  .ja  v a 2 s .c  o m
    this.lingua = lingua;
    this.dialogo = dialogo;
    impressao = PrinterJob.getPrinterJob();
    impressoras = PrinterJob.lookupPrintServices();
    parametros = new HashPrintRequestAttributeSet();
    valorqualidade = 4;
    valorlados = 0;
    valorminimo = 1;
    valormaximo = 1;
    valorcores = 1;
    ncopias = 1;
    this.file = file;
    FileInputStream fis;
    try {
        doc = PDDocument.load(file);
    } catch (IOException ex) {
        Logger.getLogger(PrintAux.class.getName()).log(Level.SEVERE, null, ex);
        doc = null;
    }

}

From source file:FileIOAux.PrintAux.java

public synchronized void print() {
    int val = this.showDialog(dialogo);
    if (val > -1) {
        try {/* www.j  a va  2s.com*/
            try (PDDocument pdf = PDDocument.load(file)) {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPageable(new PDFPageable(pdf));
                job.setPrintService(impressoras[val]);
                switch (valorqualidade) {
                case 5:
                    parametros.add(PrintQuality.HIGH);
                    break;
                case 3:
                    parametros.add(PrintQuality.DRAFT);
                    break;
                default:
                    parametros.add(PrintQuality.NORMAL);
                    break;
                }
                switch (valorlados) {
                case 1:
                    parametros.add(Sides.DUPLEX);
                    break;
                case 2:
                    parametros.add(Sides.TUMBLE);
                    break;
                default:
                    parametros.add(Sides.ONE_SIDED);
                    break;
                }
                switch (valorcores) {
                case 0:
                    parametros.add(Chromaticity.MONOCHROME);
                    break;
                default:
                    parametros.add(Chromaticity.COLOR);
                    break;
                }
                parametros.add(new PageRanges(valorminimo, valormaximo));
                parametros.add(new Copies(ncopias));
                job.print(parametros);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | PrinterException ex) {
            Logger.getLogger(PrintAux.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        doc.close();
    } catch (IOException ex) {
        Logger.getLogger(PrintAux.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:FileIOAux.PrintAux.java

/**
 * @see//w  w  w .  j  a v a 2  s. com
 * http://stackoverflow.com/questions/23326562/apache-pdfbox-convert-pdf-to-images
 * @param fil
 * @return
 */
public static BufferedImage[] pdfToImage(String fil) {
    BufferedImage[] bim = null;
    try {
        PDDocument document = PDDocument.load(new File(fil));
        if (document != null) {
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            bim = new BufferedImage[document.getNumberOfPages()];
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                bim[i] = pdfRenderer.renderImage(i);
            }
            document.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(PrintAux.class.getName()).log(Level.SEVERE, null, ex);
    }
    return bim;
}

From source file:FileIOAux.PrintAux.java

/**
 * @see/*from   w w  w  .j  av  a2s . c o  m*/
 * http://stackoverflow.com/questions/3701644/how-can-i-get-the-total-number-of-pages-to-be-printed
 */
int getNumberOfPages(PageRanges pageRanges) {
    if (doc != null) {
        PDDocument doco;
        try {
            doco = PDDocument.load(file);
        } catch (IOException ex) {
            Logger.getLogger(PrintAux.class.getName()).log(Level.SEVERE, null, ex);
            doco = null;
        }
        if (doco != null) {
            int pages = 0;
            int[][] ranges = pageRanges.getMembers();
            for (int[] range : ranges) {
                pages += 1;
                if (range.length == 2) {
                    pages += range[1] - range[0];
                }
            }
            pages = Math.min(pages, doco.getNumberOfPages());
            return pages;
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}

From source file:fire.util.fileformats.pdf.PdfRecordReader.java

License:Apache License

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {

    if (!processed) {
        Path file = fileSplit.getPath();
        FileSystem fs = file.getFileSystem(conf);
        FSDataInputStream in = null;//from   www.  ja  va 2 s .c  o m
        PDDocument pdf = null;
        String parsedText = null;
        PDFTextStripper stripper;
        try {
            in = fs.open(file);
            pdf = PDDocument.load(in);
            stripper = new PDFTextStripper();
            parsedText = stripper.getText(pdf);
            this.key = new Text(file.getName());
            this.value = new Text(parsedText);
        } finally {
            IOUtils.closeStream(in);
        }
        processed = true;
        return true;
    }
    return false;
}

From source file:firmaapp.MainFormController.java

@FXML
void workOrderDateButtonAction(ActionEvent event) throws IOException {
    LocalDate date = workOrderDatePicker.getValue();
    ProductMySQL prodMySQL = new ProductMySQL();
    String numOfWO = "";
    numOfWO = prodMySQL.createWorkOrder(Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));

    WorkOrderPDF woPDF = new WorkOrderPDF(numOfWO,
            Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()), RootController.workOrderProducts,
            RootController.workOrderSupplies);
    woPDF.createWorkOrder();/*from w  w w  .  j a  va 2  s  .c o m*/

    DateFormat df = new SimpleDateFormat("yy"); // Just the year, with 2 digits
    String formatedDate = df.format(woPDF.getDateOfWorkOrder());

    File file = new File("pdf_docs" + File.separator + "work_orders" + File.separator + "work_order_"
            + woPDF.getNumOfWorkOrder().split("/")[0] + "_" + formatedDate + ".pdf");

    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    renderer.renderImageWithDPI(0, 300);
    //BufferedImage image = renderer.renderImage(0);
    BufferedImage image = renderer.renderImageWithDPI(0, 300);
    ImageIO.write(image, "PNG", new File("pdf_docs" + File.separator + "work_orders_picture" + File.separator
            + "work_order_" + woPDF.getNumOfWorkOrder().split("/")[0] + "_" + formatedDate + ".png"));
    doc.close();

    WorkOrderMySQL woMySQL = new WorkOrderMySQL();
    ArrayList<WorkOrder> orders = woMySQL.getAllWokrOrders();
    RootController.allWorkOrders = orders;
    Collections.sort(RootController.allWorkOrders, new WorkOrderCompare());
    dataWorkOrders.clear();
    for (WorkOrder w : RootController.allWorkOrders) {
        dataWorkOrders.add(w);
    }
}

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   ww w .j  a  va  2s  . co m*/
    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();
}

From source file:fixture.pdfboxeg.CreateSignature.java

License:Apache License

/**
 * Signs the given PDF file.//ww  w .  j a  v a  2s. com
 * @param inFile input PDF file
 * @param outFile output PDF file
 * @param tsaClient optional TSA client
 * @throws IOException if the input file could not be read
 */
public void signDetached(File inFile, File outFile, TSAClient tsaClient, boolean certify) throws IOException {
    if (inFile == null || !inFile.exists()) {
        throw new FileNotFoundException("Document for signing does not exist");
    }

    FileOutputStream fos = new FileOutputStream(outFile);

    // sign
    try (PDDocument doc = PDDocument.load(inFile)) {
        signDetached(doc, fos, tsaClient, certify);
    }
}

From source file:fr.acxio.tools.agia.file.pdf.AbstractPDDocumentFactory.java

License:Apache License

protected PDDocument loadDocument(InputStream sInputStream, Map<String, Object> sParameters)
        throws IOException, CryptographyException, InvalidPasswordException {
    PDDocument aDocument = PDDocument.load(sInputStream);
    if (aDocument.isEncrypted()) {
        aDocument.decrypt((String) sParameters.get(PDDocumentFactoryConstants.PARAM_PASSWORD));
    }/*w w w  .  j  a v  a  2s  . c  om*/
    return aDocument;
}