Package org.apache.pdfbox.pdmodel

Examples of org.apache.pdfbox.pdmodel.PDDocument


        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                if( document.isEncrypted() )
                {
                    try
                    {
                        StandardDecryptionMaterial sdm = new StandardDecryptionMaterial("");
                        document.openProtection(sdm);
                    }
                    catch( InvalidPasswordException e )
                    {
                        System.err.println( "Error: Document is encrypted with a password." );
                        System.exit( 1 );
                    }
                }
                PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                stripper.setSortByPosition( true );
                Rectangle rect = new Rectangle( 10, 280, 275, 60 );
                stripper.addRegion( "class1", rect );
                PDPage firstPage = document.getPage(0);
                stripper.extractRegions( firstPage );
                System.out.println( "Text in the area:" + rect );
                System.out.println( stripper.getTextForRegion( "class1" ) );

            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here


        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                if( document.isEncrypted() )
                {
                    System.err.println( "Error: Encrypted documents are not supported for this example." );
                    System.exit( 1 );
                }
                for( PDPage page : document.getPages() )
                {
                    PDFStreamParser parser = new PDFStreamParser(page.getStream());
                    parser.parse();
                    List tokens = parser.getTokens();
                    List newTokens = new ArrayList();
                    for( int j=0; j<tokens.size(); j++)
                    {
                        Object token = tokens.get( j );
                        if( token instanceof Operator)
                        {
                            Operator op = (Operator)token;
                            if( op.getName().equals( "TJ") || op.getName().equals( "Tj" ))
                            {
                                //remove the one argument to this operator
                                newTokens.remove( newTokens.size() -1 );
                                continue;
                            }
                        }
                        newTokens.add( token );

                    }
                    PDStream newContents = new PDStream( document );
                    ContentStreamWriter writer = new ContentStreamWriter( newContents.createOutputStream() );
                    writer.writeTokens( newTokens );
                    newContents.addCompression();
                    page.setContents( newContents );
                }
                document.save( args[1] );
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

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

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

            PDPage page = new PDPage();
            doc.addPage(page);
            PDFont font = createPDType1AfmPfbFont(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

     *
     * @throws IOException If there is an error parsing the document.
     */
    private void addContent(Document document, InputStream is, String documentLocation) throws IOException
    {
        PDDocument pdfDocument = null;
        try
        {
            if (useNonSeqParser)
            {
                pdfDocument = PDDocument.loadNonSeq(is, "");
            }
            else
            {
                pdfDocument = PDDocument.load(is);

                if (pdfDocument.isEncrypted())
                {
                    // Just try using the default password and move on
                    StandardDecryptionMaterial sdm = new StandardDecryptionMaterial("");
                    pdfDocument.openProtection(sdm);
                }
            }

            // create a writer where to append the text content.
            StringWriter writer = new StringWriter();
            if (stripper == null)
            {
                stripper = new PDFTextStripper();
            }
            stripper.writeText(pdfDocument, writer);

            // Note: the buffer to string operation is costless;
            // the char array value of the writer buffer and the content string
            // is shared as long as the buffer content is not modified, which will
            // not occur here.
            String contents = writer.getBuffer().toString();

            StringReader reader = new StringReader(contents);

            // Add the tag-stripped contents as a Reader-valued Text field so it will
            // get tokenized and indexed.
            addTextField(document, "contents", reader);

            PDDocumentInformation info = pdfDocument.getDocumentInformation();
            if (info != null)
            {
                addTextField(document, "Author", info.getAuthor());
                try
                {
                    addTextField(document, "CreationDate", info.getCreationDate());
                }
                catch (IOException io)
                {
                    // ignore, bad date but continue with indexing
                }
                addTextField(document, "Creator", info.getCreator());
                addTextField(document, "Keywords", info.getKeywords());
                try
                {
                    addTextField(document, "ModificationDate", info.getModificationDate());
                }
                catch (IOException io)
                {
                    // ignore, bad date but continue with indexing
                }
                addTextField(document, "Producer", info.getProducer());
                addTextField(document, "Subject", info.getSubject());
                addTextField(document, "Title", info.getTitle());
                addTextField(document, "Trapped", info.getTrapped());
            }
            int summarySize = Math.min(contents.length(), 500);
            String summary = contents.substring(0, summarySize);
            // Add the summary as an UnIndexed field, so that it is stored and returned
            // with hit documents for display.
            addUnindexedField(document, "summary", summary);
        }
        catch (InvalidPasswordException e)
        {
            // they didn't suppply a password and the default of "" was wrong.
            throw new IOException("Error: The document(" + documentLocation + ") is encrypted and will not be indexed.");
        }
        finally
        {
            if (pdfDocument != null)
            {
                pdfDocument.close();
            }
        }
    }
View Full Code Here

        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                if( document.isEncrypted() )
                {
                    try
                    {
                        StandardDecryptionMaterial sdm = new StandardDecryptionMaterial("");
                        document.openProtection(sdm);
                    }
                    catch( InvalidPasswordException e )
                    {
                        System.err.println( "Error: Document is encrypted with a password." );
                        System.exit( 1 );
                    }
                }
                PrintImageLocations printer = new PrintImageLocations();
                int pageNum = 0;
                for( PDPage page : document.getPages() )
                {
                    pageNum++;
                    System.out.println( "Processing page: " + pageNum );
                    printer.processPage(page);
                }
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

     *
     * @throws IOException If there is an error writing the data.
     */
    public void create( String file ) throws IOException
    {
        PDDocument document = null;
        try
        {
            document = new PDDocument();
            //Every document requires at least one page, so we will add one
            //blank page.
            PDPage blankPage = new PDPage();
            document.addPage( blankPage );
            document.save( file );
        }
        finally
        {
            if( document != null )
            {
                document.close();
            }
        }
    }
View Full Code Here

        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                PDActionJavaScript javascript = new PDActionJavaScript(
                    "app.alert( {cMsg: 'PDFBox rocks!', nIcon: 3, nType: 0, cTitle: 'PDFBox Javascript example' } );");
                document.getDocumentCatalog().setOpenAction( javascript );
                if( document.isEncrypted() )
                {
                    throw new IOException( "Encrypted documents are not supported for this example" );
                }
                document.save( args[1] );
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

     * @throws IOException if something went wrong
     */
    public void overlay(Map<Integer, String> specificPageOverlayFile, boolean useNonSeqParser)
            throws IOException
    {
        PDDocument sourcePDFDocument = null;
        PDDocument defaultOverlay = null;
        PDDocument firstPageOverlay = null;
        PDDocument lastPageOverlay = null;
        PDDocument allPagesOverlay = null;
        PDDocument oddPageOverlay = null;
        PDDocument evenPageOverlay = null;
        try
        {
            sourcePDFDocument = loadPDF(inputFileName,useNonSeqParser);
            if (defaultOverlayFilename != null)
            {
                defaultOverlay = loadPDF(defaultOverlayFilename, useNonSeqParser);
                defaultOverlayPage = getLayoutPage(defaultOverlay);
            }
            if (firstPageOverlayFilename != null)
            {
                firstPageOverlay = loadPDF(firstPageOverlayFilename, useNonSeqParser);
                firstPageOverlayPage = getLayoutPage(firstPageOverlay);
            }
            if (lastPageOverlayFilename != null)
            {
                lastPageOverlay = loadPDF(lastPageOverlayFilename, useNonSeqParser);
                lastPageOverlayPage = getLayoutPage(lastPageOverlay);
            }
            if (oddPageOverlayFilename != null)
            {
                oddPageOverlay = loadPDF(oddPageOverlayFilename, useNonSeqParser);
                oddPageOverlayPage = getLayoutPage(oddPageOverlay);
            }
            if (evenPageOverlayFilename != null)
            {
                evenPageOverlay = loadPDF(evenPageOverlayFilename, useNonSeqParser);
                evenPageOverlayPage = getLayoutPage(evenPageOverlay);
            }
            if (allPagesOverlayFilename != null)
            {
                allPagesOverlay = loadPDF(allPagesOverlayFilename, useNonSeqParser);
                specificPageOverlayPage = getLayoutPages(allPagesOverlay);
                useAllOverlayPages = true;
                numberOfOverlayPages = specificPageOverlayPage.size();
            }
            for (Map.Entry<Integer, String> e : specificPageOverlayFile.entrySet())
            {
                PDDocument doc = loadPDF(e.getValue(), useNonSeqParser);
                specificPageOverlay.put(e.getKey(), doc);
                specificPageOverlayPage.put(e.getKey(), getLayoutPage(doc));
            }
            processPages(sourcePDFDocument);

View Full Code Here

        }
    }

    private PDDocument loadPDF(String pdfName, boolean useNonSeqParser) throws IOException
    {
        PDDocument pdf = null;
        if (useNonSeqParser)
        {
            pdf = PDDocument.loadNonSeq(new File(pdfName), null);
        }
        else
View Full Code Here

     * @throws IOException If there is an error writing the data.
     */
    public void createPDFFromImage( String file, String image) throws IOException
    {
        // the document
        PDDocument doc = null;
        try
        {
            doc = new PDDocument();

            PDPage page = new PDPage();
            doc.addPage( page );

            PDImageXObject pdImage;
            if( image.toLowerCase().endsWith( ".jpg" ) )
            {
                pdImage = JPEGFactory.createFromStream(doc, new FileInputStream(image));
            }
            else if (image.toLowerCase().endsWith(".tif") ||
                    image.toLowerCase().endsWith(".tiff"))
            {
                pdImage = CCITTFactory.createFromRandomAccess(doc, new RandomAccessFile(new File(image),"r"));
            }
            else if (image.toLowerCase().endsWith(".gif") ||
                    image.toLowerCase().endsWith(".bmp") ||
                    image.toLowerCase().endsWith(".png"))
            {
                BufferedImage bim = ImageIO.read(new File(image));
                pdImage = LosslessFactory.createFromImage(doc, bim);
            }
            else
            {
                throw new IOException( "Image type not supported: " + image );
            }
            PDPageContentStream contentStream = new PDPageContentStream(doc, page);

            //contentStream.drawImage(pdImage, 20, 20 );
            // better method inspired by http://stackoverflow.com/a/22318681/535646
            float scale = 1f; // reduce this value if the image is too large
            contentStream.drawXObject(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale);

            contentStream.close();
            doc.save( file );
        }
        finally
        {
            if( doc != null )
            {
                doc.close();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.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.