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:org.pdfgal.pdfgal.pdfgal.PDFGalTest.java

License:Open Source License

@Test
public void merge() {

    final String inputUri1 = System.getProperty("user.dir") + TEST_RESOURCES + "merge\\IMergeTest_1.pdf";
    final String inputUri2 = System.getProperty("user.dir") + TEST_RESOURCES + "merge\\IMergeTest_2.pdf";
    final String inputUri3 = System.getProperty("user.dir") + TEST_RESOURCES + "merge\\IMergeTest_3.pdf";
    final String inputUri4 = System.getProperty("user.dir") + TEST_RESOURCES + "merge\\IMergeTest_4.pdf";
    final String inputUri5 = System.getProperty("user.dir") + TEST_RESOURCES + "merge\\IMergeTest_5.pdf";
    final String outputUri = System.getProperty("user.dir") + TEST_RESOURCES + "merge\\OMergeTest.pdf";

    final List<String> inputUris = new ArrayList<String>();
    inputUris.add(inputUri1);/*from  www . ja  va 2  s  .c o m*/
    inputUris.add(inputUri2);
    inputUris.add(inputUri3);
    inputUris.add(inputUri4);
    inputUris.add(inputUri5);

    try {
        this.pdfGal.merge(inputUris, outputUri);

        final PDDocument inputDoc1 = PDDocument.load(inputUri1);
        final PDDocument inputDoc2 = PDDocument.load(inputUri2);
        final PDDocument inputDoc3 = PDDocument.load(inputUri3);
        final PDDocument inputDoc4 = PDDocument.load(inputUri4);
        final PDDocument inputDoc5 = PDDocument.load(inputUri5);
        final PDDocument outputDoc = PDDocument.load(outputUri);

        final Integer inputDoc1Size = inputDoc1.getNumberOfPages();
        final Integer inputDoc2Size = inputDoc2.getNumberOfPages();
        final Integer inputDoc3Size = inputDoc3.getNumberOfPages();
        final Integer inputDoc4Size = inputDoc4.getNumberOfPages();
        final Integer inputDoc5Size = inputDoc5.getNumberOfPages();

        final Integer inputDocsPagesTotal = inputDoc1Size + inputDoc2Size + inputDoc3Size + inputDoc4Size
                + inputDoc5Size;
        assertEquals(new Integer(outputDoc.getNumberOfPages()), inputDocsPagesTotal);

        final PDFTextStripper pdfStripper = new PDFTextStripper();
        String inputText = null;
        String outputText = null;
        Integer lastOutputPage = null;

        /* Let's compare the first document */
        // Extraction of output text
        lastOutputPage = inputDoc1Size;
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(lastOutputPage);
        outputText = pdfStripper.getText(outputDoc);
        // Extraction of input text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(inputDoc1Size);
        inputText = pdfStripper.getText(inputDoc1);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the second document */
        // Extraction of output text
        pdfStripper.setStartPage(lastOutputPage + 1);
        lastOutputPage = lastOutputPage + inputDoc2Size;
        pdfStripper.setEndPage(lastOutputPage);
        outputText = pdfStripper.getText(outputDoc);
        // Extraction of input text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(inputDoc2Size);
        inputText = pdfStripper.getText(inputDoc2);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the third document */
        // Extraction of output text
        pdfStripper.setStartPage(lastOutputPage + 1);
        lastOutputPage = lastOutputPage + inputDoc3Size;
        pdfStripper.setEndPage(lastOutputPage);
        outputText = pdfStripper.getText(outputDoc);
        // Extraction of input text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(inputDoc3Size);
        inputText = pdfStripper.getText(inputDoc3);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the fourth document */
        // Extraction of output text
        pdfStripper.setStartPage(lastOutputPage + 1);
        lastOutputPage = lastOutputPage + inputDoc4Size;
        pdfStripper.setEndPage(lastOutputPage);
        outputText = pdfStripper.getText(outputDoc);
        // Extraction of input text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(inputDoc4Size);
        inputText = pdfStripper.getText(inputDoc4);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the fifth document */
        // Extraction of output text
        pdfStripper.setStartPage(lastOutputPage + 1);
        lastOutputPage = lastOutputPage + inputDoc5Size;
        pdfStripper.setEndPage(lastOutputPage);
        outputText = pdfStripper.getText(outputDoc);
        // Extraction of input text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(inputDoc5Size);
        inputText = pdfStripper.getText(inputDoc5);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        // Documents clossing
        inputDoc1.close();
        inputDoc2.close();
        inputDoc3.close();
        inputDoc4.close();
        inputDoc5.close();
        outputDoc.close();

    } catch (final Exception e) {
        assertFalse(true);
    }
}

From source file:org.pdfgal.pdfgal.pdfgal.PDFGalTest.java

License:Open Source License

/**
 * Test for method PDFGal.split(String inputUri, String outputUri,
 * List<Integer> pages) throws IOException, COSVisitorException;
 *//* w ww  . j a v  a  2s.co m*/
@Test
public void splitSettingPageNumber() {

    final String inputUri = System.getProperty("user.dir") + TEST_RESOURCES + "splitlist\\ISplitListTest.pdf";
    final String outputUri = System.getProperty("user.dir") + TEST_RESOURCES + "splitlist\\OSplitListTest.pdf";
    final String outputUri1 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitlist\\OSplitListTest_1.pdf";
    final String outputUri2 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitlist\\OSplitListTest_2.pdf";
    final String outputUri3 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitlist\\OSplitListTest_3.pdf";
    final String outputUri4 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitlist\\OSplitListTest_4.pdf";

    final List<Integer> pages = new ArrayList<Integer>();
    pages.add(2);
    pages.add(5);
    pages.add(7);

    try {
        this.pdfGal.split(inputUri, outputUri, pages);

        final PDDocument outputDoc1 = PDDocument.load(outputUri1);
        final PDDocument outputDoc2 = PDDocument.load(outputUri2);
        final PDDocument outputDoc3 = PDDocument.load(outputUri3);
        final PDDocument outputDoc4 = PDDocument.load(outputUri4);
        final PDDocument inputDoc = PDDocument.load(inputUri);

        final Integer outputDoc1Size = outputDoc1.getNumberOfPages();
        final Integer outputDoc2Size = outputDoc2.getNumberOfPages();
        final Integer outputDoc3Size = outputDoc3.getNumberOfPages();
        final Integer outputDoc4Size = outputDoc4.getNumberOfPages();

        final Integer inputDocsPagesTotal = outputDoc1Size + outputDoc2Size + outputDoc3Size + outputDoc4Size;
        assertEquals(new Integer(inputDoc.getNumberOfPages()), inputDocsPagesTotal);

        final PDFTextStripper pdfStripper = new PDFTextStripper();
        String inputText = null;
        String outputText = null;
        Integer lastInputPage = null;

        /* Let's compare the first document */
        // Extraction of input text
        lastInputPage = outputDoc1Size;
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc1Size);
        outputText = pdfStripper.getText(outputDoc1);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the second document */
        // Extraction of input text
        pdfStripper.setStartPage(lastInputPage + 1);
        lastInputPage = lastInputPage + outputDoc2Size;
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc2Size);
        outputText = pdfStripper.getText(outputDoc2);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the third document */
        // Extraction of input text
        pdfStripper.setStartPage(lastInputPage + 1);
        lastInputPage = lastInputPage + outputDoc3Size;
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc3Size);
        outputText = pdfStripper.getText(outputDoc3);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the fourth document */
        // Extraction of input text
        pdfStripper.setStartPage(lastInputPage + 1);
        lastInputPage = lastInputPage + outputDoc4Size;
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc4Size);
        outputText = pdfStripper.getText(outputDoc4);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        // Closing documents
        outputDoc1.close();
        outputDoc2.close();
        outputDoc3.close();
        outputDoc4.close();
        inputDoc.close();

    } catch (final Exception e) {
        assertFalse(true);
    }
}

From source file:org.pdfgal.pdfgal.pdfgal.PDFGalTest.java

License:Open Source License

/**
 * Test for method PDFGal.split(String inputUri, String outputUri, Integer
 * pages) throws IOException, COSVisitorException;
 *//*from  ww w .  ja  v  a  2s  .c om*/
@Test
public void splitSettingNumberOfPages() {

    final String inputUri = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitinteger\\ISplitIntegerTest.pdf";
    final String outputUri = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitinteger\\OSplitIntegerTest.pdf";
    final String outputUri1 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitinteger\\OSplitIntegerTest_1.pdf";
    final String outputUri2 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitinteger\\OSplitIntegerTest_2.pdf";
    final String outputUri3 = System.getProperty("user.dir") + TEST_RESOURCES
            + "splitinteger\\OSplitIntegerTest_3.pdf";

    final Integer pages = 3;

    try {
        this.pdfGal.split(inputUri, outputUri, pages);

        final PDDocument outputDoc1 = PDDocument.load(outputUri1);
        final PDDocument outputDoc2 = PDDocument.load(outputUri2);
        final PDDocument outputDoc3 = PDDocument.load(outputUri3);
        final PDDocument inputDoc = PDDocument.load(inputUri);

        final Integer outputDoc1Size = outputDoc1.getNumberOfPages();
        final Integer outputDoc2Size = outputDoc2.getNumberOfPages();
        final Integer outputDoc3Size = outputDoc3.getNumberOfPages();

        final Integer inputDocsPagesTotal = outputDoc1Size + outputDoc2Size + outputDoc3Size;
        assertEquals(new Integer(inputDoc.getNumberOfPages()), inputDocsPagesTotal);

        final PDFTextStripper pdfStripper = new PDFTextStripper();
        String inputText = null;
        String outputText = null;
        Integer lastInputPage = null;

        /* Let's compare the first document */
        // Extraction of input text
        lastInputPage = outputDoc1Size;
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc1Size);
        outputText = pdfStripper.getText(outputDoc1);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the second document */
        // Extraction of input text
        pdfStripper.setStartPage(lastInputPage + 1);
        lastInputPage = lastInputPage + outputDoc2Size;
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc2Size);
        outputText = pdfStripper.getText(outputDoc2);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        /* Let's compare the third document */
        // Extraction of input text
        pdfStripper.setStartPage(lastInputPage + 1);
        lastInputPage = lastInputPage + outputDoc3Size;
        pdfStripper.setEndPage(lastInputPage);
        inputText = pdfStripper.getText(inputDoc);
        // Extraction of output text
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(outputDoc3Size);
        outputText = pdfStripper.getText(outputDoc3);
        // Comparison of output and input text
        assertEquals(outputText, inputText);

        // Closing documents
        outputDoc1.close();
        outputDoc2.close();
        outputDoc3.close();
        inputDoc.close();

    } catch (final Exception e) {
        assertFalse(true);
    }
}

From source file:org.pdfgal.pdfgal.pdfgal.PDFGalTest.java

License:Open Source License

@Test
public void protect() {

    final String inputUri = System.getProperty("user.dir") + TEST_RESOURCES + "protect\\IProtectTest.pdf";
    final String outputUri = System.getProperty("user.dir") + TEST_RESOURCES + "protect\\OProtectTest.pdf";
    final String password = "coNtra$1nA1";

    try {/*from w w  w  .  ja  v a2 s . c o  m*/
        // Document is going to be protected
        this.pdfGal.protect(inputUri, outputUri, password);

        final PDDocument doc = PDDocument.load(outputUri);

        try {
            // Try to open document with wrong password
            doc.openProtection(new StandardDecryptionMaterial(""));
        } catch (final Exception e) {
            // With wrong password, exception must be thrown, so this is
            // right
            assertTrue(true);
        }

        try {
            // Try to open document with right password, document should be
            // opened
            doc.openProtection(new StandardDecryptionMaterial(password));
            assertTrue(true);
        } catch (final Exception e) {
            assertFalse(true);
        }

        // Closing document
        doc.close();

    } catch (final Exception e) {
        assertFalse(true);
    }
}

From source file:org.pdfgal.pdfgal.pdfgal.PDFGalTest.java

License:Open Source License

@Test
public void unProtect() {

    final String inputUri = System.getProperty("user.dir") + TEST_RESOURCES + "unprotect\\IUnProtectTest.pdf";
    final String outputUri = System.getProperty("user.dir") + TEST_RESOURCES + "unprotect\\OUnProtectTest.pdf";
    final String password = "coNtra$1nA1";

    try {//from  www .j a v a  2  s  . c o m
        // Document is going to be unprotected
        this.pdfGal.unProtect(inputUri, outputUri, password);

        final PDDocument doc = PDDocument.load(outputUri);

        try {
            // Try to open document with previous password
            doc.openProtection(new StandardDecryptionMaterial(password));
        } catch (final Exception e) {
            // With previous password, exception must be thrown, so this is
            // right
            assertTrue(true);
        }

        // Closing document
        doc.close();

    } catch (final Exception e) {
        assertFalse(true);
    }
}

From source file:org.pdfgal.pdfgal.utils.impl.PDFUtilsImpl.java

License:Open Source License

@Override
public Integer getPages(final String uri) throws IOException {
    if (StringUtils.isBlank(uri)) {
        throw new IOException();
    }//from ww w  .  j ava2s . c om
    final PDDocument document = PDDocument.load(uri);
    final Integer numberOfPages = document.getNumberOfPages();
    document.close();
    return numberOfPages;
}

From source file:org.pdfgal.pdfgal.validator.impl.PDFGalValidatorImpl.java

License:Open Source License

@Override
public boolean isPDF(final String path) {

    boolean result = false;

    try {//from ww  w  .  jav  a2 s .  c  om
        final PDDocument document = PDDocument.load(path);
        document.close();
        result = true;
    } catch (final IOException e) {
        result = false;
    }

    return result;
}

From source file:org.pdfgal.pdfgal.validator.impl.PDFGalValidatorImpl.java

License:Open Source License

@Override
public boolean isEncrypted(final String path) {

    boolean result = false;

    PDDocument document;//from  ww  w  .j  a v  a 2 s  .  c o  m
    try {
        document = PDDocument.load(path);
        result = document.isEncrypted();
        document.close();
    } catch (final IOException e) {
        result = false;
    }

    return result;
}

From source file:org.pdfgal.pdfgal.validator.impl.PDFGalValidatorImpl.java

License:Open Source License

@Override
public boolean allLandscape(final String path) {

    boolean result = true;

    PDDocument document;//ww  w.  j  av a2s  . c  o m
    try {
        document = PDDocument.load(path);

        @SuppressWarnings("unchecked")
        final List<PDPage> pagesList = document.getDocumentCatalog().getAllPages();
        if (CollectionUtils.isNotEmpty(pagesList)) {
            for (final PDPage page : pagesList) {
                if ((page != null) && (page.getMediaBox() != null)
                        && (page.getMediaBox().getHeight() > page.getMediaBox().getWidth())) {

                    result = false;
                    break;
                }
            }
        }

        document.close();
    } catch (final IOException e) {
        result = false;
    }

    return result;
}

From source file:org.pdfmetamodifier.IOHelper.java

License:Apache License

/**
 * Save Outlines (bookmarks).//from  w w  w  .j  av a 2 s. c om
 * 
 * @param pdfFile
 *            Source PDF file.
 * @param outlinesFile
 *            File with Outlines (bookmarks) in user-frendly format.
 * @throws IOException
 */
/*
 * See:
 *      https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java?view=markup
 */
public static void saveOutlines(final File pdfFile, final File outlinesFile) throws IOException {
    PDDocument document = null;
    try {
        // Read PDF file.
        document = PDDocument.load(pdfFile);
        if (document.isEncrypted()) {
            throw new IOException("Document is encrypted.");
        }

        // Get data from PDF file.
        final PDDocumentCatalog catalog = document.getDocumentCatalog();

        final PDDocumentOutline outlines = catalog.getDocumentOutline();

        final PDPageTree pages = catalog.getPages();

        final PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(catalog);
        final PDDestinationNameTreeNode destinations = namesDictionary.getDests();

        // Convert.
        final List<String> lines = OutlineHelper.outlinesToLineList(outlines, pages, destinations);

        // Write line list into the text file.
        Files.write(outlinesFile.toPath(), lines);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}