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

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

Introduction

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

Prototype

public float getUpperRightX() 

Source Link

Document

This will get the upper right x coordinate.

Usage

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

License:EUPL

private static PDRectangle rotateBox(PDRectangle cropBox, int rotation) {
    if (rotation != 0) {
        Point2D upSrc = new Point2D.Float();

        upSrc.setLocation(cropBox.getUpperRightX(), cropBox.getUpperRightY());

        Point2D llSrc = new Point2D.Float();
        llSrc.setLocation(cropBox.getLowerLeftX(), cropBox.getLowerLeftY());
        AffineTransform transform = new AffineTransform();
        transform.setToIdentity();/*from  www  .  j  a v a 2s.c om*/
        if (rotation % 360 != 0) {
            transform.setToRotation(Math.toRadians(rotation * -1), llSrc.getX(), llSrc.getY());
        }
        Point2D upDst = new Point2D.Float();
        transform.transform(upSrc, upDst);

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

        float y1 = (float) upDst.getY();
        float y2 = (float) llDst.getY();

        if (y1 > y2) {
            float t = y1;
            y1 = y2;
            y2 = t;
        }

        if (y1 < 0) {
            y2 = y2 + -1 * y1;
            y1 = 0;
        }

        float x1 = (float) upDst.getX();
        float x2 = (float) llDst.getX();

        if (x1 > x2) {
            float t = x1;
            x1 = x2;
            x2 = t;
        }

        if (x1 < 0) {
            x2 = x2 + -1 * x1;
            x1 = 0;
        }

        cropBox.setUpperRightX(x2);
        cropBox.setUpperRightY(y2);
        cropBox.setLowerLeftY(y1);
        cropBox.setLowerLeftX(x1);
    }
    return cropBox;
}

From source file:ch.uzh.ifi.pdeboer.pdfpreprocessing.pdf.TextHighlight.java

License:Apache License

private boolean markupMatch(Color color, PDPageContentStream contentStream, Match markingMatch)
        throws IOException {
    final List<PDRectangle> textBoundingBoxes = getTextBoundingBoxes(markingMatch.positions);

    if (textBoundingBoxes.size() > 0) {
        contentStream.appendRawCommands("/highlights gs\n");
        contentStream.setNonStrokingColor(color);
        for (PDRectangle textBoundingBox : textBoundingBoxes) {
            contentStream.fillRect(textBoundingBox.getLowerLeftX(), textBoundingBox.getLowerLeftY(),
                    Math.max(Math.abs(textBoundingBox.getUpperRightX() - textBoundingBox.getLowerLeftX()), 10),
                    10);/*w w w .  j a v a  2s .c  o m*/
        }
        return true;
    }
    return false;
}

From source file:chiliad.parser.pdf.extractor.vectorgraphics.operator.Invoke.java

License:Apache License

/**
 * process : Do : Paint the specified XObject (section 4.7).
 *
 * @param operator The operator that is being executed.
 * @param arguments List//from w  w w. j a  v a  2 s .c o m
 * @throws IOException If there is an error invoking the sub object.
 */
@Override
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    VectorGraphicsExtractor extractor = (VectorGraphicsExtractor) context;

    PDPage page = extractor.getPage();
    COSName objectName = (COSName) arguments.get(0);
    Map<String, PDXObject> xobjects = extractor.getResources().getXObjects();
    PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
    if (xobject == null) {
        LOG.warn("Can't find the XObject for '" + objectName.getName() + "'");
    } else if (xobject instanceof PDXObjectImage) {
        PDXObjectImage image = (PDXObjectImage) xobject;
        try {
            if (image.getImageMask()) {
                // set the current non stroking colorstate, so that it can
                // be used to create a stencil masked image
                image.setStencilColor(extractor.getGraphicsState().getNonStrokingColor());
            }
            BufferedImage awtImage = image.getRGBImage();
            if (awtImage == null) {
                LOG.warn("getRGBImage returned NULL");
                return;//TODO PKOCH
            }
            int imageWidth = awtImage.getWidth();
            int imageHeight = awtImage.getHeight();
            double pageHeight = extractor.getPageSize().getHeight();

            LOG.debug("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight);

            Matrix ctm = extractor.getGraphicsState().getCurrentTransformationMatrix();
            float yScaling = ctm.getYScale();
            float angle = (float) Math.acos(ctm.getValue(0, 0) / ctm.getXScale());
            if (ctm.getValue(0, 1) < 0 && ctm.getValue(1, 0) > 0) {
                angle = (-1) * angle;
            }
            ctm.setValue(2, 1, (float) (pageHeight - ctm.getYPosition() - Math.cos(angle) * yScaling));
            ctm.setValue(2, 0, (float) (ctm.getXPosition() - Math.sin(angle) * yScaling));
            // because of the moved 0,0-reference, we have to shear in the opposite direction
            ctm.setValue(0, 1, (-1) * ctm.getValue(0, 1));
            ctm.setValue(1, 0, (-1) * ctm.getValue(1, 0));
            AffineTransform ctmAT = ctm.createAffineTransform();
            ctmAT.scale(1f / imageWidth, 1f / imageHeight);
            extractor.drawImage(awtImage, ctmAT);
        } catch (Exception e) {
            LOG.error(e, e);
        }
    } else if (xobject instanceof PDXObjectForm) {
        // save the graphics state
        context.getGraphicsStack().push((PDGraphicsState) context.getGraphicsState().clone());

        PDXObjectForm form = (PDXObjectForm) xobject;
        COSStream formContentstream = form.getCOSStream();
        // find some optional resources, instead of using the current resources
        PDResources pdResources = form.getResources();
        // if there is an optional form matrix, we have to map the form space to the user space
        Matrix matrix = form.getMatrix();
        if (matrix != null) {
            Matrix xobjectCTM = matrix.multiply(context.getGraphicsState().getCurrentTransformationMatrix());
            context.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
        }
        if (form.getBBox() != null) {
            PDGraphicsState graphicsState = context.getGraphicsState();
            PDRectangle bBox = form.getBBox();

            float x1 = bBox.getLowerLeftX();
            float y1 = bBox.getLowerLeftY();
            float x2 = bBox.getUpperRightX();
            float y2 = bBox.getUpperRightY();

            Point2D p0 = extractor.transformedPoint(x1, y1);
            Point2D p1 = extractor.transformedPoint(x2, y1);
            Point2D p2 = extractor.transformedPoint(x2, y2);
            Point2D p3 = extractor.transformedPoint(x1, y2);

            GeneralPath bboxPath = new GeneralPath();
            bboxPath.moveTo((float) p0.getX(), (float) p0.getY());
            bboxPath.lineTo((float) p1.getX(), (float) p1.getY());
            bboxPath.lineTo((float) p2.getX(), (float) p2.getY());
            bboxPath.lineTo((float) p3.getX(), (float) p3.getY());
            bboxPath.closePath();

            Area resultClippingArea = new Area(graphicsState.getCurrentClippingPath());
            Area newArea = new Area(bboxPath);
            resultClippingArea.intersect(newArea);

            graphicsState.setCurrentClippingPath(resultClippingArea);
        }
        getContext().processSubStream(page, pdResources, formContentstream);

        // restore the graphics state
        context.setGraphicsState((PDGraphicsState) context.getGraphicsStack().pop());
    }
}

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

License:Apache License

@Override
public int compare(final PdfTextField t1, final PdfTextField t2) {

    PDRectangle r1 = t1.getRectangle();
    PDRectangle r2 = t2.getRectangle();/* w w  w  . j a  v a 2 s  .  c  o m*/

    int r = Float.compare(t1.getFontSize(), t2.getFontSize());

    if (r == 0) {
        int v1 = Math.round(r1.getUpperRightY());
        int v2 = Math.round(r2.getUpperRightY());
        r = Integer.compare(v2, v1);
    }

    if (r == 0) {

        int d0 = round(abs(r1.getUpperRightX() - r2.getLowerLeftX()));
        int d1 = round(abs(r2.getUpperRightX() - r1.getLowerLeftX()));

        r = d0 < t1.getFontSize() || d1 < t1.getFontSize() ? 0 : -1;
    }

    return r;
}

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

License:Apache License

@Override
public int compare(final PDRectangle r1, final PDRectangle r2) {

    int v1 = (int) r1.getUpperRightY();
    int v2 = (int) r2.getUpperRightY();
    int r = Integer.compare(v2, v1);

    if (r == 0) {
        v1 = (int) r1.getUpperRightX();
        v2 = (int) r2.getUpperRightX();
        r = Integer.compare(v1, v2);
    }/*from  ww w  . j a v  a2  s.  c  o m*/

    return r;
}

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

License:Apache License

/**
 * Calculate the Distance between two {@link PDRectangle}.
 * @param r1 {@link PDRectangle}//from ww  w  .  j ava 2s  .c  o m
 * @param r2 {@link PDRectangle}
 * @return float
 */
public static float getDistanceBetween(final PDRectangle r1, final PDRectangle r2) {
    float dx = min(
            min(abs(r1.getLowerLeftX() - r2.getLowerLeftX()), abs(r1.getUpperRightX() - r2.getUpperRightX())),
            min(abs(r1.getLowerLeftX() - r2.getUpperRightX()), abs(r1.getUpperRightX() - r2.getLowerLeftX())));

    float dy = min(
            min(abs(r1.getLowerLeftY() - r2.getLowerLeftY()), abs(r1.getUpperRightY() - r2.getUpperRightY())),
            min(abs(r1.getLowerLeftY() - r2.getUpperRightY()), abs(r1.getUpperRightY() - r2.getLowerLeftY())));

    return dx + dy;
}

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  . ja va2 s.com
            } 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.infoimage.infotrac.pdfbox.PDFTextAnnotator.java

License:Apache License

private float[] computeQuads(PDRectangle rect) {
    float[] quads = new float[8];
    // top left//  ww w  . j  a va 2s .  co  m
    quads[0] = rect.getLowerLeftX(); // x1
    quads[1] = rect.getUpperRightY() - 2; // y1
    // bottom left
    quads[2] = rect.getUpperRightX(); // x2
    quads[3] = quads[1]; // y2
    // top right
    quads[4] = quads[0]; // x3
    quads[5] = rect.getLowerLeftY() - 2; // y3
    // bottom right
    quads[6] = quads[2]; // x4
    quads[7] = quads[5]; // y4
    return quads;
}

From source file:com.repeatability.pdf.PDFTextStripper.java

License:Apache License

private void fillBeadRectangles(PDPage page) {
    beadRectangles = new ArrayList<PDRectangle>();
    for (PDThreadBead bead : page.getThreadBeads()) {
        if (bead == null) {
            // can't skip, because of null entry handling in processTextPosition()
            beadRectangles.add(null);//  w ww  . ja  va  2s.c om
            continue;
        }

        PDRectangle rect = bead.getRectangle();

        // bead rectangle is in PDF coordinates (y=0 is bottom),
        // glyphs are in image coordinates (y=0 is top),
        // so we must flip
        PDRectangle mediaBox = page.getMediaBox();
        float upperRightY = mediaBox.getUpperRightY() - rect.getLowerLeftY();
        float lowerLeftY = mediaBox.getUpperRightY() - rect.getUpperRightY();
        rect.setLowerLeftY(lowerLeftY);
        rect.setUpperRightY(upperRightY);

        // adjust for cropbox
        PDRectangle cropBox = page.getCropBox();
        if (cropBox.getLowerLeftX() != 0 || cropBox.getLowerLeftY() != 0) {
            rect.setLowerLeftX(rect.getLowerLeftX() - cropBox.getLowerLeftX());
            rect.setLowerLeftY(rect.getLowerLeftY() - cropBox.getLowerLeftY());
            rect.setUpperRightX(rect.getUpperRightX() - cropBox.getLowerLeftX());
            rect.setUpperRightY(rect.getUpperRightY() - cropBox.getLowerLeftY());
        }

        beadRectangles.add(rect);
    }
}

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeProjectInformation(final float pageWidth, final float startingHeight,
        final ReportData reportData) throws IOException {
    final float height = startingHeight - 18;
    PDRectangle rectangle = pdfManager.writeWrappedLink(5, height, 280, reportData.getProjectName(),
            reportData.getProjectURL(), PDFBoxManager.DEFAULT_FONT, 18);
    final String dash = " - ";
    rectangle = pdfManager.writeText(5 + rectangle.getUpperRightX(), height, dash, PDFBoxManager.DEFAULT_FONT,
            18, Color.BLACK);// w  ww  .  j a  v a2 s. co  m
    rectangle = pdfManager.writeWrappedLink(5 + rectangle.getUpperRightX(), height, 280 - rectangle.getWidth(),
            reportData.getProjectVersion(), reportData.getProjectVersionURL(), PDFBoxManager.DEFAULT_FONT, 18);

    final String projectAttributesString = "Phase:  " + reportData.getPhase() + "    |    Distribution:  "
            + reportData.getDistribution();
    rectangle = pdfManager.writeWrappedText(5, rectangle.getLowerLeftY() - 18, 300, projectAttributesString);
    logger.trace("Finished writing the project information.");
    return rectangle;
}