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.geomajas.plugin.printing.document.SinglePageDocument.java

License:Open Source License

private void writeDocument(OutputStream outputStream, Format format, int dpi)
        throws IOException, DocumentException, PrintingException {
    if (format == Format.PDF) {
        baos.writeTo(outputStream);/*from   w w w  . j  a  v  a 2 s . c om*/
    } else {
        PDDocument pdf = PDDocument.load(new ByteArrayInputStream(baos.toByteArray()), true);
        PDFRenderer renderer = new PDFRenderer(pdf);
        BufferedImage bufferedImage = renderer.renderImageWithDPI(0, dpi);
        pdf.close();
        if (format == Format.PNG) {
            final String formatName = format.getExtension();
            for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
                ImageWriter writer1 = iw.next();
                ImageWriteParam writeParam = writer1.getDefaultWriteParam();
                ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier
                        .createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
                IIOMetadata metadata = writer1.getDefaultImageMetadata(typeSpecifier, writeParam);
                if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
                    continue;
                }

                setDPI(metadata);
                // Write bufferedImage to outputStream
                final ImageOutputStream stream = ImageIO.createImageOutputStream(outputStream);
                try {
                    writer1.setOutput(stream);
                    writer1.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
                } finally {
                    stream.flush();
                    stream.close();
                }
                break;
            }
        } else {
            ImageIO.write(bufferedImage, format.getExtension(), outputStream);
        }

    }
}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

@Override
public void load(InputStream inputStream) throws IOException {
    super.load(inputStream);

    // check that the file is a PDF
    ByteArrayInputStream bais = null;
    PDDocument document = null;

    try {/*from www  .j a v a  2  s . c  o  m*/

        bais = new ByteArrayInputStream(content);
        document = PDDocument.load(bais);

    } catch (Exception e) {
        throw new IOException("PDF document is not valid");
    } finally {
        if (document != null)
            document.close();
        IOUtils.closeQuietly(bais);
    }
}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

public int getPageCount() throws DocumentException {

    int pageCount = 0;

    if (content == null) {
        return pageCount;
    }//from   w  ww .  java  2  s  .c  o  m

    ByteArrayInputStream bais = null;
    PDDocument document = null;

    try {

        bais = new ByteArrayInputStream(content);
        document = PDDocument.load(bais);
        pageCount = document.getNumberOfPages();
    } catch (Exception e) {
        throw new DocumentException(e);
    } finally {
        if (document != null)
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        IOUtils.closeQuietly(bais);
    }

    return pageCount;

}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

public Document extract(int begin, int end) throws DocumentException {

    this.assertValidPageRange(begin, end);

    PDFDocument result = new PDFDocument();

    ByteArrayInputStream bais = null;
    ByteArrayOutputStream baos = null;

    if (content != null) {

        PDDocument document = new PDDocument();

        try {//from   ww  w.  ja va2s .c  om

            bais = new ByteArrayInputStream(content);
            baos = new ByteArrayOutputStream();
            PDDocument inputPDF = PDDocument.load(bais);
            while (begin <= end) {
                document.addPage((PDPage) inputPDF.getDocumentCatalog().getAllPages().get(begin - 1));
                begin++;
            }
            document.save(baos);
            document.close();
            result.load(new ByteArrayInputStream(baos.toByteArray()));

        } catch (Exception e) {
            throw new DocumentException(e);
        } finally {
            IOUtils.closeQuietly(bais);
            IOUtils.closeQuietly(baos);
        }

    }

    return result;
}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

@Override
public void append(Document document) throws DocumentException {

    super.append(document);

    ByteArrayOutputStream baos = null;
    PDDocument mergedDocument = new PDDocument();

    try {/*from   w w  w  . j  ava2s .  c  o  m*/

        baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = new ByteArrayInputStream(content);
        PDDocument pDocument = PDDocument.load(bais);
        int pageCount = pDocument.getNumberOfPages();
        for (int i = 0; i < pageCount; i++) {
            mergedDocument.addPage((PDPage) pDocument.getDocumentCatalog().getAllPages().get(i));
        }

        // copy new document
        ByteArrayInputStream baisNewDoc = new ByteArrayInputStream(document.getContent());
        PDDocument pNewDocument = PDDocument.load(baisNewDoc);
        pageCount = pNewDocument.getNumberOfPages();
        for (int i = 0; i < pageCount; i++) {
            mergedDocument.addPage((PDPage) pNewDocument.getDocumentCatalog().getAllPages().get(i));
        }
        mergedDocument.save(baos);
        mergedDocument.close();
        // replace content with new content
        content = baos.toByteArray();

    } catch (Exception e) {
        throw new DocumentException(e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

}

From source file:org.github.jipsg.pdfbox.PdfToImageConverter.java

License:Apache License

/**
 * Generates preview images of the given PDF document.
 *
 * @param source      the opaque source of the PDF
 * @param imageFormat the requested image format, e.g. "jpeg"
 * @param startPage   the first extracted page
 * @param endPage     the las extracted page
 * @param resolution  the resolution of the extracted images
 * @param color       the color model, e.g. "rgb"
 * @return list of images/*from   w w w.  ja  v  a2s .  c  o  m*/
 * @throws Exception the operation failed
 */
public List<BufferedImage> toImages(Object source, String imageFormat, int startPage, int endPage,
        int resolution, String color) throws Exception {
    PDDocument pdDocument = new PDDocumentFactory().create(source);

    try {
        return toImages(pdDocument, imageFormat, startPage, endPage, resolution, color);
    } finally {
        if (pdDocument != null) {
            pdDocument.close();
        }
    }
}

From source file:org.hippoecm.repository.PdfExtractedTextWithLineBreaksAreIndexedCorrectlyTest.java

License:Apache License

private void createDocumentWithPdf(String name, String lineSeparator) throws Exception {

    Node handle = testPath.addNode(name, HippoNodeType.NT_HANDLE);
    Node document = handle.addNode(name, NT_SEARCHDOCUMENT);

    Node compound = document.addNode("substructure", NT_COMPOUNDSTRUCTURE);
    Node resource = compound.addNode("hippo:testresource", "hippo:resource");

    {/*w  w  w. ja  v a 2  s.  co  m*/
        resource.setProperty("jcr:encoding", "UTF-8");
        resource.setProperty("jcr:mimeType", "application/pdf");
        InputStream pdf = this.getClass().getResourceAsStream(WORDS_ON_NEW_LINE_WITHOUT_SPACES);
        resource.setProperty("jcr:data", new BinaryImpl(new BufferedInputStream(pdf)));
        resource.setProperty("jcr:lastModified", Calendar.getInstance());
    }
    {
        InputStream pdf = this.getClass().getResourceAsStream(WORDS_ON_NEW_LINE_WITHOUT_SPACES);
        try {
            PDFParser parser = new PDFParser(new BufferedInputStream(pdf));
            PDDocument pdDocument = null;
            try {
                parser.parse();
                pdDocument = parser.getPDDocument();
                CharArrayWriter writer = new CharArrayWriter();

                PDFTextStripper stripper = new PDFTextStripper();
                if (lineSeparator != null) {
                    stripper.setLineSeparator(lineSeparator);
                }
                stripper.writeText(pdDocument, writer);

                StringBuilder extracted = new StringBuilder();
                extracted.append(writer.toCharArray());
                // make sure to store it as UTF-8
                InputStream extractedStream = IOUtils.toInputStream(extracted.toString(), "UTF-8");
                resource.setProperty("hippo:text",
                        resource.getSession().getValueFactory().createBinary(extractedStream));
            } finally {
                try {
                    if (pdDocument != null) {
                        pdDocument.close();
                    }
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (Exception e) {
            // it may happen that PDFParser throws a runtime
            // exception when parsing certain pdf documents

            // we set empty text:
            final ByteArrayInputStream emptyByteArrayInputStream = new ByteArrayInputStream(new byte[0]);
            resource.setProperty("hippo:text",
                    resource.getSession().getValueFactory().createBinary(emptyByteArrayInputStream));

        } finally {
            pdf.close();
        }
    }
    testPath.getSession().save();
}

From source file:org.hippoecm.repository.PdfExtractionAndIndexingTest.java

License:Apache License

private void createDocumentWithPdf(String name, boolean includeHippoText) throws Exception {

    Node handle = testPath.addNode(name, HippoNodeType.NT_HANDLE);
    Node document = handle.addNode(name, NT_SEARCHDOCUMENT);

    Node compound = document.addNode("substructure", NT_COMPOUNDSTRUCTURE);
    Node resource = compound.addNode("hippo:testresource", "hippo:resource");

    {/*from w  w  w .j  a  va2s  .c  om*/
        resource.setProperty("jcr:encoding", "UTF-8");
        resource.setProperty("jcr:mimeType", "text/plain");
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(data, "UTF-8");
        writer.write("The quick brown fox jumps over the lazy " + UNIQUE_WORD_IN_PLAIN_TEXT);
        writer.close();
        resource.setProperty("jcr:data", new BinaryImpl(new ByteArrayInputStream(data.toByteArray())));
        resource.setProperty("jcr:lastModified", Calendar.getInstance());
    }

    if (includeHippoText) {
        InputStream pdf = this.getClass().getResourceAsStream(UNITTEST_PDF_FILE_NAME);
        try {
            PDFParser parser = new PDFParser(new BufferedInputStream(pdf));
            PDDocument pdDocument = null;
            try {
                parser.parse();
                pdDocument = parser.getPDDocument();
                CharArrayWriter writer = new CharArrayWriter();

                PDFTextStripper stripper = new PDFTextStripper();
                stripper.setLineSeparator("\n");
                stripper.writeText(pdDocument, writer);

                StringBuilder extracted = new StringBuilder();
                extracted.append(writer.toCharArray());
                // make sure to store it as UTF-8
                InputStream extractedStream = IOUtils.toInputStream(extracted.toString(), "UTF-8");
                resource.setProperty("hippo:text",
                        resource.getSession().getValueFactory().createBinary(extractedStream));
            } finally {
                try {
                    if (pdDocument != null) {
                        pdDocument.close();
                    }
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (Exception e) {
            // it may happen that PDFParser throws a runtime
            // exception when parsing certain pdf documents

            // we set empty text:
            final ByteArrayInputStream emptyByteArrayInputStream = new ByteArrayInputStream(new byte[0]);
            resource.setProperty("hippo:text",
                    resource.getSession().getValueFactory().createBinary(emptyByteArrayInputStream));

        } finally {
            pdf.close();
        }
    }
    testPath.save();
}

From source file:org.hippoecm.repository.PdfExtractionAndIndexingTest.java

License:Apache License

private void createDocumentWithPdfAndHippoTextBinary(String name, boolean setHippoBinaryEmpty)
        throws Exception {

    Node handle = testPath.addNode(name, HippoNodeType.NT_HANDLE);
    Node document = handle.addNode(name, NT_SEARCHDOCUMENT);

    Node compound = document.addNode("substructure", NT_COMPOUNDSTRUCTURE);
    Node resource = compound.addNode("hippo:testresource", "hippo:resource");

    {/*from   ww  w  . j  av  a 2  s .  c om*/
        resource.setProperty("jcr:encoding", "UTF-8");
        resource.setProperty("jcr:mimeType", "text/plain");
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(data, "UTF-8");
        writer.write("The quick brown fox jumps over the lazy " + UNIQUE_WORD_IN_PLAIN_TEXT);
        writer.close();
        resource.setProperty("jcr:data", new BinaryImpl(new ByteArrayInputStream(data.toByteArray())));
        resource.setProperty("jcr:lastModified", Calendar.getInstance());
    }

    if (setHippoBinaryEmpty) {
        final ByteArrayInputStream emptyByteArrayInputStream = new ByteArrayInputStream(new byte[0]);
        resource.setProperty("hippo:text",
                resource.getSession().getValueFactory().createBinary(emptyByteArrayInputStream));
    } else {
        InputStream pdf = this.getClass().getResourceAsStream(UNITTEST_PDF_FILE_NAME);
        try {
            PDFParser parser = new PDFParser(new BufferedInputStream(pdf));
            PDDocument pdDocument = null;
            try {
                parser.parse();
                pdDocument = parser.getPDDocument();
                CharArrayWriter writer = new CharArrayWriter();

                PDFTextStripper stripper = new PDFTextStripper();
                stripper.setLineSeparator("\n");
                stripper.writeText(pdDocument, writer);

                StringBuilder extracted = new StringBuilder();
                extracted.append(writer.toCharArray());
                // make sure to store it as UTF-8
                InputStream extractedStream = IOUtils.toInputStream(extracted.toString(), "UTF-8");
                resource.setProperty("hippo:text",
                        resource.getSession().getValueFactory().createBinary(extractedStream));
            } finally {
                try {
                    if (pdDocument != null) {
                        pdDocument.close();
                    }
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (Exception e) {
            // it may happen that PDFParser throws a runtime
            // exception when parsing certain pdf documents

            // we set empty text:
            final ByteArrayInputStream emptyByteArrayInputStream = new ByteArrayInputStream(new byte[0]);
            resource.setProperty("hippo:text",
                    resource.getSession().getValueFactory().createBinary(emptyByteArrayInputStream));

        } finally {
            pdf.close();
        }
    }
    testPath.getSession().save();
}

From source file:org.jahia.modules.dm.thumbnails.impl.PDFBoxPDF2ImageConverterService.java

License:Open Source License

public BufferedImage getImageOfPage(InputStream pdfInputStream, int pageNumber)
        throws DocumentOperationException {
    BufferedImage image = null;/*w w  w  . j av  a2  s  .  c  o m*/

    long timer = System.currentTimeMillis();

    PDDocument pdfDoc = null;
    try {
        pdfDoc = PDDocument.load(pdfInputStream);
        PDPage page = (PDPage) pdfDoc.getDocumentCatalog().getAllPages().get(pageNumber);
        image = page.convertToImage(imageType, resolution);

        if (image != null && logger.isDebugEnabled()) {
            logger.debug("Generated an image for the page {} of the supplied input stream in {} ms", pageNumber,
                    (System.currentTimeMillis() - timer));
        }
    } catch (IndexOutOfBoundsException e) {
        logger.warn("No page with the number {} found in the PDF document", pageNumber);
    } catch (IOException e) {
        throw new DocumentOperationException("Error occurred trying to generate an image for the page "
                + pageNumber + " of the supplied input stream", e);
    } finally {
        try {
            pdfDoc.close();
        } catch (Exception e) {
            // ignore
        }
    }

    return image;
}