Java Code Examples for org.apache.pdfbox.pdmodel.PDPage#getContents()

The following examples show how to use org.apache.pdfbox.pdmodel.PDPage#getContents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorsProcessor.java    From asciidoctorj-pdf with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a document extracting the colors for the specified words in
 * the constructor
 *
 * @param filename PDF document path
 */
public void parse (String filename) throws IOException {
    PDDocument document = null;
    try {
        document = PDDocument.load(filename, false);
        List allPages = document.getDocumentCatalog().getAllPages();
        for( int i=0; i<allPages.size(); i++ ) {
            PDPage page = (PDPage)allPages.get( i );
            PDStream contents = page.getContents();
            if (contents != null) {
                processStream( page, page.getResources(),
                    page.getContents().getStream() );
            }
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
 
Example 2
Source File: ColorsProcessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a document extracting the colors for the specified words in
 * the constructor
 *
 * @param filename PDF document path
 */
public void parse (String filename) throws IOException {
    PDDocument document = null;
    try {
        document = PDDocument.load(filename, false);
        List allPages = document.getDocumentCatalog().getAllPages();
        for( int i=0; i<allPages.size(); i++ ) {
            PDPage page = (PDPage)allPages.get( i );
            PDStream contents = page.getContents();
            if (contents != null) {
                processStream( page, page.getResources(),
                    page.getContents().getStream() );
            }
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
 
Example 3
Source File: LayerUtility.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Imports a page from some PDF file as a Form XObject so it can be placed on another page
 * in the target document.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to
 * make sure that the graphics state is reset.
 * 
 * @param sourceDoc the source PDF document that contains the page to be copied
 * @param page the page in the source PDF document to be copied
 * @return a Form XObject containing the original page's content
 * @throws IOException if an I/O error occurs
 */
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException
{
    importOcProperties(sourceDoc);

    PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE);
    PDFormXObject form = new PDFormXObject(newStream);

    //Copy resources
    PDResources pageRes = page.getResources();
    PDResources formRes = new PDResources();
    cloner.cloneMerge(pageRes, formRes);
    form.setResources(formRes);

    //Transfer some values from page to form
    transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true);

    Matrix matrix = form.getMatrix();
    AffineTransform at = matrix.createAffineTransform();
    PDRectangle mediaBox = page.getMediaBox();
    PDRectangle cropBox = page.getCropBox();
    PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);

    //Handle the /Rotation entry on the page dict
    int rotation = page.getRotation();

    //Transform to FOP's user space
    //at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
    at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(),
            mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
    switch (rotation)
    {
    case 90:
        at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
        at.translate(0, viewBox.getWidth());
        at.rotate(-Math.PI / 2.0);
        break;
    case 180:
        at.translate(viewBox.getWidth(), viewBox.getHeight());
        at.rotate(-Math.PI);
        break;
    case 270:
        at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
        at.translate(viewBox.getHeight(), 0);
        at.rotate(-Math.PI * 1.5);
        break;
    default:
        //no additional transformations necessary
    }
    //Compensate for Crop Boxes not starting at 0,0
    at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
    if (!at.isIdentity())
    {
        form.setMatrix(at);
    }

    BoundingBox bbox = new BoundingBox();
    bbox.setLowerLeftX(viewBox.getLowerLeftX());
    bbox.setLowerLeftY(viewBox.getLowerLeftY());
    bbox.setUpperRightX(viewBox.getUpperRightX());
    bbox.setUpperRightY(viewBox.getUpperRightY());
    form.setBBox(new PDRectangle(bbox));

    return form;
}
 
Example 4
Source File: TestGraphicsCounter.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28321374/how-to-get-page-content-height-using-pdfbox">
 * How to get page content height using pdfbox
 * </a>
 * <br/>
 * <a href="https://drive.google.com/file/d/0B65bQnJhC1mvbEVQQ0o0QU9STlU/view?usp=sharing">
 * test.pdf
 * </a>, here as <code>test-rivu.pdf</code>
 * <p>
 * Rivu's code from a comment to count lines etc.
 * </p>
 */
@Test
public void testCountTestLikeRivu() throws IOException
{
    try (InputStream resource = getClass().getResourceAsStream("test-rivu.pdf"))
    {
        System.out.println("test-rivu.pdf");
        PDDocument document = Loader.loadPDF(resource);

        PDPage page = document.getPage(4);
        PDFStreamParser parser = new PDFStreamParser(page.getContents());
        List<Object> tokens = parser.parse();
        int lines=0;
        int curves=0;
        int rectangles=0;
        int doOps=0;
        int clipPaths=0;
        for (Object token:tokens){
            if (token instanceof Operator) {
                Operator op=(Operator) token;
                if ("do".equals(op.getName()))
                    doOps+=1;
                else if ("W".equals(op.getName())|| "W*".equals(op.getName()))
                    clipPaths+=1;
                else if ("l".equals(op.getName()) || "h".equals(op.getName()))
                    lines+=1;
                else if ("c".equals(op.getName())||"y".equals(op.getName()) ||"v".equals(op.getName())){
                    System.out.println(op);
                    curves+=1;
                }
                else if ("re".equals(op.getName()))
                    rectangles+=1;


            }
        }
        System.out.println(lines + " lines, " + curves + " curves, " + rectangles + " rectangles, " + doOps + " xobjects, " + clipPaths + " clip paths");

        document.close();
    }
}