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.ala.harvester.ExtractPubfSciNamesAndImages.java

License:Apache License

/**
 * This will print the documents text in a certain area.
 *
 * @param args The command line arguments.
 *
 * @throws Exception If there is an error parsing the document.
 *//*from   w w  w.java  2 s.c om*/
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        usage();
    } else {
        PDDocument document = null;
        try {
            document = PDDocument.load(args[0]);
            if (document.isEncrypted()) {
                try {
                    document.decrypt("");
                } catch (InvalidPasswordException e) {
                    System.err.println("Error: Document is encrypted with a password.");
                    System.exit(1);
                }
            }

            extractSciNameAndImages(document);

        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFAppendActionExecuter.java

License:Apache License

/**
 * @param reader//from  w  w w.  j  ava  2s  .  co  m
 * @param writer
 * @param options
 * @throws Exception
 */
protected final void action(Action ruleAction, NodeRef actionedUponNodeRef, NodeRef targetNodeRef,
        ContentReader reader, ContentReader targetContentReader, Map<String, Object> options) {
    PDDocument pdf = null;
    PDDocument pdfTarget = null;
    InputStream is = null;
    InputStream tis = null;
    File tempDir = null;
    ContentWriter writer = null;

    try {
        is = reader.getContentInputStream();
        tis = targetContentReader.getContentInputStream();
        // stream the document in
        pdf = PDDocument.load(is);
        pdfTarget = PDDocument.load(tis);
        // Append the PDFs
        PDFMergerUtility merger = new PDFMergerUtility();
        merger.appendDocument(pdfTarget, pdf);
        merger.setDestinationFileName(options.get(PARAM_DESTINATION_NAME).toString());
        merger.mergeDocuments();

        // build a temp dir name based on the ID of the noderef we are
        // importing
        File alfTempDir = TempFileProvider.getTempDir();
        tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId());
        tempDir.mkdir();

        String fileName = options.get(PARAM_DESTINATION_NAME).toString();
        pdfTarget.save(tempDir + "" + File.separatorChar + fileName + FILE_EXTENSION);

        for (File file : tempDir.listFiles()) {
            try {
                if (file.isFile()) {
                    // Get a writer and prep it for putting it back into the
                    // repo
                    NodeRef destinationNode = createDestinationNode(file.getName(),
                            (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER),
                            actionedUponNodeRef);
                    writer = serviceRegistry.getContentService().getWriter(destinationNode,
                            ContentModel.PROP_CONTENT, true);

                    writer.setEncoding(reader.getEncoding()); // original
                                                              // encoding
                    writer.setMimetype(FILE_MIMETYPE);

                    // Put it in the repo
                    writer.putContent(file);

                    // Clean up
                    file.delete();
                }
            } catch (FileExistsException e) {
                throw new AlfrescoRuntimeException("Failed to process file.", e);
            }
        }
    } catch (COSVisitorException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    }

    finally {
        if (pdf != null) {
            try {
                pdf.close();
            } catch (IOException e) {
                throw new AlfrescoRuntimeException(e.getMessage(), e);
            }
        }
        if (pdfTarget != null) {
            try {
                pdfTarget.close();
            } catch (IOException e) {
                throw new AlfrescoRuntimeException(e.getMessage(), e);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw new AlfrescoRuntimeException(e.getMessage(), e);
            }
        }

        if (tempDir != null) {
            tempDir.delete();
        }
    }
}

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFInsertAtPageActionExecuter.java

License:Apache License

/**
 * @param reader//from   w w  w.  ja v  a  2 s.  c o  m
 * @param writer
 * @param options
 * @throws Exception
 */
protected final void action(Action ruleAction, NodeRef actionedUponNodeRef, ContentReader reader,
        ContentReader insertReader, Map<String, Object> options) {
    PDDocument pdf = null;
    PDDocument insertContentPDF = null;
    InputStream is = null;
    InputStream cis = null;
    File tempDir = null;
    ContentWriter writer = null;

    try {

        int insertAt = Integer.valueOf((String) options.get(PARAM_INSERT_AT_PAGE)).intValue();

        // Get contentReader inputStream
        is = reader.getContentInputStream();
        // Get insertContentReader inputStream
        cis = insertReader.getContentInputStream();
        // stream the target document in
        pdf = PDDocument.load(is);
        // stream the insert content document in
        insertContentPDF = PDDocument.load(cis);

        // split the PDF and put the pages in a list
        Splitter splitter = new Splitter();
        // Need to adjust the input value to get the split at the right page
        splitter.setSplitAtPage(insertAt - 1);

        // Split the pages
        List<PDDocument> pdfs = splitter.split(pdf);

        // Build the output PDF
        PDFMergerUtility merger = new PDFMergerUtility();
        merger.appendDocument((PDDocument) pdfs.get(0), insertContentPDF);
        merger.appendDocument((PDDocument) pdfs.get(0), (PDDocument) pdfs.get(1));
        merger.setDestinationFileName(options.get(PARAM_DESTINATION_NAME).toString());
        merger.mergeDocuments();

        // build a temp dir, name based on the ID of the noderef we are
        // importing
        File alfTempDir = TempFileProvider.getTempDir();
        tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId());
        tempDir.mkdir();

        String fileName = options.get(PARAM_DESTINATION_NAME).toString();

        PDDocument completePDF = (PDDocument) pdfs.get(0);

        completePDF.save(tempDir + "" + File.separatorChar + fileName + FILE_EXTENSION);

        try {
            completePDF.close();
        } catch (IOException e) {
            throw new AlfrescoRuntimeException(e.getMessage(), e);
        }

        for (File file : tempDir.listFiles()) {
            try {
                if (file.isFile()) {

                    // Get a writer and prep it for putting it back into the
                    // repo
                    NodeRef destinationNode = createDestinationNode(file.getName(),
                            (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER),
                            actionedUponNodeRef);
                    writer = serviceRegistry.getContentService().getWriter(destinationNode,
                            ContentModel.PROP_CONTENT, true);

                    writer.setEncoding(reader.getEncoding()); // original
                    // encoding
                    writer.setMimetype(FILE_MIMETYPE);

                    // Put it in the repo
                    writer.putContent(file);

                    // Clean up
                    file.delete();
                }
            } catch (FileExistsException e) {
                throw new AlfrescoRuntimeException("Failed to process file.", e);
            }
        }
    }
    // TODO add better handling
    catch (COSVisitorException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    }

    finally {
        if (pdf != null) {
            try {
                pdf.close();
            } catch (IOException e) {
                throw new AlfrescoRuntimeException(e.getMessage(), e);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw new AlfrescoRuntimeException(e.getMessage(), e);
            }
        }

        if (tempDir != null) {
            tempDir.delete();
        }
    }
}

From source file:org.alfresco.repo.content.transform.OOoContentTransformerHelper.java

License:Open Source License

/**
 * This method produces an empty PDF file at the specified File location.
 * Apache's PDFBox is used to create the PDF file.
 *///  ww w .j  a  v  a2 s .c o  m
private void produceEmptyPdfFile(File tempToFile) {
    // If improvement PDFBOX-914 is incorporated, we can do this with a straight call to
    // org.apache.pdfbox.TextToPdf.createPDFFromText(new StringReader(""));
    // https://issues.apache.org/jira/browse/PDFBOX-914

    PDDocument pdfDoc = null;
    PDPageContentStream contentStream = null;
    try {
        pdfDoc = new PDDocument();
        PDPage pdfPage = new PDPage();
        // Even though, we want an empty PDF, some libs (e.g. PDFRenderer) object to PDFs
        // that have literally nothing in them. So we'll put a content stream in it.
        contentStream = new PDPageContentStream(pdfDoc, pdfPage);
        pdfDoc.addPage(pdfPage);

        // Now write the in-memory PDF document into the temporary file.
        pdfDoc.save(tempToFile.getAbsolutePath());

    } catch (COSVisitorException cvx) {
        throw new ContentIOException("Error creating empty PDF file", cvx);
    } catch (IOException iox) {
        throw new ContentIOException("Error creating empty PDF file", iox);
    } finally {
        if (contentStream != null) {
            try {
                contentStream.close();
            } catch (IOException ignored) {
                // Intentionally empty
            }
        }
        if (pdfDoc != null) {
            try {
                pdfDoc.close();
            } catch (IOException ignored) {
                // Intentionally empty.
            }
        }
    }
}

From source file:org.alfresco.repo.content.transform.TextToPdfContentTransformer.java

License:Open Source License

@Override
protected void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options)
        throws Exception {
    PDDocument pdf = null;
    InputStream is = null;// w ww  . j a  va  2  s. com
    InputStreamReader ir = null;
    OutputStream os = null;
    try {
        is = reader.getContentInputStream();
        ir = buildReader(is, reader.getEncoding(), reader.getContentUrl());

        TransformationOptionLimits limits = getLimits(reader, writer, options);
        TransformationOptionPair pageLimits = limits.getPagesPair();
        pdf = transformer.createPDFFromText(ir, pageLimits, reader.getContentUrl());

        // dump it all to the writer
        os = writer.getContentOutputStream();
        pdf.save(os);
    } finally {
        if (pdf != null) {
            try {
                pdf.close();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        if (ir != null) {
            try {
                ir.close();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.alfresco.repo.content.transform.TextToPdfContentTransformerTest.java

License:Open Source License

private void transformTextAndCheck(String text, String encoding, String checkText) throws IOException {
    // Get a reader for the text
    ContentReader reader = buildContentReader(text, Charset.forName(encoding));

    // And a temp writer
    File out = TempFileProvider.createTempFile("AlfrescoTest_", ".pdf");
    ContentWriter writer = new FileContentWriter(out);
    writer.setMimetype("application/pdf");

    // Transform to PDF
    transformer.transform(reader, writer);

    // Read back in the PDF and check it
    PDDocument doc = PDDocument.load(out);
    PDFTextStripper textStripper = new PDFTextStripper();
    StringWriter textWriter = new StringWriter();
    textStripper.writeText(doc, textWriter);
    doc.close();

    String roundTrip = clean(textWriter.toString());

    assertEquals("Incorrect text in PDF when starting from text in " + encoding, checkText, roundTrip);
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

private String writeText(FontInfo fi, String pdf) throws IOException {
    PDDocument doc = getResource(pdf);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    String c = getPDFBoxAdapter().createStreamFromPDFBoxPage(doc, page, pdf, at, fi, new Rectangle());
    //        PDResources sourcePageResources = page.findResources();
    //        COSDictionary fonts = (COSDictionary)sourcePageResources.getCOSDictionary().getDictionaryObject(COSName.FONT);
    //        PDFBoxAdapter.PDFWriter w = adapter. new MergeFontsPDFWriter(fonts, fi, "", new ArrayList<COSName>());
    //        String c = w.writeText(page.getContents());
    doc.close();
    return c;//from   w  ww. j  a va2s .  co  m
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testCFF() throws Exception {
    PDDocument doc = getResource(CFF1);
    FOPPDFSingleByteFont sbfont = new FOPPDFSingleByteFont(getFont(doc, "R11"),
            "MyriadPro-Regular_Type1f0encstdcs");

    Assert.assertTrue(Arrays.asList(sbfont.getEncoding().getCharNameMap()).contains("bracketright"));
    Assert.assertTrue(!Arrays.asList(sbfont.getEncoding().getCharNameMap()).contains("A"));
    Assert.assertTrue(!Arrays.toString(sbfont.getEncoding().getUnicodeCharMap()).contains("A"));
    Assert.assertEquals(sbfont.mapChar('A'), 0);
    Assert.assertEquals(sbfont.getWidths().length, 28);
    Assert.assertEquals(sbfont.getFirstChar(), 87);
    Assert.assertEquals(sbfont.getLastChar(), 114);

    PDDocument doc2 = getResource(CFF2);
    String name = sbfont.addFont(getFont(doc2, "R11"));
    Assert.assertTrue(name.contains("MyriadPro"));

    Assert.assertEquals(sbfont.getFontName(), "MyriadPro-Regular_Type1f0encstdcs");
    Assert.assertEquals(sbfont.getEncodingName(), "WinAnsiEncoding");
    Assert.assertEquals(sbfont.mapChar('W'), 'W');
    String x = IOUtils.toString(sbfont.getInputStream());
    Assert.assertTrue(x, x.contains("Adobe Systems"));
    Assert.assertEquals(sbfont.getEncoding().getName(), "FOPPDFEncoding");
    Assert.assertTrue(Arrays.asList(sbfont.getEncoding().getCharNameMap()).contains("A"));
    Assert.assertEquals(sbfont.getWidths().length, 65);
    Assert.assertEquals(sbfont.getFirstChar(), 50);
    Assert.assertEquals(sbfont.getLastChar(), 114);

    Assert.assertEquals(sbfont.addFont(getFont(doc2, "R13")), null);

    doc.close();
    doc2.close();// w  w w  . j a v a2s. co m
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testCFF2() throws Exception {
    PDDocument doc = getResource(CFF3);
    FOPPDFSingleByteFont sbfont = new FOPPDFSingleByteFont(getFont(doc, "T1_0"), "Myriad_Pro_Type1f0encf1cs");
    Assert.assertTrue(Arrays.asList(sbfont.getEncoding().getCharNameMap()).contains("uni004E"));
    Assert.assertEquals(sbfont.getFontName(), "Myriad_Pro_Type1f0encf1cs");
    Assert.assertEquals(sbfont.getEncodingName(), null);
    byte[] is = IOUtils.toByteArray(sbfont.getInputStream());

    CFFParser p = new CFFParser();
    CFFFont ff = p.parse(is).get(0);// w  w  w  .  jav  a 2s .c o  m
    Assert.assertEquals(ff.getName(), "MNEACN+Myriad_Pro");
    //        Assert.assertEquals(ff.getCharset().getEntries().get(0).getSID(), 391);

    doc.close();
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testTTCID() throws Exception {
    PDDocument doc = getResource(TTCID1);
    FOPPDFMultiByteFont mbfont = new FOPPDFMultiByteFont(getFont(doc, "C2_0"), "ArialMT_Type0");
    mbfont.addFont(getFont(doc, "C2_0"));
    Assert.assertEquals(mbfont.mapChar('t'), 67);

    PDDocument doc2 = getResource(TTCID2);
    String name = mbfont.addFont(getFont(doc2, "C2_0"));
    Assert.assertEquals(name, "ArialMT_Type0");
    Assert.assertEquals(mbfont.getFontName(), "ArialMT_Type0");
    byte[] is = IOUtils.toByteArray(mbfont.getInputStream());
    Assert.assertEquals(is.length, 38640);
    doc.close();
    doc2.close();/*  w  w  w . jav  a 2s .com*/
}