Example usage for org.apache.pdfbox.pdmodel PDPageTree getCount

List of usage examples for org.apache.pdfbox.pdmodel PDPageTree getCount

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPageTree getCount.

Prototype

public int getCount() 

Source Link

Document

Returns the number of leaf nodes (page objects) that are descendants of this root within the page tree.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureDesigner.java

License:EUPL

/**
 * Each page of document can be different sizes.
 * /*  w  w  w .  j a v  a  2  s .c o m*/
 * @param document
 * @param page
 */
private void calculatePageSize(PDDocument document, int page, boolean newpage) {

    if (page < 1) {
        throw new IllegalArgumentException("First page of pdf is 1, not " + page);
    }

    PDPageTree pages = document.getDocumentCatalog().getPages();
    if (newpage) {
        PDPage lastPage = (PDPage) pages.get(pages.getCount() - 1);
        PDRectangle mediaBox = lastPage.getMediaBox();
        pageRotation = lastPage.getRotation() % 360;
        if (pageRotation == 90 || pageRotation == 270) {
            this.pageHeight(mediaBox.getWidth());
            this.pageWidth = mediaBox.getHeight();
        } else {
            this.pageHeight(mediaBox.getHeight());
            this.pageWidth = mediaBox.getWidth();
        }
    } else {
        PDPage firstPage = (PDPage) pages.get(page - 1);
        PDRectangle mediaBox = firstPage.getMediaBox();
        pageRotation = firstPage.getRotation() % 360;
        if (pageRotation == 90 || pageRotation == 270) {
            this.pageHeight(mediaBox.getWidth());
            this.pageWidth = mediaBox.getHeight();
        } else {
            this.pageHeight(mediaBox.getHeight());
            this.pageWidth = mediaBox.getWidth();
        }
    }
    float x = this.pageWidth;
    float y = 0;
    this.pageWidth = this.pageWidth + y;
    float tPercent = (100 * y / (x + y));
    this.imageSizeInPercents = 100 - tPercent;
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureProperties.java

License:EUPL

public PDFAsVisualSignatureProperties(ISettings settings, PDFBOXObject object, PdfBoxVisualObject visObj,
        PositioningInstruction pos, SignatureProfileSettings signatureProfileSettings) {
    this.settings = settings;
    this.signatureProfileSettings = signatureProfileSettings;
    try {/*from w ww .  ja  va  2  s.  c  o m*/
        main = visObj.getTable();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    this.rotationAngle = pos.getRotation();
    try {
        origDoc = object.getDocument();

        designer = new PDFAsVisualSignatureDesigner(origDoc, pos.getPage(), this, pos.isMakeNewPage());
        PDPageTree pages = origDoc.getDocumentCatalog().getPages();
        PDPage page = null;
        if (pos.isMakeNewPage()) {
            page = (PDPage) pages.get(pages.getCount() - 1);
        } else {
            page = (PDPage) pages.get(pos.getPage() - 1);
        }
        logger.debug("PAGE width {} HEIGHT {}", designer.getPageWidth(), designer.getPageHeight());
        logger.debug("POS X {} Y {}", pos.getX(), pos.getY());
        int rot = page.getRotation();
        float posy = designer.getPageHeight() - pos.getY();
        float posx = pos.getX();
        /*switch (rot) {
        case 90: // CW
           posx = designer.getPageHeight() - pos.getY();
           posy = designer.getPageWidth() - main.getWidth();
           break;
        case 180:
           posy = pos.getY();
           posx = designer.getPageWidth() - pos.getX();
           break;
        case 270: // CCW
           posx = pos.getY();
           posy = designer.getPageWidth() - pos.getX();
           break;
        }*/
        logger.debug("ROT {}", rot);
        logger.debug("COORD X {} Y {}", posx, posy);
        designer.coordinates(posx, posy);
        float[] form_rect = new float[] { 0, 0, main.getWidth() + 2, main.getHeight() + 2 };
        logger.debug("AP Rect: {} {} {} {}", form_rect[0], form_rect[1], form_rect[2], form_rect[3]);
        designer.formaterRectangleParams(form_rect);
        //this.setPdVisibleSignature(designer);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

From source file:com.formkiq.core.service.generator.pdfbox.PdfEditorServiceImpl.java

License:Apache License

/**
 * Generate {@link Map} {@link COSDictionary} to Page Numbers.
 * @param doc {@link PDDocument}/*from w ww  .j  a  v  a  2 s  .c  o m*/
 * @return {@link Map} of {@link COSDictionary} to {@link Integer}
 * @throws IOException IOException
 */
private Map<COSDictionary, Integer> getCOSDictionaryToPageNumberMap(final PDDocument doc) throws IOException {

    Map<COSDictionary, Integer> map = new HashMap<>();

    PDPageTree pages = doc.getPages();
    for (int i = 0; i < pages.getCount(); i++) {
        for (PDAnnotation annotation : pages.get(i).getAnnotations()) {
            COSDictionary annotationObject = annotation.getCOSObject();
            map.put(annotationObject, Integer.valueOf(i));
        }
    }

    return map;
}

From source file:com.formkiq.core.service.generator.pdfbox.PdfEditorServiceImpl.java

License:Apache License

@Override
public Pair<FormJSON, List<WorkflowOutputFormField>> getOutputFormFields(final String filename,
        final byte[] data) throws IOException {

    List<WorkflowOutputFormField> wofields = new ArrayList<>();

    PDDocument doc = loadPDF(data);//w  w  w . jav  a 2 s . c o m

    try {

        Map<COSDictionary, Integer> obMap = getCOSDictionaryToPageNumberMap(doc);

        Map<Integer, List<PDField>> pdMap = getPDFields(doc, obMap);

        Map<Integer, List<PdfTextField>> textsMap = getTextMap(doc);

        PDPageTree pages = doc.getDocumentCatalog().getPages();

        FormJSON form = buildFormJSON(doc, textsMap.get(Integer.valueOf(0)));

        for (int i = 0; i < pages.getCount(); i++) {

            PDPage page = pages.get(i);
            Integer pageNum = Integer.valueOf(i);

            List<PDField> fields = pdMap.getOrDefault(pageNum, emptyList());

            List<PdfTextField> texts = getTextForPage(textsMap, pageNum);

            List<PDRectangle> lineRects = getPageLinePaths(pages.get(i));

            Map<PDField, FormJSONField> fieldMap = buildFormSection(form, page, fields, texts, lineRects);

            List<WorkflowOutputFormField> outfields = createFieldOutputs(form, fields, fieldMap);

            wofields.addAll(outfields);
        }

        return Pair.of(form, wofields);

    } finally {
        doc.close();
    }
}

From source file:com.liferay.portlet.documentlibrary.util.LiferayPDFBoxConverter.java

License:Open Source License

public void generateImagesPB() throws Exception {
    PDDocument pdDocument = null;/*from  w w w  .j a va2 s  .  c o m*/

    try {
        pdDocument = PDDocument.load(_inputFile);

        PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog();

        PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);

        PDPageTree pdPages = pdDocumentCatalog.getPages();

        for (int i = 0; i < pdPages.getCount(); i++) {
            if (_generateThumbnail && (i == 0)) {
                _generateImagesPB(pdfRenderer, i, _thumbnailFile, _thumbnailExtension);
            }

            if (!_generatePreview) {
                break;
            }

            _generateImagesPB(pdfRenderer, i, _previewFiles[i], _extension);
        }
    } finally {
        if (pdDocument != null) {
            pdDocument.close();
        }
    }
}

From source file:eu.europa.esig.dss.pades.signature.PAdESVisibleSignaturePositionTest.java

License:Open Source License

private void checkImageSimilarityPdf(String samplePdf, String checkPdf, float similarity) throws IOException {
    DSSDocument document = sign(signablePdfs.get(samplePdf));
    PDDocument sampleDocument = PDDocument.load(document.openStream());
    PDDocument checkDocument = PDDocument
            .load(getClass().getResourceAsStream("/visualSignature/check/" + checkPdf));

    PDPageTree samplePageTree = sampleDocument.getPages();
    PDPageTree checkPageTree = checkDocument.getPages();

    Assert.assertEquals(checkPageTree.getCount(), samplePageTree.getCount());

    PDFRenderer sampleRenderer = new PDFRenderer(sampleDocument);
    PDFRenderer checkRenderer = new PDFRenderer(checkDocument);

    for (int pageNumber = 0; pageNumber < checkPageTree.getCount(); pageNumber++) {
        BufferedImage sampleImage = sampleRenderer.renderImageWithDPI(pageNumber, DPI);
        BufferedImage checkImage = checkRenderer.renderImageWithDPI(pageNumber, DPI);

        float checkSimilarity = checkImageSimilarity(sampleImage, checkImage, CHECK_RESOLUTION);
        float calculatedSimilarity = ((int) (similarity * 100f)) / 100f; // calulate rotated position has about 1
        // pixel position difference
        Assert.assertTrue(checkSimilarity >= calculatedSimilarity);
    }//w w w . j  a  va  2  s  . c  o m
}