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

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

Introduction

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

Prototype

public PDRectangle(float x, float y, float width, float height) 

Source Link

Document

Constructor.

Usage

From source file:airviewer.AnnotationGenerator.java

/**
 *
 * @param a//from  www .j  a va 2 s  .  c o m
 * @throws IOException
 */
public static void resizeAnnotationToContent(PDAnnotation a) throws IOException {
    assert null != a;

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

        float width = FONT.getStringWidth(contents) * fontSize / SIZE_UNITS_PER_PDF_POINT;
        float height = textHeight;

        // Reserve enough width for border and margin at start and end
        width += (borderWidth + MARGIN_SIZE_PDF_POINTS) * 2.0f;
        height += (borderWidth + MARGIN_SIZE_PDF_POINTS) * 2.0f;

        // Replace the annotations existing rectangle
        a.setRectangle(new PDRectangle(lowerLeftX, lowerLeftY, width, height));
    }

}

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

License:Apache License

/**
 * Get the Page Line Paths./*from  www .j av a 2  s.  co m*/
 * @param page {@link PDPage}
 * @return {@link List} of {@link PDRectangle}
 */
private List<PDRectangle> getPageLinePaths(final PDPage page) {

    List<PDRectangle> rects = new ArrayList<>();

    try {
        PdfLinePaths linePaths = new PdfLinePaths(page);
        linePaths.processPage();

        List<GeneralPath> lines = linePaths.getLinePaths();

        for (GeneralPath gp : lines) {
            Rectangle r = gp.getBounds();
            rects.add(new PDRectangle(r.x, r.y, r.width, r.height));
        }

    } catch (IOException e) {
        LOG.log(Level.WARNING, "Unable to find Page Line Paths", e);
        rects = Collections.emptyList();
    }

    return rects;
}

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

License:Apache License

/**
 * Calculate the {@link PDRectangle} of {@link TextPosition}.
 * @param textPositions {@link List}/*from  w w w.  ja v  a2 s.  c  o  m*/
 * @return {@link PDRectangle}
 */
public static PDRectangle calculateTextPosition(final List<TextPosition> textPositions) {

    float x = round(minimum(textPositions.stream().map(s -> Float.valueOf(s.getXDirAdj()))));

    float y = round(getPageHeight(textPositions)
            - minimum(textPositions.stream().map(s -> Float.valueOf(s.getYDirAdj()))));

    float width = getWidth(textPositions);
    float height = getHeight(textPositions);

    return new PDRectangle(x, y, width, height);
}

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

License:Apache License

/**
 * Calculate Horziontal Matching Rectangle by field type.
 *
 * @param page {@link PDPage}// www . ja v  a  2  s  .  co  m
 * @param field {@link PDField}
 * @param w {@link PDAnnotationWidget}
 * @return {@link PDRectangle}
 */
private PDRectangle getHorizontalRectangle(final PDPage page, final PDField field, final PDAnnotationWidget w) {

    PDRectangle wrect = w.getRectangle();
    PDRectangle rect = new PDRectangle(0, wrect.getLowerLeftY(), wrect.getWidth() + wrect.getLowerLeftX(),
            wrect.getHeight());

    if (field instanceof PDCheckBox) {
        rect = new PDRectangle(0, wrect.getLowerLeftY(), page.getMediaBox().getWidth(), wrect.getHeight());
    }

    return rect;
}

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

License:Apache License

/**
 * Calculate Vertical Matching Rectangle by field type.
 *
 * @param field {@link PDField}//from w  w w . j  av  a  2  s. c  o m
 * @param w {@link PDAnnotationWidget}
 * @return {@link PDRectangle}
 */
private PDRectangle getVerticalRectangle(final PDField field, final PDAnnotationWidget w) {

    PDRectangle rect = null;

    if (!(field instanceof PDCheckBox)) {

        PDRectangle wrect = w.getRectangle();
        float addition = 2 * wrect.getHeight();

        rect = new PDRectangle(wrect.getLowerLeftX() - addition, wrect.getLowerLeftY(),
                wrect.getWidth() + addition, wrect.getHeight() + addition);
    }

    return rect;
}

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

License:Apache License

@Override
public List<PDFieldSearchRectangle> getTextSearchArea(final PDPage page, final PDField pdField,
        final PDAnnotationWidget widget, final List<PDRectangle> lineRects) {

    List<PDFieldSearchRectangle> area = new ArrayList<>(1);
    PDRectangle wrect = widget.getRectangle();

    List<PDRectangle> leftlist = new ArrayList<>();
    List<PDRectangle> rightlist = new ArrayList<>();
    List<PDRectangle> toplist = new ArrayList<>();
    List<PDRectangle> bottomlist = new ArrayList<>();

    for (PDRectangle line : lineRects) {

        if (line.getLowerLeftY() < wrect.getLowerLeftY() && wrect.getUpperRightY() < line.getUpperRightY()) {

            if (line.getUpperRightX() < wrect.getLowerLeftX()) {
                leftlist.add(line);//from w  ww .j a  v a 2s . co  m
            } else if (wrect.getUpperRightX() < line.getLowerLeftX()) {
                rightlist.add(line);
            }
        }

        if (line.getLowerLeftX() < wrect.getLowerLeftX() && wrect.getUpperRightX() < line.getUpperRightX()) {

            if (line.getUpperRightY() < wrect.getLowerLeftY()) {
                bottomlist.add(line);
            } else if (wrect.getUpperRightY() < line.getUpperRightY()) {
                toplist.add(line);
            }
        }
    }

    PDRectangle left = !leftlist.isEmpty() ? Collections.max(leftlist, new PDRectangleXComparator()) : null;
    PDRectangle right = !rightlist.isEmpty() ? Collections.min(rightlist, new PDRectangleXComparator()) : null;
    PDRectangle top = !toplist.isEmpty() ? Collections.min(toplist, new PDRectangleYComparator()) : null;
    PDRectangle bottom = !bottomlist.isEmpty() ? Collections.max(bottomlist, new PDRectangleYComparator())
            : null;

    if (left != null && right != null && top != null && bottom != null) {
        PDRectangle r = new PDRectangle(left.getLowerLeftX(), bottom.getLowerLeftY(),
                right.getUpperRightX() - left.getLowerLeftX(), top.getUpperRightY() - bottom.getLowerLeftY());

        area.add(new PDFieldSearchRectangle(PDFieldAreaSearch.RECTANGLE, r));
    }

    return area;
}

From source file:com.formkiq.core.service.pdf.pdfbox.PdfTextFieldHorizontalGroupComparatorTest.java

License:Apache License

/**
 * testCompare01().//from   ww  w . jav a  2s .c  om
 * same line
 */
@Test
public void testCompare01() {
    // given
    final int fontSize = 12;
    String fontName = "bold";

    final int x0 = 401;
    final int y0 = 951;
    final int width0 = 30;
    final int height0 = 4;

    final int x1 = 432;
    final int y1 = 951;
    final int width1 = 25;
    final int height1 = 4;

    PdfTextField o1 = new PdfTextField();
    o1.setRectangle(new PDRectangle(x0, y0, width0, height0));
    o1.setFontSize(fontSize);
    o1.setFontName(fontName);

    PdfTextField o2 = new PdfTextField();
    o2.setRectangle(new PDRectangle(x1, y1, width1, height1));
    o2.setFontSize(fontSize);
    o2.setFontName(fontName);

    // when
    int result0 = this.comparator.compare(o1, o2);
    int result1 = this.comparator.compare(o2, o1);

    // then
    assertEquals(0, result0);
    assertEquals(0, result1);
}

From source file:com.formkiq.core.service.pdf.pdfbox.PdfTextFieldHorizontalGroupComparatorTest.java

License:Apache License

/**
 * testCompare02().//w  w  w .  ja v a 2 s.c  o m
 * same line, too far apart
 */
@Test
public void testCompare02() {
    // given
    final int fontSize = 12;
    String fontName = "bold";

    final int x0 = 201;
    final int y0 = 951;
    final int width0 = 30;
    final int height0 = 4;

    final int x1 = 432;
    final int y1 = 951;
    final int width1 = 25;
    final int height1 = 4;

    PdfTextField o1 = new PdfTextField();
    o1.setRectangle(new PDRectangle(x0, y0, width0, height0));
    o1.setFontSize(fontSize);
    o1.setFontName(fontName);

    PdfTextField o2 = new PdfTextField();
    o2.setRectangle(new PDRectangle(x1, y1, width1, height1));
    o2.setFontSize(fontSize);
    o2.setFontName(fontName);

    // when
    int result0 = this.comparator.compare(o1, o2);
    int result1 = this.comparator.compare(o2, o1);

    // then
    assertNotEquals(0, result0);
    assertNotEquals(0, result1);
}

From source file:com.formkiq.core.service.pdf.pdfbox.PdfTextFieldHorizontalGroupComparatorTest.java

License:Apache License

/**
 * testCompare01()./*from ww  w .j  a va  2s.com*/
 * same different line
 */
@Test
public void testCompare03() {
    // given
    final int fontSize = 12;
    String fontName = "bold";

    final int x0 = 401;
    final int y0 = 951;
    final int width0 = 30;
    final int height0 = 4;

    final int x1 = 432;
    final int y1 = 961;
    final int width1 = 25;
    final int height1 = 4;

    PdfTextField o1 = new PdfTextField();
    o1.setRectangle(new PDRectangle(x0, y0, width0, height0));
    o1.setFontSize(fontSize);
    o1.setFontName(fontName);

    PdfTextField o2 = new PdfTextField();
    o2.setRectangle(new PDRectangle(x1, y1, width1, height1));
    o2.setFontSize(fontSize);
    o2.setFontName(fontName);

    // when
    int result0 = this.comparator.compare(o1, o2);
    int result1 = this.comparator.compare(o2, o1);

    // then
    assertNotEquals(0, result0);
    assertNotEquals(0, result1);
}

From source file:com.formkiq.core.service.pdf.pdfbox.PdfTextFieldTextGroupComparatorTest.java

License:Apache License

/**
 * testCompare01()./*from w ww.  j  a  v  a2s  .  com*/
 */
@Test
public void testCompare01() {
    // given
    final int fontSize = 12;
    String fontName = "bold";

    final int x = 30;
    final int y0 = 409;
    final int width0 = 4;
    final int height0 = 4;

    final int y1 = 219;
    final int width1 = 133;
    final int height1 = 5;

    PdfTextField o1 = new PdfTextField();
    o1.setRectangle(new PDRectangle(x, y0, width0, height0));
    o1.setFontSize(fontSize);
    o1.setFontName(fontName);

    PdfTextField o2 = new PdfTextField();
    o2.setRectangle(new PDRectangle(x, y1, width1, height1));
    o2.setFontSize(fontSize);
    o2.setFontName(fontName);

    // when
    int result0 = this.comparator.compare(o1, o2);
    int result1 = this.comparator.compare(o2, o1);

    // then
    assertNotEquals(0, result0);
    assertNotEquals(0, result1);
}