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

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

This will return a string representation of this rectangle.

Usage

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 av a  2  s.  c  o m
 *            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 va2s. com
 *            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.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createSignatureRectangle(PDSignatureField signatureField, PDFAsVisualSignatureDesigner properties,
        float degrees) throws IOException {

    PDRectangle rect = new PDRectangle();

    Point2D upSrc = new Point2D.Float();
    upSrc.setLocation(properties.getxAxis() + properties.getWidth(),
            properties.getPageHeight() - properties.getyAxis());

    Point2D llSrc = new Point2D.Float();
    llSrc.setLocation(properties.getxAxis(),
            properties.getPageHeight() - properties.getyAxis() - properties.getHeight());

    rect.setUpperRightX((float) upSrc.getX());
    rect.setUpperRightY((float) upSrc.getY());
    rect.setLowerLeftY((float) llSrc.getY());
    rect.setLowerLeftX((float) llSrc.getX());
    logger.debug("orig rectangle of signature has been created: {}", rect.toString());

    AffineTransform transform = new AffineTransform();
    transform.setToIdentity();//from w ww .  j av  a  2 s .co  m
    if (degrees % 360 != 0) {
        transform.setToRotation(Math.toRadians(degrees), llSrc.getX(), llSrc.getY());
    }

    Point2D upDst = new Point2D.Float();
    transform.transform(upSrc, upDst);

    Point2D llDst = new Point2D.Float();
    transform.transform(llSrc, llDst);

    float xPos = properties.getxAxis();
    float yPos = properties.getPageHeight() - properties.getyAxis();
    logger.debug("POS {} x {}", xPos, yPos);
    logger.debug("SIZE {} x {}", properties.getWidth(), properties.getHeight());
    // translate according to page! rotation
    int pageRotation = properties.getPageRotation();
    AffineTransform translate = new AffineTransform();
    switch (pageRotation) {
    case 90:
        translate.setToTranslation(
                properties.getPageHeight() - (properties.getPageHeight() - properties.getyAxis())
                        - properties.getxAxis() + properties.getHeight(),
                properties.getxAxis() + properties.getHeight()
                        - (properties.getPageHeight() - properties.getyAxis()));
        break;
    case 180:
        // translate.setToTranslation(properties.getPageWidth() -
        // properties.getxAxis() - properties.getxAxis(),
        // properties.getPageHeight() - properties.getyAxis() +
        // properties.getHeight());
        translate.setToTranslation(properties.getPageWidth() - 2 * xPos,
                properties.getPageHeight() - 2 * (yPos - properties.getHeight()));
        break;
    case 270:
        translate.setToTranslation(-properties.getHeight() + yPos - xPos,
                properties.getPageWidth() - (yPos - properties.getHeight()) - xPos);
        break;
    }

    translate.transform(upDst, upDst);
    translate.transform(llDst, llDst);

    rect.setUpperRightX((float) upDst.getX());
    rect.setUpperRightY((float) upDst.getY());
    rect.setLowerLeftY((float) llDst.getY());
    rect.setLowerLeftX((float) llDst.getX());
    logger.debug("rectangle of signature has been created: {}", rect.toString());
    signatureField.getWidget().setRectangle(rect);
    getStructure().setSignatureRectangle(rect);
    logger.debug("rectangle of signature has been created");
}