Package org.pdfbox.pdmodel

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


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

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

    {

        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

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

    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

     *
     * @throws IOException If there is an error parsing the document.
     */
    private static PDDocument parseDocument( InputStream input )throws IOException
    {
        PDDocument document = PDDocument.load( input );
        if( document.isEncrypted() )
        {
            try
            {
                document.decrypt( "" );
            }
            catch( InvalidPasswordException e )
            {
                System.err.println( "Error: The document is encrypted." );
            }
View Full Code Here

            {
                password = "";
            }
           

            PDDocument document = null;

            try
            {
                document = PDDocument.load( infile );

                if( document.isEncrypted() )
                {
                    DecryptionMaterial decryptionMaterial = null;
                    if( keyStore != null )
                    {
                        KeyStore ks = KeyStore.getInstance("PKCS12");      
                        ks.load(new FileInputStream(keyStore), password.toCharArray());
                           
                        decryptionMaterial = new PublicKeyDecryptionMaterial(ks, alias, password);
                    }
                    else
                    {
                        decryptionMaterial = new StandardDecryptionMaterial(password);
                    }
                    document.openProtection(decryptionMaterial);
                    AccessPermission ap = document.getCurrentAccessPermission();
                    if(ap.isOwnerPermission())
                    {
                        document.save( outfile );
                    }
                    else
                    {
                        throw new IOException(
                        "Error: You are only allowed to decrypt a document with the owner password." );
                    }
                }
                else
                {
                    System.err.println( "Error: Document is not encrypted." );
                }
            }           
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

     * @throws IOException If there is an error generating the highlight file.
     */
    public static void main(String[] args) throws IOException
    {
        PDFHighlighter xmlExtractor = new PDFHighlighter();
        PDDocument doc = null;
        try
        {
            if( args.length < 2 )
            {
                usage();
            }
            String[] highlightStrings = new String[ args.length - 1];
            System.arraycopy( args, 1, highlightStrings, 0, highlightStrings.length );
            doc = PDDocument.load( args[0] );
           
            xmlExtractor.generateXMLHighlight(
                doc,
                highlightStrings,
                new OutputStreamWriter( System.out ) );
        }
        finally
        {
            if( doc != null )
            {
                doc.close();
            }
        }
    }
View Full Code Here

            if(outputPrefix == null)
            {
                outputPrefix = pdfFile.substring( 0, pdfFile.lastIndexOf( '.' ));
            }
   
            PDDocument document = null;
            try
            {
                document = PDDocument.load( pdfFile );
   
   
                //document.print();
                if( document.isEncrypted() )
                {
                    try
                    {
                        document.decrypt( password );
                    }
                    catch( InvalidPasswordException e )
                    {
                        if( args.length == 4 )//they supplied the wrong password
                        {
                            System.err.println( "Error: The supplied password is incorrect." );
                            System.exit( 2 );
                        }
                        else
                        {
                            //they didn't suppply a password and the default of "" was wrong.
                            System.err.println( "Error: The document is encrypted." );
                            usage();
                        }
                    }
                }
                List pages = document.getDocumentCatalog().getAllPages();
                for( int i=startPage-1; i<endPage && i<pages.size(); i++ )
                {
                    ImageOutputStream output = null;
                    ImageWriter imageWriter = null;
                    try
                    {
                        PDPage page = (PDPage)pages.get( i );
                        BufferedImage image = page.convertToImage();
                        String fileName = outputPrefix + (i+1) + "." + imageType;
                        System.out.println( "Writing:" + fileName );
                        output = ImageIO.createImageOutputStream( new File( fileName ) );
                       
                        boolean foundWriter = false;
                        Iterator writerIter = ImageIO.getImageWritersByFormatName( imageType );
                        while( writerIter.hasNext() && !foundWriter )
                        {
                            try
                            {
                                imageWriter = (ImageWriter)writerIter.next();
                                ImageWriteParam writerParams = imageWriter.getDefaultWriteParam();
                                if(writerParams.canWriteCompressed() )
                                {
                                    writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                                    writerParams.setCompressionQuality(1.0f);
                                }
                               
                               
                                imageWriter.setOutput( output );
                                imageWriter.write( null, new IIOImage( image, null, null), writerParams );
                                foundWriter = true;
                            }
                            catch( IIOException io )
                            {
                                //ignore exception
                            }
                            finally
                            {
                                if( imageWriter != null )
                                {
                                    imageWriter.dispose();
                                }
                            }
                        }
                        if( !foundWriter )
                        {
                            throw new RuntimeException( "Error: no writer found for image type '" + imageType + "'" );
                        }
                    }
                    finally
                    {
                        if( output != null )
                        {
                            output.flush();
                            output.close();
                        }
                    }
                }
            }
            finally
            {
                if( document != null )
                {
                    document.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.