Package org.pdfbox.pdmodel

Examples of org.pdfbox.pdmodel.PDDocument


        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                if( document.isEncrypted() )
                {
                    System.err.println( "Error: Cannot add bookmark destination to encrypted documents." );
                    System.exit( 1 );
                }
               
                List pages = document.getDocumentCatalog().getAllPages();
                if( pages.size() < 2 )
                {
                    throw new IOException( "Error: The PDF must have at least 2 pages.");
                }
                PDDocumentOutline bookmarks = document.getDocumentCatalog().getDocumentOutline();
                if( bookmarks == null )
                {
                    throw new IOException( "Error: The PDF does not contain any bookmarks" );
                }
                PDOutlineItem item = bookmarks.getFirstChild().getNextSibling();
                PDDestination dest = item.getDestination();
                PDActionGoTo action = new PDActionGoTo();
                action.setDestination(dest);        
                document.getDocumentCatalog().setOpenAction(action);
               
                document.save( args[1] );
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here


     * @throws IOException If there is an error saving the document.
     * @throws COSVisitorException If an error occurs while saving the destination file.
     */
    public void mergeDocuments() throws IOException, COSVisitorException
    {
        PDDocument destination = null;
        File sourceFile;
        PDDocument source;
        if (sources != null && sources.size() > 0)
        {
            try
            {
                Iterator sit = sources.iterator();
                sourceFile = (File) sit.next();
                destination = PDDocument.load(sourceFile);
                while (sit.hasNext())
                {
                    sourceFile = (File) sit.next();
                    source = PDDocument.load(sourceFile);
                    try
                    {
                        appendDocument(destination, source);
                    }
                    finally
                    {
                        if (source != null)
                        {
                            source.close();
                        }
                    }
                }
                destination.save(destinationFileName);
            }
View Full Code Here

     * @throws COSVisitorException If there is an error writing the PDF.
     */
    public void doIt( String file, String message, String  outfile ) throws IOException, COSVisitorException
    {
        // the document
        PDDocument doc = null;
        try
        {
            doc = PDDocument.load( file );
           
            List allPages = doc.getDocumentCatalog().getAllPages();
            PDFont font = PDType1Font.HELVETICA_BOLD;
            float fontSize = 12.0f;
           
            for( int i=0; i<allPages.size(); i++ )
            {
                PDPage page = (PDPage)allPages.get( i );
                PDRectangle pageSize = page.findMediaBox();
                float stringWidth = font.getStringWidth( message );
                float centeredPosition = (pageSize.getWidth() - (stringWidth*fontSize)/1000f)/2f;
                PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
                contentStream.beginText();
                contentStream.setFont( font, fontSize );
                contentStream.moveTextPositionByAmount( centeredPosition, 30 );
                contentStream.drawString( message );
                contentStream.endText();
                contentStream.close();
            }
           
           
            doc.save( outfile );
        }
        finally
        {
            if( doc != null )
            {
                doc.close();
            }
        }
    }
View Full Code Here

        }
        else
        {
            String password = args[0];
            String infile = args[1];
            PDDocument document = null;
            try
            {
                document = PDDocument.load( infile );

                if( document.isEncrypted() )
                {
                    document.decrypt( password );
                }
                else
                {
                    System.err.println( "Warning: Document is not encrypted." );
                }

                COSDictionary trailer = document.getDocument().getTrailer();
                COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
                COSDictionary acroForm = (COSDictionary)root.getDictionaryObject( COSName.getPDFName( "AcroForm" ) );
                COSArray fields = (COSArray)acroForm.getDictionaryObject( COSName.getPDFName( "Fields" ) );
                for( int i=0; i<fields.size(); i++ )
                {
                    COSDictionary field = (COSDictionary)fields.getObject( i );
                    String type = field.getNameAsString( "FT" );
                    if( "Sig".equals( type ) )
                    {
                        COSDictionary cert = (COSDictionary)field.getDictionaryObject( COSName.getPDFName( "V" ) );
                        if( cert != null )
                        {
                            System.out.println( "Certificate found" );
                            System.out.println( "Name=" + cert.getDictionaryObject( COSName.getPDFName( "Name" ) ) );
                            System.out.println( "Modified=" + cert.getDictionaryObject( COSName.getPDFName( "M" ) ) );
                            COSName subFilter = (COSName)cert.getDictionaryObject( COSName.getPDFName( "SubFilter" ) );
                            if( subFilter != null )
                            {
                                if( subFilter.getName().equals( "adbe.x509.rsa_sha1" ) )
                                {
                                    COSString certString = (COSString)cert.getDictionaryObject(
                                        COSName.getPDFName( "Cert" ) );
                                    byte[] certData = certString.getBytes();
                                    CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
                                    ByteArrayInputStream certStream = new ByteArrayInputStream( certData );
                                    Collection certs = factory.generateCertificates( certStream );
                                    System.out.println( "certs=" + certs );
                                }
                                else if( subFilter.getName().equals( "adbe.pkcs7.sha1" ) )
                                {
                                    COSString certString = (COSString)cert.getDictionaryObject(
                                        COSName.getPDFName( "Contents" ) );
                                    byte[] certData = certString.getBytes();
                                    CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
                                    ByteArrayInputStream certStream = new ByteArrayInputStream( certData );
                                    Collection certs = factory.generateCertificates( certStream );
                                    System.out.println( "certs=" + certs );
                                }
                                else
                                {
                                    System.err.println( "Unknown certificate type:" + subFilter );
                                }
                            }
                            else
                            {
                                throw new IOException( "Missing subfilter for cert dictionary" );
                            }
                        }
                        else
                        {
                            System.out.println( "Signature found, but no certificate" );
                        }
                    }
                }
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

    public void doIt(final String file, final String message,
            final String fontfile) throws IOException, COSVisitorException
    {

        // the document
        PDDocument doc = null;
        try
        {
            doc = new PDDocument();

            PDPage page = new PDPage();
            doc.addPage(page);
            PDFont font = PDTrueTypeFont.loadTTF(doc, fontfile);

            PDPageContentStream contentStream = new PDPageContentStream(doc,
                    page);
            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.moveTextPositionByAmount(100, 700);
            contentStream.drawString(message);
            contentStream.endText();
            contentStream.close();
            doc.save(file);
            System.out.println(file + " created!");
        }
        finally
        {
            if (doc != null)
            {
                doc.close();
            }
        }
    }
View Full Code Here

        {
            usage();
        }
        else
        {
            PDDocument document = null;
            FileInputStream file = null;
            try
            {
                file = new FileInputStream( args[0] );
                PDFParser parser = new PDFParser( file );
                parser.parse();
                document = parser.getPDDocument();
                if( document.isEncrypted() )
                {
                    try
                    {
                        document.decrypt( "" );
                    }
                    catch( InvalidPasswordException e )
                    {
                        System.err.println( "Error: Document is encrypted with a password." );
                        System.exit( 1 );
                    }
                }
                PrintBookmarks meta = new PrintBookmarks();
                PDDocumentOutline outline =  document.getDocumentCatalog().getDocumentOutline();
                if( outline != null )
                {
                    meta.printBookmark( outline, "" );
                }
                else
                {
                    System.out.println( "This document does not contain any bookmarks" );
                }
            }
            finally
            {
                if( file != null )
                {
                    file.close();
                }
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

     *
     * @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

     * @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

     *
     * @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

    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

TOP

Related Classes of org.pdfbox.pdmodel.PDDocument

Copyright © 2018 www.massapicom. 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.