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:org.encuestame.business.search.IndexerFile.java

License:Apache License

/**
 * Parse pdf Document.//from  w  w  w.j a v  a 2 s  .  c om
 * @param file
 * @return
 * @throws IOException
 */
public static PDDocument parsePdfDocument(final File file) throws IOException {
    InputStream is = new FileInputStream(file);
    COSDocument cosDoc = null;
    PDDocument pdDoc = null;
    try {
        cosDoc = SearchUtils.parseDocument(is);
        pdDoc = new PDDocument(cosDoc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.error(e);
    } finally {
        if (pdDoc == null) {
            log.error("PdDocument is null");
        } else {
            pdDoc.close();
        }
    }

    return pdDoc;
}

From source file:org.encuestame.business.search.SearchUtils.java

License:Apache License

/**
 * Create PDF Document.// ww w .j a  va2  s  .  c o  m
 * @param file {@link File}
 * @param Long attachmentId.
 * @return {@link Document}
 * @throws Exception
 */
public static Document createPdfDocument(final File file) throws Exception {
    InputStream is = new FileInputStream(file);
    COSDocument cosDoc = null;
    String docText = "";
    PDDocument pdDoc = null;
    try {
        cosDoc = parseDocument(is);
        pdDoc = new PDDocument(cosDoc);
        PDFTextStripper stripper = new PDFTextStripper();
        docText = stripper.getText(pdDoc);
        log.debug("PDF Doc Text " + docText.length());
    } finally {
        if (pdDoc == null) {
            log.error("PdDocument is null");
        } else {
            pdDoc.close();
        }
    }
    final Document doc = SearchUtils.addFields(file, docText);
    return doc;
}

From source file:org.esteco.jira.pdf.AddImageToPDF.java

License:Apache License

/**
 * Add an image to an existing PDF document.
 *
 * @param inputFile  The input PDF to add the image to.
 * @param imagePath  The filename of the image to put in the PDF.
 * @param outputFile The file to write to the pdf to.
 * @throws IOException If there is an error writing the data.
 *///  w  ww.  j a v a2 s  .c  o m
public void createPDFFromImage(String inputFile, String imagePath, String outputFile) throws IOException {
    // the document
    PDDocument doc = null;
    try {
        doc = PDDocument.load(new File(inputFile));

        //we will add the image to the first page.
        PDPage page = doc.getPage(0);
        //page.setRotation(90);

        // createFromFile is the easiest way with an image file
        // if you already have the image in a BufferedImage,
        // call LosslessFactory.createFromImage() instead
        PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);

        // contentStream.drawImage(ximage, 20, 20 );
        // better method inspired by http://stackoverflow.com/a/22318681/535646
        // reduce this value if the image is too large
        float scale = 0.4f;
        contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth() * scale, pdImage.getHeight() * scale);

        contentStream.close();
        doc.save(outputFile);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:org.esteco.jira.pdf.UsingTextMatrix.java

License:Apache License

/**
 * creates a sample document with some text using a text matrix.
 *
 * @param message The message to write in the file.
 * @param outfile The resulting PDF.//w w  w.jav a  2s  .com
 * @throws IOException If there is an error writing the data.
 */
public void doIt(String message, String outfile) throws IOException {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        // Page 1
        PDFont font = PDType1Font.HELVETICA;
        PDPage page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        float fontSize = 12.0f;

        PDRectangle pageSize = page.getMediaBox();
        float centeredXPosition = (pageSize.getWidth() - fontSize / 1000f) / 2f;
        float stringWidth = font.getStringWidth(message);
        float centeredYPosition = (pageSize.getHeight() - (stringWidth * fontSize) / 1000f) / 3f;

        PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();
        // counterclockwise rotation
        for (int i = 0; i < 8; i++) {
            contentStream.setTextMatrix(Matrix.getRotateInstance(i * Math.PI * 0.25, centeredXPosition,
                    pageSize.getHeight() - centeredYPosition));
            contentStream.showText(message + " " + i);
        }
        // clockwise rotation
        for (int i = 0; i < 8; i++) {
            contentStream.setTextMatrix(
                    Matrix.getRotateInstance(-i * Math.PI * 0.25, centeredXPosition, centeredYPosition));
            contentStream.showText(message + " " + i);
        }

        contentStream.endText();
        contentStream.close();

        // Page 2
        page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        fontSize = 1.0f;

        contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();

        // text scaling and translation
        for (int i = 0; i < 10; i++) {
            contentStream.setTextMatrix(new Matrix(12 + (i * 6), 0, 0, 12 + (i * 6), 100, 100 + i * 50));
            contentStream.showText(message + " " + i);
        }
        contentStream.endText();
        contentStream.close();

        // Page 3
        page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        fontSize = 1.0f;

        contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();

        int i = 0;
        // text scaling combined with rotation
        contentStream.setTextMatrix(new Matrix(12, 0, 0, 12, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(0, 18, -18, 0, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(-24, 0, 0, -24, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(0, -30, 30, 0, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.endText();
        contentStream.close();

        doc.save(outfile);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:org.exoplatform.services.document.impl.PDFDocumentReader.java

License:Open Source License

/**
 * Returns only a text from pdf file content.
 * //from   w  ww . j a  v  a 2  s  .  c o  m
 * @param is an input stream with .pdf file content.
 * @return The string only with text from file content.
 */
public String getContentAsText(final InputStream is) throws IOException, DocumentReadException {

    try {
        return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<String>() {
            public String run() throws Exception {
                if (is == null) {
                    throw new IllegalArgumentException("InputStream is null.");
                }
                PDDocument pdDocument = null;
                StringWriter sw = new StringWriter();
                try {
                    if (is.available() == 0)
                        return "";

                    try {
                        pdDocument = PDDocument.load(is);
                    } catch (IOException e) {
                        throw new DocumentReadException("Can not load PDF document.", e);
                    }

                    PDFTextStripper stripper = new PDFTextStripper();
                    stripper.setStartPage(1);
                    stripper.setEndPage(Integer.MAX_VALUE);
                    stripper.writeText(pdDocument, sw);
                } finally {
                    if (pdDocument != null)
                        try {
                            pdDocument.close();
                        } catch (IOException e) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("An exception occurred: " + e.getMessage());
                            }
                        }
                    if (is != null)
                        try {
                            is.close();
                        } catch (IOException e) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("An exception occurred: " + e.getMessage());
                            }
                        }
                }
                return sw.toString();
            }
        });

    } catch (PrivilegedActionException pae) {
        Throwable cause = pae.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else {
            throw new RuntimeException(cause);
        }
    }

}

From source file:org.exoplatform.services.document.impl.PDFDocumentReader.java

License:Open Source License

public Properties getProperties(final InputStream is) throws IOException, DocumentReadException {
    try {//from  w w w  .  j a va2  s  .  c o  m
        return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Properties>() {
            public Properties run() throws Exception {
                if (is == null) {
                    throw new IllegalArgumentException("InputStream is null.");
                }

                PDDocument pdDocument = PDDocument.load(is);
                Properties props = new Properties();
                try {
                    if (pdDocument.isEncrypted()) {
                        try {
                            pdDocument.decrypt("");
                        } catch (InvalidPasswordException e) {
                            throw new DocumentReadException("The pdf document is encrypted.", e);
                        } catch (org.apache.pdfbox.exceptions.CryptographyException e) {
                            throw new DocumentReadException(e.getMessage(), e);
                        }
                    }

                    PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
                    PDMetadata meta = catalog.getMetadata();
                    if (meta != null) {
                        XMPMetadata metadata = meta.exportXMPMetadata();

                        XMPSchemaDublinCore dc = metadata.getDublinCoreSchema();
                        if (dc != null) {
                            try {
                                if (dc.getTitle() != null)
                                    props.put(DCMetaData.TITLE, fixEncoding(dc.getTitle()));
                            } catch (Exception e) {
                                LOG.warn("getTitle failed: " + e.getMessage());
                            }
                            try {
                                if (dc.getDescription() != null)
                                    props.put(DCMetaData.DESCRIPTION, fixEncoding(dc.getDescription()));
                            } catch (Exception e) {
                                LOG.warn("getSubject failed: " + e.getMessage());
                            }

                            try {
                                if (dc.getCreators() != null) {
                                    for (String creator : dc.getCreators()) {
                                        props.put(DCMetaData.CREATOR, fixEncoding(creator));
                                    }
                                }
                            } catch (Exception e) {
                                LOG.warn("getCreator failed: " + e.getMessage());
                            }

                            try {
                                if (dc.getDates() != null) {
                                    for (Calendar date : dc.getDates()) {
                                        props.put(DCMetaData.DATE, date);
                                    }
                                }
                            } catch (Exception e) {
                                LOG.warn("getDate failed: " + e.getMessage());
                            }
                        }

                        XMPSchemaPDF pdf = metadata.getPDFSchema();
                        if (pdf != null) {
                            try {
                                if (pdf.getKeywords() != null)
                                    props.put(DCMetaData.SUBJECT, fixEncoding(pdf.getKeywords()));
                            } catch (Exception e) {
                                LOG.warn("getKeywords failed: " + e.getMessage());
                            }

                            try {
                                if (pdf.getProducer() != null)
                                    props.put(DCMetaData.PUBLISHER, fixEncoding(pdf.getProducer()));
                            } catch (Exception e) {
                                LOG.warn("getProducer failed: " + e.getMessage());
                            }
                        }

                        XMPSchemaBasic basic = metadata.getBasicSchema();
                        if (basic != null) {
                            try {
                                if (basic.getCreateDate() != null)
                                    props.put(DCMetaData.DATE, basic.getCreateDate());
                            } catch (Exception e) {
                                LOG.warn("getCreationDate failed: " + e.getMessage());
                            }
                            try {
                                if (basic.getModifyDate() != null)
                                    props.put(DCMetaData.DATE, basic.getModifyDate());
                            } catch (Exception e) {
                                LOG.warn("getModificationDate failed: " + e.getMessage());
                            }

                            // DCMetaData.PUBLISHER - basic.getCreatorTool()
                        }
                    }

                    if (props.isEmpty()) {
                        // The pdf doesn't contain any metadata, try to use the document
                        // information instead
                        PDDocumentInformation docInfo = pdDocument.getDocumentInformation();

                        if (docInfo != null) {
                            try {
                                if (docInfo.getAuthor() != null)
                                    props.put(DCMetaData.CONTRIBUTOR, docInfo.getAuthor());
                            } catch (Exception e) {
                                LOG.warn("getAuthor failed: " + e.getMessage());
                            }
                            try {
                                if (docInfo.getCreationDate() != null)
                                    props.put(DCMetaData.DATE, docInfo.getCreationDate());
                            } catch (Exception e) {
                                LOG.warn("getCreationDate failed: " + e.getMessage());
                            }
                            try {
                                if (docInfo.getCreator() != null)
                                    props.put(DCMetaData.CREATOR, docInfo.getCreator());
                            } catch (Exception e) {
                                LOG.warn("getCreator failed: " + e.getMessage());
                            }
                            try {

                                if (docInfo.getKeywords() != null)
                                    props.put(DCMetaData.SUBJECT, docInfo.getKeywords());
                            } catch (Exception e) {
                                LOG.warn("getKeywords failed: " + e.getMessage());
                            }
                            try {
                                if (docInfo.getModificationDate() != null)
                                    props.put(DCMetaData.DATE, docInfo.getModificationDate());
                            } catch (Exception e) {
                                LOG.warn("getModificationDate failed: " + e.getMessage());
                            }
                            try {
                                if (docInfo.getProducer() != null)
                                    props.put(DCMetaData.PUBLISHER, docInfo.getProducer());
                            } catch (Exception e) {
                                LOG.warn("getProducer failed: " + e.getMessage());
                            }
                            try {
                                if (docInfo.getSubject() != null)
                                    props.put(DCMetaData.DESCRIPTION, docInfo.getSubject());
                            } catch (Exception e) {
                                LOG.warn("getSubject failed: " + e.getMessage());
                            }
                            try {
                                if (docInfo.getTitle() != null)
                                    props.put(DCMetaData.TITLE, docInfo.getTitle());
                            } catch (Exception e) {
                                LOG.warn("getTitle failed: " + e.getMessage());
                            }

                            // docInfo.getTrapped();
                        }
                    }
                } finally {
                    if (pdDocument != null) {
                        pdDocument.close();
                    }

                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("An exception occurred: " + e.getMessage());
                            }
                        }
                    }
                }
                return props;
            }
        });

    } catch (PrivilegedActionException pae) {
        Throwable cause = pae.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else {
            throw new RuntimeException(cause);
        }
    }
}

From source file:org.fit.pdfdom.PDFToHTML.java

License:Open Source License

public static void main(String[] args) {

    if (args.length < 1) {
        System.out.println("Usage: PDFToHTML <infile> [<outfile>]");
        System.exit(1);/* w  ww  . j  a va2  s  . co m*/
    }

    String infile = args[0];
    String outfile;
    if (args.length > 1)
        outfile = args[1];
    else {
        String base = args[0];
        if (base.toLowerCase().endsWith(".pdf"))
            base = base.substring(0, base.length() - 4);
        outfile = base + ".html";
    }

    PDDocument document = null;
    try {
        document = PDDocument.load(new File(infile));
        PDFDomTree parser = new PDFDomTree();
        //parser.setDisableImageData(true);
        Writer output = new PrintWriter(outfile, "utf-8");
        parser.writeText(document, output);
        output.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
                System.err.println("Error: " + e.getMessage());
                //e.printStackTrace();
            }
        }
    }

}

From source file:org.frameworkset.http.converter.wordpdf.Word2PDFResponse.java

License:Apache License

protected void render_(HttpOutputMessage outputMessage, HttpInputMessage inputMessage, File file) {
    OutputStream out = null;/*  ww  w .ja v  a  2 s .  com*/
    PDDocument doc2 = null;
    try {
        File contract_pdf = file;
        doc2 = PDDocument.load(contract_pdf);
        HttpServletResponse response = outputMessage.getResponse();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition",
                "inline; filename=" + handleCNName(contract_pdf.getName(), inputMessage.getServletRequest()));
        out = response.getOutputStream();
        doc2.save(out);
        out.flush();
    } catch (Exception e) {
        throw new HttpMessageNotWritableException(this.getPdfFile(), e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (doc2 != null)
            try {
                doc2.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}

From source file:org.freeeed.ocr.ImageTextParser.java

License:Apache License

private static String splitPages(String filePath) throws IOException {
    File file = new File(filePath);
    String pagePath;//from w  ww .j  ava2  s  .c  om
    try (PDDocument document = PDDocument.load(file)) {
        Splitter splitter = new Splitter();
        List<PDDocument> pages = splitter.split(document);
        Iterator<PDDocument> iterator = pages.listIterator();
        int i = 0;
        pagePath = createTempPath(file);
        LOGGER.debug("pagePath = " + pagePath);
        while (iterator.hasNext()) {
            PDDocument pd = iterator.next();
            pd.save(pagePath + i++ + ".pdf");
            pd.close();
        }
    }
    return pagePath;
}

From source file:org.geomajas.plugin.printing.document.DefaultDocumentTest.java

License:Open Source License

@Test
public void testToImage() throws Exception {
    testRender();/*from   www .j a  va  2s  . c  o  m*/
    PDDocument pdf = PDDocument.load(new File("target/test.pdf"), true);
    PDFRenderer renderer = new PDFRenderer(pdf);
    BufferedImage bufferedImage = renderer.renderImageWithDPI(0, 144);
    pdf.close();
    ImageIO.write(bufferedImage, "PNG", new File("target/test.png"));
}