Examples of PDDocument


Examples of org.pdfbox.pdmodel.PDDocument

     *
     * @throws IOException If there is an error creating the field.
     */
    public void testFlags() throws IOException
    {
        PDDocument doc = null;
        try
        {
            doc = new PDDocument();
            PDAcroForm form = new PDAcroForm( doc );
            PDTextbox textBox = new PDTextbox(form);
           
            //assert that default is false.
            assertFalse( textBox.shouldComb() );
           
            //try setting and clearing a single field
            textBox.setComb( true );
            assertTrue( textBox.shouldComb() );
            textBox.setComb( false );
            assertFalse( textBox.shouldComb() );
           
            //try setting and clearing multiple fields
            textBox.setComb( true );
            textBox.setDoNotScroll( true );
            assertTrue( textBox.shouldComb() );
            assertTrue( textBox.doNotScroll() );
           
            textBox.setComb( false );
            textBox.setDoNotScroll( false );
            assertFalse( textBox.shouldComb() );
            assertFalse( textBox.doNotScroll() );
           
            //assert that setting a field to false multiple times works
            textBox.setComb( false );
            assertFalse( textBox.shouldComb() );
            textBox.setComb( false );
            assertFalse( textBox.shouldComb() );
           
            //assert that setting a field to true multiple times works
            textBox.setComb( true );
            assertTrue( textBox.shouldComb() );
            textBox.setComb( true );
            assertTrue( textBox.shouldComb() );
           
           
           
           
           
        }
        finally
        {
            if( doc != null )
            {
                doc.close();
            }
        }
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

     * @throws Exception If there is an error during the test.
     */
    public void testProtectionError() throws Exception
    {
                       
        PDDocument doc = PDDocument.load(input);
        protect(doc, publicCert1.getAbsolutePath());
       
        doc.save(output.getAbsolutePath());
           
        doc.close();
                       
        PDDocument doc2 = PDDocument.load(output);   
       
        Exception e = null;
       
        try
        {       
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

     *
     * @throws Exception If there is an error during the test.
     */
    public void testProtection() throws Exception
    {
        PDDocument doc = PDDocument.load(input);
        protect(doc, publicCert1.getAbsolutePath());
       
        //Assert.assertTrue(doc.isEncrypted());
       
        doc.save(output.getAbsolutePath());
           
        doc.close();
                       
        PDDocument doc2 = PDDocument.load(output);
       
        Assert.assertNotNull(doc2);
       
        open(doc2, privateCert1.getAbsolutePath(), password1);       
       
        Assert.assertTrue(doc2.isEncrypted());
       
        AccessPermission currentAp = doc2.getCurrentAccessPermission();
       
        Assert.assertFalse(currentAp.canAssembleDocument());
        Assert.assertFalse(currentAp.canExtractContent());
        Assert.assertTrue(currentAp.canExtractForAccessibility());
        Assert.assertFalse(currentAp.canFillInForm());
        Assert.assertFalse(currentAp.canModify());
        Assert.assertFalse(currentAp.canModifyAnnotations());
        Assert.assertFalse(currentAp.canPrint());
        Assert.assertFalse(currentAp.canPrintDegraded());
       
        doc2.close();
           
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

    public void testMultipleRecipients() throws Exception
    {           
       
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
       
        PDDocument doc = PDDocument.load(input);
       
        PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
       
        PublicKeyRecipient recip1 = new PublicKeyRecipient();
        PublicKeyRecipient recip2 = new PublicKeyRecipient();
       
        recip1.setPermission(accessPermission);
        recip2.setPermission(accessPermission2);
       
        InputStream inStream = new FileInputStream(publicCert1);       
        Assert.assertNotNull(cf);
        X509Certificate certificate1 = (X509Certificate)cf.generateCertificate(inStream);
        inStream.close();       
       
        InputStream inStream2 = new FileInputStream(publicCert2);       
        Assert.assertNotNull(cf);
        X509Certificate certificate2 = (X509Certificate)cf.generateCertificate(inStream2);
        inStream.close();       
       
        recip1.setX509(certificate1);
        recip2.setX509(certificate2);
       
        ppp.addRecipient(recip1);
        ppp.addRecipient(recip2);
       
        doc.protect(ppp);               
        doc.save(output.getAbsolutePath());       
        doc.close();
       
        /* open first time */
       
        PDDocument docOpen1 = PDDocument.load(output);
       
        KeyStore ks1 = KeyStore.getInstance("PKCS12");       
        ks1.load(new FileInputStream(privateCert1), password1.toCharArray());           
        PublicKeyDecryptionMaterial pdm = new PublicKeyDecryptionMaterial(ks1, null, password1);       
        docOpen1.openProtection(pdm);       
        docOpen1.close();

        /* open second time */
       
        PDDocument docOpen2 = PDDocument.load(output);
       
        KeyStore ks2 = KeyStore.getInstance("PKCS12");       
        ks2.load(new FileInputStream(privateCert2), password2.toCharArray());           
        PublicKeyDecryptionMaterial pdm2 = new PublicKeyDecryptionMaterial(ks2, null, password2);       
        docOpen2.openProtection(pdm2);       
        docOpen2.close();
               
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

    {
        System.out.println("Preparing to parse " + file.getName());
       
        OutputStream os = null;
        Writer writer = null;
        PDDocument document = null;
        try
        {
            document = PDDocument.load(file);

            File outFile = new File(file.getParentFile().getParentFile(), "output/" + file.getName() + ".txt");
            os = new FileOutputStream(outFile);
            os.write( 0xFF );
            os.write( 0xFE );
            writer = new OutputStreamWriter(os,"UTF-16LE");

            stripper.writeText(document, writer);



            if (bLogResult)
            {
                System.out.println("Text for " + file.getName() + ":\r\n" + stripper.getText(document));
            }

            File expectedFile = new File(file.getParentFile().getParentFile(), "input/" + file.getName() + ".txt");
            File actualFile = new File(file.getParentFile().getParentFile(), "output/" + file.getName() + ".txt");

            if (!expectedFile.exists())
            {
                this.bFail = true;
                System.err.println(
                    "FAILURE: Input verification file: " + expectedFile.getAbsolutePath() +
                    " did not exist");
                return;
            }

            LineNumberReader expectedReader =
                new LineNumberReader(new InputStreamReader(new FileInputStream(expectedFile),"UTF-16"));
            LineNumberReader actualReader =
                new LineNumberReader(new InputStreamReader(new FileInputStream(actualFile), "UTF-16"));

            while (true)
            {
                String expectedLine = expectedReader.readLine();
                while( expectedLine != null && expectedLine.trim().length() == 0 )
                {
                    expectedLine = expectedReader.readLine();
                }
                String actualLine = actualReader.readLine();
                while( actualLine != null && actualLine.trim().length() == 0 )
                {
                    actualLine = actualReader.readLine();
                }
                if (!stringsEqual(expectedLine, actualLine))
                {
                    this.bFail = true;
                    System.err.println("FAILURE: Line mismatch for file " + file.getName() +
                              " at expected line: " + expectedReader.getLineNumber() +
                              " at actual line: " + actualReader.getLineNumber() +
                              "\r\n  expected line was: \"" + expectedLine + "\"" +
                              "\r\n  actual line was:   \"" + actualLine + "\"");
                    //lets report all lines, even though this might produce some verbose logging
                    //break;
                }

                if( expectedLine == null || actualLine==null)
                {
                    break;
                }
            }
        }
        finally
        {
            if( writer != null )
            {
                writer.close();
            }
            if( os != null )
            {
                os.close();
            }
            if( document != null )
            {
                document.close();
            }
        }
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

     *
     * @throws Exception If there is an exception while encrypting.
     */
    public void testFDFfdeb() throws Exception
    {
        PDDocument fdeb = null;
        try
        {
            fdeb = PDDocument.load( "test/input/fdeb.pdf" );
            PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm();
            PDTextbox field = (PDTextbox)form.getField( "f67_1" );
            field.setValue( "2" );
           
            String expected =
                "/Tx BMC " +
                "BT " +
                "/Helv 9 Tf " +
                " 0 g " +
                " 2 1.985585 Td " +
                "2.07698 0 Td " +
                "(2) Tj " +
                "ET " +
                "EMC";
           
            testContentStreams( fdeb, field, expected );
        }
        finally
        {
            if( fdeb != null )
            {
                fdeb.close();
            }
        }
       
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

     *
     * @throws Exception If there is an exception while encrypting.
     */
    public void testFDFPDFWithLotsOfFields() throws Exception
    {
        PDDocument fdeb = null;
        try
        {
            fdeb = PDDocument.load( "test/input/pdf_with_lots_of_fields.pdf" );
            PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm();
            PDTextbox feld2 = (PDTextbox)form.getField( "Feld.2" );
            feld2.setValue( "Benjamin" );
           
            String expected =
            "1 1 0.8000000119 rg " +
            " 0 0 127.5 19.8299999237 re " +
            " f " +
            " 0 0 0 RG " +
            " 1 w " +
            " 0.5 0.5 126.5 18.8299999237 re " +
            " S " +
            " 0.5 g " +
            " 1 1 m " +
            " 1 18.8299999237 l " +
            " 126.5 18.8299999237 l " +
            " 125.5 17.8299999237 l " +
            " 2 17.8299999237 l " +
            " 2 2 l " +
            " 1 1 l " +
            " f " +
            " 0.75 g " +
            " 1 1 m " +
            " 126.5 1 l " +
            " 126.5 18.8299999237 l " +
            " 125.5 17.8299999237 l " +
            " 125.5 2 l " +
            " 2 2 l " +
            " 1 1 l " +
            " f " +
            " /Tx BMC  " +
            "BT " +
            "/Helv 14 Tf " +
            " 0 0 0 rg " +
            " 4 4.721 Td " +
            "(Benjamin) Tj " +
            "ET " +
            "EMC";
           
            testContentStreams( fdeb, feld2, expected );
           
            PDRadioCollection feld3 = (PDRadioCollection)form.getField( "Feld.3" );
            feld3.setValue("RB1");
            assertEquals( "RB1", feld3.getValue() );
            //assertEquals( ((PDCheckbox)feld3.getKids().get( 0 )).getValue(), "RB1" );
           
        }
        finally
        {
            if( fdeb != null )
            {
                fdeb.close();
            }
        }
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

    {

        PDFTextStripper stripper = new PDFTextStripper();
        OutputStream os = null;
        Writer writer = null;
        PDDocument document = null;
        try
        {
            document = PDDocument.load(file);

            File outFile = new File(file.getParentFile().getParentFile(), "output/" + file.getName() + ".txt");
            os = new FileOutputStream(outFile);
            writer = new OutputStreamWriter(os);

            stripper.writeText(document, writer);
        }
        finally
        {
            if( writer != null )
            {
                writer.close();
            }
            if( os != null )
            {
                os.close();
            }
            if( document != null )
            {
                document.close();
            }
        }
    }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

     *
     * @throws Exception If there is an error while testing.
     */
    public void testFDFFreedomExpressions() throws Exception
    {
        PDDocument freedom = null;
        FDFDocument fdf = null;
        try
        {
            freedom = PDDocument.load( "test/input/FreedomExpressions.pdf" );
            fdf = FDFDocument.load( "test/input/FreedomExpressions.fdf" );
            PDAcroForm form = freedom.getDocumentCatalog().getAcroForm();
            form.importFDF( fdf );
            PDTextbox feld2 = (PDTextbox)form.getField( "eeFirstName" );
            List kids = feld2.getKids();
            PDField firstKid = (PDField)kids.get( 0 );
            PDField secondKid = (PDField)kids.get( 1 );
            testContentStreamContains( freedom, firstKid, "Steve" );
            testContentStreamContains( freedom, secondKid, "Steve" );
           
            //the appearance stream is suppose to be null because there
            //is an F action in the AA dictionary that populates that field.
            PDField totalAmt = form.getField( "eeSuppTotalAmt" );
            assertTrue( totalAmt.getDictionary().getDictionaryObject( "AP" ) == null );
           
        }
        finally
        {
            if( freedom != null )
            {
                freedom.close();
            }
            if( fdf != null )
            {
                fdf.close();
            }
View Full Code Here

Examples of org.pdfbox.pdmodel.PDDocument

    private void readPDFFile(String file) throws Exception
    {
        InputStream input = null;
        File f = new File( file );
        input = new FileInputStream(f);
        PDDocument document = parseDocument( input );
        TreeModel model=new PDFTreeModel(document);
        jTree1.setModel(model);
        setTitle( "PDFBox - " + f.getAbsolutePath() );
        /*
        List pages = document.getDocumentCatalog().getAllPages();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.