Example usage for org.apache.pdfbox.pdmodel.common PDRectangle getWidth

List of usage examples for org.apache.pdfbox.pdmodel.common PDRectangle getWidth

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.common PDRectangle getWidth.

Prototype

public float getWidth() 

Source Link

Document

This will get the width of this rectangle as calculated by upperRightX - lowerLeftX.

Usage

From source file:PDFLayoutTextStripper.java

License:Open Source License

@Override
protected void processPage(PDPage page, COSStream content) throws IOException {
    PDRectangle pageRectangle = page.findMediaBox();
    if (pageRectangle != null) {
        this.setCurrentPageWidth(pageRectangle.getWidth());
        super.processPage(page, content);
        this.previousTextPosition = null;
        this.textLineList = new ArrayList<TextLine>();
    }// ww w .j a  v  a 2 s . c om
}

From source file:airviewer.AnnotationGenerator.java

/**
 *
 * @param a/*from   w ww . j av a  2 s  . c  o  m*/
 * @param d
 * @param p
 * @param shouldResize
 * @return
 */
public static PDAppearanceStream generateSquareAppearance(PDAnnotation a, PDDocument d, PDPage p,
        boolean shouldResize) {
    assert null != a;
    assert null != d;
    assert null != p;

    PDAppearanceStream annotationAppearanceStream = null;

    try {
        if (shouldResize) {
            resizeAnnotationToContent(a);
        }

        final String contents = a.getContents();
        final boolean hasContents = null != contents && 0 < contents.length();
        float borderWidth = 0;
        if (a instanceof PDAnnotationMarkup) {
            final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle();
            if (null != a.getColor() && null != borderStyle) {
                borderWidth = Math.abs(borderStyle.getWidth());
            }
        }
        final float fontSize = FONT_SIZE_PDF_POINTS;
        final float textHeight = fontSize;
        final float margin = MARGIN_SIZE_PDF_POINTS;

        PDRectangle position = a.getRectangle();
        final float lowerLeftX = position.getLowerLeftX();
        final float lowerLeftY = position.getLowerLeftY();
        float width = position.getWidth();
        float height = position.getHeight();

        annotationAppearanceStream = new PDAppearanceStream(d);

        annotationAppearanceStream.setBBox(position);
        annotationAppearanceStream.setMatrix(new AffineTransform());
        annotationAppearanceStream.setResources(p.getResources());

        try (PDPageContentStream appearanceContent = new PDPageContentStream(d, annotationAppearanceStream)) {
            appearanceContent.transform(new Matrix()); // Identity transform

            // Rect is inset by half border width to prevent border leaking
            // outside bounding box
            final float insetLowerLeftX = lowerLeftX + borderWidth * 0.5f;
            final float insetLowerLeftY = lowerLeftY + borderWidth * 0.5f;
            final float insetWidth = width - borderWidth;
            final float insetheight = height - borderWidth;
            appearanceContent.addRect(insetLowerLeftX, insetLowerLeftY, insetWidth, insetheight);
            appearanceContent.setLineWidth(borderWidth);
            appearanceContent.setNonStrokingColor(GRAY);

            if (null != a.getColor() && 0 < borderWidth) {
                appearanceContent.setStrokingColor(a.getColor());
                appearanceContent.fillAndStroke();
            } else {
                appearanceContent.fill();

            }

            if (hasContents) {
                appearanceContent.moveTo(0, 0);
                appearanceContent.beginText();

                // Center vertically, left justified inside border with margin
                appearanceContent.newLineAtOffset(lowerLeftX + borderWidth + margin,
                        lowerLeftY + (height + LINE_SPACE_SIZE_PDF_POINTS) * 0.5f - textHeight * 0.5f);
                appearanceContent.setFont(FONT, fontSize);
                if (null != a.getColor()) {
                    appearanceContent.setNonStrokingColor(a.getColor()); // Sets color of text
                } else {
                    appearanceContent.setNonStrokingColor(BLACK); // Sets color of text

                }
                appearanceContent.showText(a.getContents());
                appearanceContent.endText();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(AnnotationGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }

    return annotationAppearanceStream;
}

From source file:airviewer.AnnotationGenerator.java

/**
 *
 * @param a/*from   w w  w  .ja v a  2s.  com*/
 * @param d
 * @param p
 * @param shouldResize
 * @return
 */
public static PDAppearanceStream generateCircleAppearance(PDAnnotation a, PDDocument d, PDPage p,
        boolean shouldResize) {
    assert null != a;
    assert null != d;
    assert null != p;

    PDAppearanceStream annotationAppearanceStream = null;

    try {
        if (shouldResize) {
            resizeAnnotationToContent(a);
        }

        final String contents = a.getContents();
        final boolean hasContents = null != contents && 0 < contents.length();
        float borderWidth = 0;
        if (a instanceof PDAnnotationMarkup) {
            final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle();
            if (null != a.getColor() && null != borderStyle) {
                borderWidth = Math.abs(borderStyle.getWidth());
            }
        }
        final float fontSize = FONT_SIZE_PDF_POINTS;
        final float textHeight = fontSize;
        final float margin = MARGIN_SIZE_PDF_POINTS;

        PDRectangle position = a.getRectangle();
        final float lowerLeftX = position.getLowerLeftX();
        final float lowerLeftY = position.getLowerLeftY();
        float width = position.getWidth();
        float height = position.getHeight();

        annotationAppearanceStream = new PDAppearanceStream(d);

        annotationAppearanceStream.setBBox(position);
        annotationAppearanceStream.setMatrix(new AffineTransform());
        annotationAppearanceStream.setResources(p.getResources());

        try (PDPageContentStream appearanceContent = new PDPageContentStream(d, annotationAppearanceStream)) {
            appearanceContent.transform(new Matrix()); // Identity transform

            // Rect is inset by half border width to prevent border leaking
            // outside bounding box
            final float insetLowerLeftX = lowerLeftX + borderWidth * 0.5f;
            final float insetLowerLeftY = lowerLeftY + borderWidth * 0.5f;
            final float insetWidth = width - borderWidth;
            final float insetheight = height - borderWidth;

            if (null != a.getColor()) {
                appearanceContent.setLineWidth(borderWidth);
                appearanceContent.moveTo(insetLowerLeftX, insetLowerLeftY + insetheight * 0.5f);
                appearanceContent.curveTo(insetLowerLeftX, insetLowerLeftY + insetheight * 0.75f,
                        insetLowerLeftX + insetWidth * 0.25f, insetLowerLeftY + insetheight,
                        insetLowerLeftX + insetWidth * 0.5f, insetLowerLeftY + insetheight);
                appearanceContent.curveTo(insetLowerLeftX + insetWidth * 0.75f, insetLowerLeftY + insetheight,
                        insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.75f,
                        insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.5f);
                appearanceContent.curveTo(insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.25f,
                        insetLowerLeftX + insetWidth * 0.75f, insetLowerLeftY,
                        insetLowerLeftX + insetWidth * 0.5f, insetLowerLeftY);
                appearanceContent.curveTo(insetLowerLeftX + insetWidth * 0.25f, insetLowerLeftY,
                        insetLowerLeftX, insetLowerLeftY + insetheight * 0.25f, insetLowerLeftX,
                        insetLowerLeftY + insetheight * 0.5f);
            }
            appearanceContent.setNonStrokingColor(GRAY);

            if (null != a.getColor() && 0 < borderWidth) {
                appearanceContent.setStrokingColor(a.getColor());
                appearanceContent.fillAndStroke();
            } else {
                appearanceContent.fill();

            }

            if (hasContents) {
                appearanceContent.moveTo(0, 0);
                appearanceContent.beginText();

                // Center text vertically, left justified inside border with margin
                appearanceContent.newLineAtOffset(lowerLeftX + borderWidth + margin,
                        lowerLeftY + (height + LINE_SPACE_SIZE_PDF_POINTS) * 0.5f - textHeight * 0.5f);
                appearanceContent.setFont(FONT, fontSize);
                appearanceContent.setNonStrokingColor(BLACK); // Sets color of text
                appearanceContent.showText(a.getContents());
                appearanceContent.endText();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(AnnotationGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }

    return annotationAppearanceStream;
}

From source file:airviewer.AnnotationGenerator.java

/**
 * /*from  w  w  w . j  a  v  a 2s. co  m*/
 * @param document
 * @param page
 * @param subtype
 * @param position
 * @param borderColor
 * @param fillColor
 * @param borderWidth
 * @param contents
 * @return 
 */
public static PDAnnotationMarkup makeAnnotation(PDDocument document, PDPage page, String subtype,
        PDRectangle position, PDColor borderColor, PDColor fillColor, float borderWidth, String contents) {
    assert null != document;
    assert null != page;
    assert null != subtype;
    assert null != position;

    PDAnnotationMarkup result = null;

    try {
        if (null != contents && 0 < contents.length()) {
            PDFont font = FONT;
            final float fontSize = FONT_SIZE_PDF_POINTS;
            final float lineSpacing = LINE_SPACE_SIZE_PDF_POINTS;
            float width = max(position.getWidth(),
                    font.getStringWidth(contents) * fontSize / AnnotationGenerator.SIZE_UNITS_PER_PDF_POINT);
            position.setUpperRightX(position.getLowerLeftX() + width);
        }

        PDAnnotationSquareCircle a = new PDAnnotationSquareCircle(subtype);
        a.setAnnotationName(new UID().toString());
        a.setContents(contents);
        a.setRectangle(position);

        if (null != fillColor) {
            a.setInteriorColor(fillColor);
        }

        if (0 < borderWidth) {
            PDBorderStyleDictionary borderStyle = new PDBorderStyleDictionary();
            borderStyle.setWidth(borderWidth);
            a.setBorderStyle(borderStyle);
            if (null != borderColor) {
                a.setColor(borderColor);

            }
        }

        // The following lines are needed for PDFRenderer to render 
        // annotations. Preview and Acrobat don't seem to need these.
        if (null == a.getAppearance()) {
            a.setAppearance(new PDAppearanceDictionary());
            PDAppearanceStream annotationAppearanceStream = AnnotationGenerator.generateAppearanceStream(a,
                    document, page, true);
            a.getAppearance().setNormalAppearance(annotationAppearanceStream);
        }

        result = a;

    } catch (IOException | NullPointerException ex) {
        Logger.getLogger(EllipseAnnotationMaker.class.getName()).log(Level.SEVERE, null, ex);
    }

    return result;
}

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.positioning.Positioning.java

License:EUPL

/**
 * Sets the width of the table according to the layout of the document and
 * calculates the y position where the PDFPTable should be placed.
 *
 * @param pdfDataSource//from  ww  w  .  j a  va  2s . c  om
 *            The PDF document.
 * @param pdf_table
 *            The PDFPTable to be placed.
 * @return Returns the position where the PDFPTable should be placed.
 * @throws PdfAsException
 *             F.e.
 */
public static PositioningInstruction adjustSignatureTableandCalculatePosition(final PDDocument pdfDataSource,
        IPDFVisualObject pdf_table, TablePos pos, boolean legacy32, boolean legacy40) throws PdfAsException {

    PdfBoxUtils.checkPDFPermissions(pdfDataSource);
    // get pages of currentdocument

    int doc_pages = pdfDataSource.getNumberOfPages();
    int page = doc_pages;
    boolean make_new_page = pos.isNewPage();
    if (!(pos.isNewPage() || pos.isPauto())) {
        // we should posit signaturtable on this page

        page = pos.getPage();
        // System.out.println("XXXXPAGE="+page+" doc_pages="+doc_pages);
        if (page > doc_pages) {
            make_new_page = true;
            page = doc_pages;
            // throw new PDFDocumentException(227, "Page number is to big(="
            // + page+
            // ") cannot be parsed.");
        }
    }

    PDPage pdPage = (PDPage) pdfDataSource.getDocumentCatalog().getAllPages().get(page - 1);
    PDRectangle cropBox = pdPage.getCropBox();

    // fallback to MediaBox if Cropbox not available!

    if (cropBox == null) {
        cropBox = pdPage.findCropBox();
    }

    if (cropBox == null) {
        cropBox = pdPage.findMediaBox();
    }

    // getPagedimensions
    // Rectangle psize = reader.getPageSizeWithRotation(page);
    // int page_rotation = reader.getPageRotation(page);

    // Integer rotation = pdPage.getRotation();
    // int page_rotation = rotation.intValue();

    int rotation = pdPage.findRotation();

    logger.debug("Original CropBox: " + cropBox.toString());

    cropBox = rotateBox(cropBox, rotation);

    logger.debug("Rotated CropBox: " + cropBox.toString());

    float page_width = cropBox.getWidth();
    float page_height = cropBox.getHeight();

    logger.debug("CropBox width: " + page_width);
    logger.debug("CropBox heigth: " + page_height);

    // now we can calculate x-position
    float pre_pos_x = SIGNATURE_MARGIN_HORIZONTAL;
    if (!pos.isXauto()) {
        // we do have absolute x
        pre_pos_x = pos.getPosX();
    }
    // calculate width
    // center
    float pre_width = page_width - 2 * pre_pos_x;
    if (!pos.isWauto()) {
        // we do have absolute width
        pre_width = pos.getWidth();
        if (pos.isXauto()) { // center x
            pre_pos_x = (page_width - pre_width) / 2;
        }
    }
    final float pos_x = pre_pos_x;
    final float width = pre_width;
    // Signatur table dimensions are complete
    pdf_table.setWidth(width);
    pdf_table.fixWidth();
    // pdf_table.setTotalWidth(width);
    // pdf_table.setLockedWidth(true);

    final float table_height = pdf_table.getHeight();
    // now check pos_y
    float pos_y = pos.getPosY();

    // in case an absolute y position is already given OR
    // if the table is related to an invisible signature
    // there is no need for further calculations
    // (fixed adding new page in case of invisible signatures)
    if (!pos.isYauto() || table_height == 0) {
        // we do have y-position too --> all parameters but page ok
        if (make_new_page) {
            page++;
        }
        return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);
    }
    // pos_y is auto
    if (make_new_page) {
        // ignore footer in new page
        page++;
        pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
        return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);
    }
    // up to here no checks have to be made if Tablesize and Pagesize are
    // fit
    // Now we have to getfreespace in page and reguard footerline
    float footer_line = pos.getFooterLine();

    float pre_page_length = PDFUtilities.calculatePageLength(pdfDataSource, page - 1,
            page_height - footer_line, /* page_rotation, */
            legacy32, legacy40);

    if (pre_page_length == Float.NEGATIVE_INFINITY) {
        // we do have an empty page or nothing in area above footerline
        pre_page_length = page_height;
        // no text --> SIGNATURE_BORDER
        pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
        if (pos_y - footer_line <= table_height) {
            make_new_page = true;
            if (!pos.isPauto()) {
                // we have to correct pagenumber
                page = pdfDataSource.getNumberOfPages();
            }
            page++;
            // no text --> SIGNATURE_BORDER
            pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
        }
        return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);
    }
    final float page_length = pre_page_length;
    // we do have text take SIGNATURE_MARGIN
    pos_y = page_height - page_length - SIGNATURE_MARGIN_VERTICAL;
    if (pos_y - footer_line <= table_height) {
        make_new_page = true;
        if (!pos.isPauto()) {
            // we have to correct pagenumber in case of absolute page and
            // not enough
            // space
            page = pdfDataSource.getNumberOfPages();
        }
        page++;
        // no text --> SIGNATURE_BORDER
        pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
    }
    return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);

}

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.positioning.Positioning.java

License:EUPL

/**
 * Sets the width of the table according to the layout of the document and
 * calculates the y position where the PDFPTable should be placed.
 *
 * @param pdfDataSource//from w w  w.  j  a  v  a  2 s . c  o m
 *            The PDF document.
 * @param pdf_table
 *            The PDFPTable to be placed.
 * @param settings 
 * @return Returns the position where the PDFPTable should be placed.
 * @throws PdfAsException
 *             F.e.
 */
public static PositioningInstruction adjustSignatureTableandCalculatePosition(final PDDocument pdfDataSource,
        IPDFVisualObject pdf_table, TablePos pos, ISettings settings) throws PdfAsException {

    PdfBoxUtils.checkPDFPermissions(pdfDataSource);
    // get pages of currentdocument

    int doc_pages = pdfDataSource.getNumberOfPages();
    int page = doc_pages;
    boolean make_new_page = pos.isNewPage();
    if (!(pos.isNewPage() || pos.isPauto())) {
        // we should posit signaturtable on this page

        page = pos.getPage();
        // System.out.println("XXXXPAGE="+page+" doc_pages="+doc_pages);
        if (page > doc_pages) {
            make_new_page = true;
            page = doc_pages;
            // throw new PDFDocumentException(227, "Page number is to big(="
            // + page+
            // ") cannot be parsed.");
        }
    }

    PDPage pdPage = pdfDataSource.getPage(page - 1);

    PDRectangle cropBox = pdPage.getCropBox();

    // fallback to MediaBox if Cropbox not available!

    if (cropBox == null) {
        cropBox = pdPage.getCropBox();
    }

    if (cropBox == null) {
        cropBox = pdPage.getCropBox();
    }

    // getPagedimensions
    // Rectangle psize = reader.getPageSizeWithRotation(page);
    // int page_rotation = reader.getPageRotation(page);

    // Integer rotation = pdPage.getRotation();
    // int page_rotation = rotation.intValue();

    int rotation = pdPage.getRotation();

    logger.debug("Original CropBox: " + cropBox.toString());

    cropBox = rotateBox(cropBox, rotation);

    logger.debug("Rotated CropBox: " + cropBox.toString());

    float page_width = cropBox.getWidth();
    float page_height = cropBox.getHeight();

    logger.debug("CropBox width: " + page_width);
    logger.debug("CropBox heigth: " + page_height);

    // now we can calculate x-position
    float pre_pos_x = SIGNATURE_MARGIN_HORIZONTAL;
    if (!pos.isXauto()) {
        // we do have absolute x
        pre_pos_x = pos.getPosX();
    }
    // calculate width
    // center
    float pre_width = page_width - 2 * pre_pos_x;
    if (!pos.isWauto()) {
        // we do have absolute width
        pre_width = pos.getWidth();
        if (pos.isXauto()) { // center x
            pre_pos_x = (page_width - pre_width) / 2;
        }
    }
    final float pos_x = pre_pos_x;
    final float width = pre_width;
    // Signatur table dimensions are complete
    pdf_table.setWidth(width);
    pdf_table.fixWidth();
    // pdf_table.setTotalWidth(width);
    // pdf_table.setLockedWidth(true);

    final float table_height = pdf_table.getHeight();
    // now check pos_y
    float pos_y = pos.getPosY();

    // in case an absolute y position is already given OR
    // if the table is related to an invisible signature
    // there is no need for further calculations
    // (fixed adding new page in case of invisible signatures)
    if (!pos.isYauto() || table_height == 0) {
        // we do have y-position too --> all parameters but page ok
        if (make_new_page) {
            page++;
        }
        return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);
    }
    // pos_y is auto
    if (make_new_page) {
        // ignore footer in new page
        page++;
        pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
        return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);
    }
    // up to here no checks have to be made if Tablesize and Pagesize are
    // fit
    // Now we have to getfreespace in page and reguard footerline
    float footer_line = pos.getFooterLine();

    //      float pre_page_length = PDFUtilities.calculatePageLength(pdfDataSource,
    //            page - 1, page_height - footer_line, /* page_rotation, */
    //            legacy32, legacy40);

    float pre_page_length = Float.NEGATIVE_INFINITY;
    try {
        pre_page_length = PDFUtilities.getMaxYPosition(pdfDataSource, page - 1, pdf_table,
                SIGNATURE_MARGIN_VERTICAL, footer_line, settings);
        //pre_page_length = PDFUtilities.getFreeTablePosition(pdfDataSource, page-1, pdf_table,SIGNATURE_MARGIN_VERTICAL);
    } catch (IOException e) {
        logger.warn("Could not determine page length, using -INFINITY");
    }

    if (pre_page_length == Float.NEGATIVE_INFINITY) {
        // we do have an empty page or nothing in area above footerline
        pre_page_length = page_height;
        // no text --> SIGNATURE_BORDER
        pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
        if (pos_y - footer_line <= table_height) {
            make_new_page = true;
            if (!pos.isPauto()) {
                // we have to correct pagenumber
                page = pdfDataSource.getNumberOfPages();
            }
            page++;
            // no text --> SIGNATURE_BORDER
            pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
        }
        return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);
    }
    final float page_length = pre_page_length;
    // we do have text take SIGNATURE_MARGIN
    pos_y = page_height - page_length - SIGNATURE_MARGIN_VERTICAL;
    if (pos_y - footer_line <= table_height) {
        make_new_page = true;
        if (!pos.isPauto()) {
            // we have to correct pagenumber in case of absolute page and
            // not enough
            // space
            page = pdfDataSource.getNumberOfPages();
        }
        page++;
        // no text --> SIGNATURE_BORDER
        pos_y = page_height - SIGNATURE_MARGIN_VERTICAL;
    }
    return new PositioningInstruction(make_new_page, page, pos_x, pos_y, pos.rotation);

}

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

License:EUPL

/**
 * Each page of document can be different sizes.
 * //from ww w. j a v  a2 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);
    }

    List<?> pages = document.getDocumentCatalog().getAllPages();
    if (newpage) {
        PDPage lastPage = (PDPage) pages.get(pages.size() - 1);
        PDRectangle mediaBox = lastPage.findMediaBox();
        pageRotation = lastPage.findRotation() % 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.findMediaBox();
        pageRotation = firstPage.findRotation() % 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.PDFAsVisualSignatureDesigner.java

License:EUPL

/**
 * Each page of document can be different sizes.
 * //from   w w  w.j a va  2  s . c  om
 * @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.knowcenter.wag.egov.egiz.pdf.PDFPage.java

License:EUPL

/**
 * Registers a rectangle that bounds the path currently being drawn.
 * //  ww  w.  ja  v  a2s  .  c  o  m
 * @param bounds
 *            A rectangle depicting the bounds (coordinates originating from
 *            bottom left).
 * @author Datentechnik Innovation GmbH
 */
public void registerPathBounds(Rectangle bounds) {
    if (!bounds.isEmpty()) {
        logger.debug("Registering path bounds: " + bounds);

        // vertical start of rectangle (counting from top of page)
        float upperBoundYPositionFromTop;

        // vertical end of rectangle (counting from top of page)
        // this depicts the current end of path-related page content
        float lowerBoundYPositionFromTop;

        PDRectangle boundaryBox = this.getCurrentPage().findCropBox();

        if (boundaryBox == null) {
            boundaryBox = this.getCurrentPage().findMediaBox();
        }

        float pageHeight;

        switch (this.getCurrentPage().findRotation()) {
        case 90: // CW
            pageHeight = boundaryBox.getWidth();
            upperBoundYPositionFromTop = (float) bounds.getMinX();
            lowerBoundYPositionFromTop = (float) bounds.getMaxX();
            break;
        case 180:
            pageHeight = boundaryBox.getHeight();
            upperBoundYPositionFromTop = (float) bounds.getMinY();
            lowerBoundYPositionFromTop = (float) bounds.getMaxY();
            break;
        case 270: // CCW
            pageHeight = boundaryBox.getWidth();
            upperBoundYPositionFromTop = pageHeight - (float) bounds.getMaxX();
            lowerBoundYPositionFromTop = pageHeight - (float) bounds.getMinX();
            break;
        default:
            pageHeight = boundaryBox.getHeight();
            upperBoundYPositionFromTop = pageHeight - (float) bounds.getMaxY();
            lowerBoundYPositionFromTop = pageHeight - (float) bounds.getMinY();
            break;
        }

        // new maximum ?
        if (lowerBoundYPositionFromTop > maxPathRelatedYPositionFromTop) {
            // Is the rectangle (at least partly) located above the footer
            // line?
            // (effective page height := page height - footer line)
            if (upperBoundYPositionFromTop <= effectivePageHeight) {
                // yes: update current end of path-related page content
                maxPathRelatedYPositionFromTop = lowerBoundYPositionFromTop;
                logger.trace("New max path related y position (from top): " + maxPathRelatedYPositionFromTop);
            } else {
                // no: rectangle is fully located below the footer line ->
                // ignore
                logger.trace("Ignoring path bound below the footer line.");
            }
        }
    }
}

From source file:at.knowcenter.wag.egov.egiz.pdfbox2.pdf.PDFPage.java

License:EUPL

/**
 * Registers a rectangle that bounds the path currently being drawn.
 * /*  w w  w. j  a  v a 2  s  .com*/
 * @param bounds
 *            A rectangle depicting the bounds (coordinates originating from
 *            bottom left).
 * @author Datentechnik Innovation GmbH
 */
public void registerPathBounds(Rectangle bounds) {
    if (!bounds.isEmpty()) {
        logger.debug("Registering path bounds: " + bounds);

        // vertical start of rectangle (counting from top of page)
        float upperBoundYPositionFromTop;

        // vertical end of rectangle (counting from top of page)
        // this depicts the current end of path-related page content
        float lowerBoundYPositionFromTop;

        PDRectangle boundaryBox = this.getCurrentPage().getCropBox();

        if (boundaryBox == null) {
            boundaryBox = this.getCurrentPage().getMediaBox();
        }

        float pageHeight;

        switch (this.getCurrentPage().getRotation()) {
        case 90: // CW
            pageHeight = boundaryBox.getWidth();
            upperBoundYPositionFromTop = (float) bounds.getMinX();
            lowerBoundYPositionFromTop = (float) bounds.getMaxX();
            break;
        case 180:
            pageHeight = boundaryBox.getHeight();
            upperBoundYPositionFromTop = (float) bounds.getMinY();
            lowerBoundYPositionFromTop = (float) bounds.getMaxY();
            break;
        case 270: // CCW
            pageHeight = boundaryBox.getWidth();
            upperBoundYPositionFromTop = pageHeight - (float) bounds.getMaxX();
            lowerBoundYPositionFromTop = pageHeight - (float) bounds.getMinX();
            break;
        default:
            pageHeight = boundaryBox.getHeight();
            upperBoundYPositionFromTop = pageHeight - (float) bounds.getMaxY();
            lowerBoundYPositionFromTop = pageHeight - (float) bounds.getMinY();
            break;
        }

        // new maximum ?
        if (lowerBoundYPositionFromTop > maxPathRelatedYPositionFromTop) {
            // Is the rectangle (at least partly) located above the footer
            // line?
            // (effective page height := page height - footer line)
            if (upperBoundYPositionFromTop <= effectivePageHeight) {
                // yes: update current end of path-related page content
                maxPathRelatedYPositionFromTop = lowerBoundYPositionFromTop;
                logger.trace("New max path related y position (from top): " + maxPathRelatedYPositionFromTop);
            } else {
                // no: rectangle is fully located below the footer line ->
                // ignore
                logger.trace("Ignoring path bound below the footer line.");
            }
        }
    }
}