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

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

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

This will close the underlying COSDocument object.

Usage

From source file:helper.ThumbnailGenerator.java

License:Open Source License

private static File generateThumbnailFromPdf(InputStream in, int size, String name) {
    PDDocument document = null;
    try {//  w  ww. j a v a 2  s . c o  m
        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 a  2  s. co m*/
    document.close();
}

From source file:in.co.itasca.hms.api.CarODataSingleProcessor.java

License:Apache License

@Override
public ODataResponse readEntityMedia(GetMediaResourceUriInfo uriInfo, String contentType)
        throws ODataException {
    //        if(entityName.equalsIgnoreCase(ENTITYSET_REGISTRATION)){

    if (uriInfo.getNavigationSegments().size() == 0) {
        EdmEntitySet entitySet = uriInfo.getStartEntitySet();
        int id = getKeyValue(uriInfo.getKeyPredicates().get(0));
        try {// w  w  w  . ja  va  2 s .c o  m

            // Create a document and add a page to it
            PDDocument document;
            IService service = ServiceFactory.getInstance(null).getService(ENTITYSET_REGISTRATION);
            RegistrationService reg = (RegistrationService) service;
            // Save the results and ensure that the document is properly closed:
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            document = reg.getPDF(id);

            document.save("10.pdf");
            document.save(out);

            document.close();

            String mimeType = "image/pdf";
            return ODataResponse.fromResponse(EntityProvider.writeBinary(mimeType, out.toByteArray())).build();
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
    return super.readEntityMedia(uriInfo, contentType);
}

From source file:indexer.Indexer.java

public static String getPDF(String fileLoc, int pageNumber) {
    PDDocument pdf = null;
    String parsedText = null;/*from   w  ww . jav  a 2s  .  c o m*/
    COSDocument cosDoc = null;
    //BufferedWriter br = null;
    try {
        File inputPDF = new File(fileLoc);
        PDFParser parser = new PDFParser(new FileInputStream(inputPDF));
        parser.parse();
        cosDoc = parser.getDocument();
        pdf = new PDDocument(cosDoc);
        PDFTextStripper stripper = new PDFTextStripper();
        stripper.setStartPage(pageNumber);
        stripper.setEndPage(pageNumber);
        //br = new BufferedWriter( new OutputStreamWriter(null));
        //stripper.writeText(pdf, br);
        parsedText = stripper.getText(pdf);
        pdf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return parsedText;
}

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();/*from w w  w . j av  a 2  s  .com*/
    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();
}

From source file:indexer.PDFTextExtractor.java

License:Open Source License

/**
 * Given a PDF file, gets all the text from it
 *
 * @param filename The filename to get all the text from
 * @return The fulltext of the file//  www. j  a  v a  2s .c  o m
 * @throws IOException
 */
public String extractText(String filename) throws IOException {
    PDDocument document = getPDDocument(filename);
    String res = stripper.getText(document);
    document.close();
    return res;
}

From source file:indexer.PDFTextExtractor.java

License:Open Source License

public void extractTextToWriter(String filename, Writer out) throws IOException {
    PDDocument document = getPDDocument(filename);
    stripper.writeText(document, out);/*from  ww w.java 2  s .  c o  m*/
    document.close();
}

From source file:insight.masters.policyanalytics.services.BranchingOriginStanfordKeywords.java

public static String readfrompdf(String datsetspath, String Document) {
    /**//from  ww  w  .  j a v  a2 s  .  c  om
     * 1 POlicy text Policy Aspects
     */

    PDFParser parser = null;
    PDDocument pdDoc = null;
    COSDocument cosDoc = null;
    PDFTextStripper pdfStripper;

    String parsedText;
    String policytext = null;
    File file = new File(datsetspath + Document);
    try {
        parser = new PDFParser(new FileInputStream(file));
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);
        parsedText = pdfStripper.getText(pdDoc);
        parsedText.replaceAll("[^A-Za-z0-9. ]+", "");
        policytext = parsedText;
        // System.out.println(policytext);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            if (cosDoc != null)
                cosDoc.close();
            if (pdDoc != null)
                pdDoc.close();
        } catch (Exception e1) {
            e.printStackTrace();
        }

    }
    return policytext;

}

From source file:io.konik.carriage.pdfbox.PDFBoxInvoiceAppender.java

License:Open Source License

@Override
public void append(AppendParameter appendParameter) {
    InputStream inputPdf = appendParameter.inputPdf();
    try {/*ww w  .j  av a 2  s.  c o m*/
        PDDocument doc = PDDocument.load(inputPdf);
        setMetadata(doc, appendParameter);
        attachZugferdFile(doc, appendParameter.attachmentFile());
        doc.getDocument().setVersion(1.7f);
        doc.save(appendParameter.resultingPdf());
        doc.close();
    } catch (Exception e) {
        throw new InvoiceAppendError("Error appending Invoice", e);
    }

}

From source file:IO.search.SearchWordFile.java

private void search(File scrFile, String word) {
    String[] arrStr = null;// w w w .  jav  a 2  s. c  o m
    String[] arrStrA = null;
    if (word.contains(" ")) {
        arrStr = word.split(" ");
    } else if (word.contains("-")) {
        arrStrA = word.split("-");
        System.out.println("reach");
    }

    boolean is03word = scrFile.getName().matches("^.+\\.(?i)(doc)$");
    if (is03word) {
        try {
            InputStream is = new FileInputStream(scrFile);
            WordExtractor ex = new WordExtractor(is);
            String text2003 = ex.getText();
            if (arrStr != null && arrStr.length > 0) {
                for (int i = 0; i < arrStr.length; i++) {
                    if (text2003.toLowerCase().contains(arrStr[i].toLowerCase())) {
                        nameList.add(scrFile.getPath());
                        return;
                    }
                }
            } else if (arrStrA != null && arrStrA.length > 0) {
                int count = 0;
                for (int i = 0; i < arrStrA.length; i++) {
                    if (text2003.toLowerCase().contains(arrStrA[i].toLowerCase())) {
                        count++;
                    }
                }
                if (count == arrStrA.length) {
                    nameList.add(scrFile.getPath());
                }
            } else if (text2003.toLowerCase().contains(word.toLowerCase())) {
                System.out.println("true");
                nameList.add(scrFile.getPath());
            }
        } catch (Exception ex) {
            Logger.getLogger(SearchWordFile.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else if (scrFile.getName().matches("^.+\\.(?i)(docx)$")) {
        try {
            OPCPackage opcPackage = POIXMLDocument.openPackage(scrFile.getPath());
            POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
            String text2007 = extractor.getText();
            if (arrStr != null && arrStr.length > 0) {
                for (int i = 0; i < arrStr.length; i++) {
                    if (text2007.toLowerCase().contains(arrStr[i].toLowerCase())) {
                        nameList.add(scrFile.getPath());
                        return;
                    }
                }
            } else if (arrStrA != null && arrStrA.length > 0) {
                int count = 0;
                for (int i = 0; i < arrStrA.length; i++) {
                    if (text2007.toLowerCase().contains(arrStrA[i].toLowerCase())) {
                        count++;
                    }
                }
                if (count == arrStrA.length) {
                    nameList.add(scrFile.getPath());
                }
            } else if (text2007.toLowerCase().contains(word.toLowerCase())) {
                System.out.println("true");
                nameList.add(scrFile.getPath());
            }
        } catch (Exception ex) {
            Logger.getLogger(SearchWordFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (scrFile.getName().matches("^.+\\.(?i)(pdf)$")) {

        FileInputStream input = null;
        PDDocument pdfDocument = null;
        try {

            input = new FileInputStream(scrFile);
            PDFParser pdfParser = new PDFParser(input);
            pdfParser.parse();
            pdfDocument = pdfParser.getPDDocument();
            PDFTextStripper stripper = new PDFTextStripper();
            String content = stripper.getText(pdfDocument);
            if (arrStr != null && arrStr.length > 0) {
                for (int i = 0; i < arrStr.length; i++) {
                    if (content.toLowerCase().contains(arrStr[i].toLowerCase())) {
                        nameList.add(scrFile.getPath());
                        return;
                    }
                }
            } else if (arrStrA != null && arrStrA.length > 0) {
                int count = 0;
                for (int i = 0; i < arrStrA.length; i++) {
                    if (content.toLowerCase().contains(arrStrA[i].toLowerCase())) {
                        count++;
                    }
                }
                if (count == arrStrA.length) {
                    nameList.add(scrFile.getPath());
                }
            } else if (content.toLowerCase().contains(word.toLowerCase())) {
                System.out.println("true");
                nameList.add(scrFile.getPath());

            }

        } catch (Exception ex) {
            Logger.getLogger(SearchWordFile.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                input.close();
                pdfDocument.close();
            } catch (IOException ex) {
                Logger.getLogger(SearchWordFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } else if (scrFile.getName().matches("^.+\\.(?i)(txt)$")) {
        BufferedReader in = null;
        try {
            in = new BufferedReader((new InputStreamReader(new FileInputStream(scrFile),
                    getCharset(scrFile.getAbsolutePath()))));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                if (arrStr != null && arrStr.length > 0) {
                    for (int i = 0; i < arrStr.length; i++) {
                        if (line.toLowerCase().contains(arrStr[i].toLowerCase())) {
                            nameList.add(scrFile.getPath());
                            return;
                        }
                    }
                } else if (arrStrA != null && arrStrA.length > 0) {
                    int count = 0;
                    for (int i = 0; i < arrStrA.length; i++) {
                        if (line.contains(arrStrA[i])) {
                            count++;
                        }
                    }
                    if (count == arrStrA.length) {
                        nameList.add(scrFile.getPath());
                    }
                } else if (line.toLowerCase().contains(word.toLowerCase())) {
                    System.out.println("true");
                    nameList.add(scrFile.getPath());
                    return;
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(SearchWordFile.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(SearchWordFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}