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:gov.nist.appvet.toolmgr.ToolServiceAdapter.java

License:Open Source License

public static String getPdfReportString(String reportPath, AppInfo appInfo) {
    File file = new File(reportPath);
    PDDocument pddDocument = null;//from  w  ww.  j av  a 2 s  . co  m
    PDFTextStripper textStripper = null;
    try {
        pddDocument = PDDocument.load(file);
        textStripper = new PDFTextStripper();
        textStripper.setStartPage(1);
        textStripper.setEndPage(1);
        final String report = textStripper.getText(pddDocument);
        return report;
    } catch (final IOException e) {
        appInfo.log.error(e.getMessage());
        return null;
    } finally {
        if (pddDocument != null) {
            try {
                pddDocument.close();
                pddDocument = null;
            } catch (IOException e) {
                appInfo.log.error(e.getMessage());
            }
        }
        textStripper = null;
        file = null;
    }
}

From source file:gui.dialog.PlainTextDialog.java

License:Apache License

/**
 *    @Method: pdfParser// w ww .  j  av a 2s .  c  o m
 * 
 *    input : File
 *  output: String
 *  
 *  Diese Methode liet den Text aus der Pdf Datei aus und gibt den Text als String zurck
 */
private String pdfParser(File pdfFile) {
    NDC.push("pdfParser");

    PDDocument document = null;
    try {
        document = PDDocument.load(pdfFile);
    } catch (IOException e) {
        logger.error("Could not load document", e);
        NDC.pop();
        return null;
    }

    if (document.isEncrypted()) {
        NDC.pop();
        return "Encrypted documents are not supported";
    }

    PDFTextStripper stripper;
    try {
        stripper = new PDFTextStripper();
    } catch (IOException e) {
        logger.error("Could not create stripper", e);
        NDC.pop();
        return null;
    }

    stripper.setStartPage(1);
    stripper.setEndPage(2);

    String text;
    try {
        text = stripper.getText(document);
    } catch (Exception e) {
        logger.error("Could not parse PDF", e);
        NDC.pop();
        return null;
    }
    NDC.pop();
    return text;
}

From source file:GUI.Helper.IOHelper.java

public static boolean getProjectImage(MainController mc) {
    FileChooser fc = new FileChooser();
    fc.setTitle("Select WZITS Project Image");
    fc.getExtensionFilters().addAll(/*from   w  ww .  j ava2  s.co m*/
            //new FileChooser.ExtensionFilter("All Images", "*.*"),
            new FileChooser.ExtensionFilter("JPG", "*.jpg"), new FileChooser.ExtensionFilter("PNG", "*.png"),
            new FileChooser.ExtensionFilter("PDF", "*.pdf"));
    File openFile = fc.showOpenDialog(MainController.getWindow()); //mc.getMainWindow()
    if (openFile != null) {
        if (fc.getSelectedExtensionFilter().getExtensions().get(0).equalsIgnoreCase("*.pdf")) {
            try {
                PDDocument doc = PDDocument.load(openFile);
                PDFRenderer pdfRenderer = new PDFRenderer(doc);
                BufferedImage image = pdfRenderer.renderImage(0);
                //ImageIOUtil.writeImage(image, "C:\\Users\\ltrask\\Documents\\test_image.png", 300);
                Image convertedImage = SwingFXUtils.toFXImage(image, null);
                mc.getProject().setProjPhoto(convertedImage);
                doc.close();
                return true;
            } catch (IOException e) {
                Alert al = new Alert(Alert.AlertType.ERROR);
                al.setTitle("WZITS Tool");
                al.setHeaderText("The selected PDF is password protected");
                al.showAndWait();
            }
        } else {
            try {
                mc.getProject().setProjPhoto(new Image(new FileInputStream(openFile)));
                return true;
            } catch (FileNotFoundException e) {

            }
        }
    }
    return false;
}

From source file:GUI.Helper.IOHelper.java

public static Image openImage(MainController mc) {
    FileChooser fc = new FileChooser();
    fc.setTitle("Select WZITS Project Image");
    fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG", "*.jpg"),
            new FileChooser.ExtensionFilter("PNG", "*.png"), new FileChooser.ExtensionFilter("PDF", "*.pdf"));
    File openFile = fc.showOpenDialog(mc.getWindow()); //mc.getMainWindow()
    if (openFile != null) {
        if (fc.getSelectedExtensionFilter().getExtensions().get(0).equalsIgnoreCase("*.pdf")) {
            try {
                PDDocument doc = PDDocument.load(openFile);
                PDFRenderer pdfRenderer = new PDFRenderer(doc);
                BufferedImage image = pdfRenderer.renderImage(0);
                Image convertedImage = SwingFXUtils.toFXImage(image, null);
                doc.close();/*from   ww w.  j av  a 2  s.  c o  m*/
                return convertedImage;
            } catch (IOException e) {
                Alert al = new Alert(Alert.AlertType.ERROR);
                al.setTitle("WZITS Tool");
                al.setHeaderText("The selected PDF is password protected");
                al.showAndWait();
            }
        } else {
            try {
                return new Image(new FileInputStream(openFile));
            } catch (FileNotFoundException e) {

            }
        }
    }
    return null;
}

From source file:helper.PdfText.java

License:Apache License

/**
 * @param pdfFile this file will be extracted.
 * @return the plain text of the pdf/*from   w  w w . j a  v  a  2 s. c  o m*/
 */
public String toString(InputStream pdfFile) {
    PDDocument doc = null;
    try {

        doc = PDDocument.load(pdfFile);
        PDFTextStripper stripper = new PDFTextStripper();
        String text = stripper.getText(doc);
        return text;
    } catch (IOException e) {
        throw new HttpArchiveException(500, e);
    } catch (Exception e) {
        throw new HttpArchiveException(500, e);
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException e) {
                logger.warn("", e);
            }
        }
    }
}

From source file:helper.ThumbnailGenerator.java

License:Open Source License

private static File generateThumbnailFromPdf(InputStream in, int size, String name) {
    PDDocument document = null;/*  w  w w .  j  a  v  a2  s  . c  om*/
    try {
        document = PDDocument.load(in);
        BufferedImage tmpImage = writeImageFirstPage(document, BufferedImage.TYPE_INT_RGB, size);
        return createFileFromImage(tmpImage, size, name);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:idp.pdf_converter.java

public static void pdf_converter(File file) throws IOException {
    PDDocument document = PDDocument.load(file);
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    String path = System.getProperty("user.dir") + "\\src\\main\\temp\\images\\";
    for (int page = 0; page < document.getNumberOfPages(); ++page) {
        BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.GRAY);
        ImageIOUtil.writeImage(bim, path + file.getName() + "-" + (page + 1) + ".png", 300);
    }//from w w  w . jav a2 s.  c  o m
    document.close();
}

From source file:impresionXML.impresion.pdf.JPDFManejo.java

public PDDocument getDocumento() throws Exception {
    if (document == null) {
        if (msPDFFile.indexOf("://") >= 0) {
            document = PDDocument.load(new URL(msPDFFile));
        } else {//from   www .j ava 2 s . c  o m
            document = PDDocument.load(msPDFFile);
        }
        if (document.isEncrypted()) {
            document.decrypt(getPassword());
        }
    }
    return document;
}

From source file:indexer.Indexer.java

public static int getPDFPages(String fileLoc) throws IOException {
    PDDocument doc = PDDocument.load(new File(fileLoc));
    int count = doc.getNumberOfPages();
    return count;
}

From source file:indexer.Indexer.java

public static void writePDF(String file_location, Map<String, Set> map) throws IOException {
    PDFMergerUtility finalDoc = new PDFMergerUtility();
    PDDocument document = PDDocument.load(file_location);
    PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);
    contentStream.beginText();/*www.j av  a  2  s . c  o m*/
    contentStream.setFont(PDType1Font.HELVETICA, 12);

    for (Map.Entry<String, Set> entry : map.entrySet()) {
        contentStream.drawString(entry.getKey() + "- " + entry.getValue().toString());
    }

    contentStream.endText();
    contentStream.close();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        document.save(out);
    } catch (COSVisitorException ex) {
        Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
    }
    finalDoc.addSource(new ByteArrayInputStream(out.toByteArray()));
    document.close();
}