Java Code Examples for org.apache.pdfbox.pdmodel.common.PDRectangle#getLowerLeftX()
The following examples show how to use
org.apache.pdfbox.pdmodel.common.PDRectangle#getLowerLeftX() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RotatePageContent.java From testarea-pdfbox2 with Apache License 2.0 | 6 votes |
/** * <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java"> * Rotate PDF around its center using PDFBox in java * </a> * <p> * This test shows how to rotate the page content and then move its crop box and * media box accordingly to make it appear as if the content was rotated around * the center of the crop box. * </p> */ @Test public void testRotateMoveBox() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") ) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getDocumentCatalog().getPages().get(0); PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false); Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0); cs.transform(matrix); cs.close(); PDRectangle cropBox = page.getCropBox(); float cx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2; float cy = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2; Point2D.Float newC = matrix.transformPoint(cx, cy); float tx = (float)newC.getX() - cx; float ty = (float)newC.getY() - cy; page.setCropBox(new PDRectangle(cropBox.getLowerLeftX() + tx, cropBox.getLowerLeftY() + ty, cropBox.getWidth(), cropBox.getHeight())); PDRectangle mediaBox = page.getMediaBox(); page.setMediaBox(new PDRectangle(mediaBox.getLowerLeftX() + tx, mediaBox.getLowerLeftY() + ty, mediaBox.getWidth(), mediaBox.getHeight())); document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-move-box.pdf")); } }
Example 2
Source File: PDCIDFontType2.java From gcs with Mozilla Public License 2.0 | 6 votes |
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null && (Float.compare(bbox.getLowerLeftX(), 0) != 0 || Float.compare(bbox.getLowerLeftY(), 0) != 0 || Float.compare(bbox.getUpperRightX(), 0) != 0 || Float.compare(bbox.getUpperRightY(), 0) != 0)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return ttf.getFontBBox(); }
Example 3
Source File: CloudyBorder.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Returns the updated <code>RD</code> entry for Square and Circle annotations. * * @return Annotation <code>RD</code> value. */ PDRectangle getRectDifference() { if (annotRect == null) { float d = (float)lineWidth / 2; return new PDRectangle(d, d, (float)lineWidth, (float)lineWidth); } PDRectangle re = (rectWithDiff != null) ? rectWithDiff : annotRect; float left = re.getLowerLeftX() - (float)bboxMinX; float bottom = re.getLowerLeftY() - (float)bboxMinY; float right = (float)bboxMaxX - re.getUpperRightX(); float top = (float)bboxMaxY - re.getUpperRightY(); return new PDRectangle(left, bottom, right - left, top - bottom); }
Example 4
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 6 votes |
/** * Transform the rectangle in order to match the page rotation * @param rect the rectangle. * @param page the page. * @return the transformed rectangle. */ public static PDRectangle transformToPageRotation( final PDRectangle rect, final PDPage page) { AffineTransform transform = transformToPageRotation(page); if (transform == null) { return rect; } float[] points = new float[] {rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()}; float[] rotatedPoints = new float[4]; transform.transform(points, 0, rotatedPoints, 0, 2); PDRectangle rotated = new PDRectangle(); rotated.setLowerLeftX(rotatedPoints[0]); rotated.setLowerLeftY(rotatedPoints[1]); rotated.setUpperRightX(rotatedPoints[2]); rotated.setUpperRightY(rotatedPoints[3]); return rotated; }
Example 5
Source File: RotatePageContent.java From testarea-pdfbox2 with Apache License 2.0 | 6 votes |
/** * <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java"> * Rotate PDF around its center using PDFBox in java * </a> * <p> * This test shows how to rotate the page content around the center of its crop box. * </p> */ @Test public void testRotateCenter() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") ) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getDocumentCatalog().getPages().get(0); PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false); PDRectangle cropBox = page.getCropBox(); float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2; float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2; cs.transform(Matrix.getTranslateInstance(tx, ty)); cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0)); cs.transform(Matrix.getTranslateInstance(-tx, -ty)); cs.close(); document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center.pdf")); } }
Example 6
Source File: PDType1CFont.java From gcs with Mozilla Public License 2.0 | 5 votes |
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null && (bbox.getLowerLeftX() != 0 || bbox.getLowerLeftY() != 0 || bbox.getUpperRightX() != 0 || bbox.getUpperRightY() != 0)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return genericFont.getFontBBox(); }
Example 7
Source File: PdfBoxFinder.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * The regions ({@link Rectangle2D} instances with coordinates according * to the PDFBox text extraction API, e.g. for initializing the regions of * a {@link PDFTextStripperByArea}) the {@link PdfBoxFinder} has recognized * on the current page. */ public Map<String, Rectangle2D> getRegions() { PDRectangle cropBox = getPage().getCropBox(); float xOffset = cropBox.getLowerLeftX(); float yOffset = cropBox.getUpperRightY(); Map<String, Rectangle2D> result = getBoxes(); for (Map.Entry<String, Rectangle2D> entry : result.entrySet()) { Rectangle2D box = entry.getValue(); Rectangle2D region = new Rectangle2D.Float(xOffset + (float)box.getX(), yOffset - (float)(box.getY() + box.getHeight()), (float)box.getWidth(), (float)box.getHeight()); entry.setValue(region); } return result; }
Example 8
Source File: LayerUtility.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Places the given form over the existing content of the indicated page (like an overlay). * The form is enveloped in a marked content section to indicate that it's part of an * optional content group (OCG), here used as a layer. This optional group is returned and * can be enabled and disabled through methods on {@link PDOptionalContentProperties}. * <p> * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make * sure that the graphics state is reset. * * @param targetPage the target page * @param form the form to place * @param transform the transformation matrix that controls the placement of your form. You'll * need this if your page has a crop box different than the media box, or if these have negative * coordinates, or if you want to scale or adjust your form. * @param layerName the name for the layer/OCG to produce * @return the optional content group that was generated for the form usage * @throws IOException if an I/O error occurs */ public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException { PDDocumentCatalog catalog = targetDoc.getDocumentCatalog(); PDOptionalContentProperties ocprops = catalog.getOCProperties(); if (ocprops == null) { ocprops = new PDOptionalContentProperties(); catalog.setOCProperties(ocprops); } if (ocprops.hasGroup(layerName)) { throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName); } PDRectangle cropBox = targetPage.getCropBox(); if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) { // PDFBOX-4044 LOG.warn("Negative cropBox " + cropBox + " and identity transform may make your form invisible"); } PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName); ocprops.addGroup(layer); PDPageContentStream contentStream = new PDPageContentStream( targetDoc, targetPage, AppendMode.APPEND, !DEBUG); contentStream.beginMarkedContent(COSName.OC, layer); contentStream.saveGraphicsState(); contentStream.transform(new Matrix(transform)); contentStream.drawForm(form); contentStream.restoreGraphicsState(); contentStream.endMarkedContent(); contentStream.close(); return layer; }
Example 9
Source File: PDAbstractAppearanceHandler.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Get a rectangle with the differences applied to each side. * * <p> * Creates a new rectangle with differences added to each side. If there are no valid * differences, then the original rectangle is returned. * * @param rectangle the rectangle. * @param differences the differences to apply. * @return the padded rectangle. */ PDRectangle applyRectDifferences(PDRectangle rectangle, float[] differences) { if (differences == null || differences.length != 4) { return rectangle; } return new PDRectangle(rectangle.getLowerLeftX() + differences[0], rectangle.getLowerLeftY() + differences[1], rectangle.getWidth() - differences[0] - differences[2], rectangle.getHeight() - differences[1] - differences[3]); }
Example 10
Source File: PDAbstractAppearanceHandler.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Get a rectangle enlarged by the differences. * * <p> * Creates a new rectangle with differences added to each side. If there are no valid * differences, then the original rectangle is returned. * * @param rectangle the rectangle. * @param differences the differences to apply. * @return the padded rectangle. */ PDRectangle addRectDifferences(PDRectangle rectangle, float[] differences) { if (differences == null || differences.length != 4) { return rectangle; } return new PDRectangle(rectangle.getLowerLeftX() - differences[0], rectangle.getLowerLeftY() - differences[1], rectangle.getWidth() + differences[0] + differences[2], rectangle.getHeight() + differences[1] + differences[3]); }
Example 11
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 5 votes |
/** * Return the quad points representation of the given rect. * * @param rect * the rectangle. * @param xOffset * the offset in x-direction to add. * @param yOffset * the offset in y-direction to add. * @return the quad points. */ public static float[] toQuadPoints(final PDRectangle rect, float xOffset, float yOffset) { float[] quads = new float[8]; quads[0] = rect.getLowerLeftX() + xOffset; // x1 quads[1] = rect.getUpperRightY() + yOffset; // y1 quads[2] = rect.getUpperRightX() + xOffset; // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = rect.getLowerLeftY() + yOffset; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 return quads; }
Example 12
Source File: PDFPrintable.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * This will find the CropBox with rotation applied, for this page by looking up the hierarchy * until it finds them. * * @return The CropBox at this level in the hierarchy. */ static PDRectangle getRotatedCropBox(PDPage page) { PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle == 90 || rotationAngle == 270) { return new PDRectangle(cropBox.getLowerLeftY(), cropBox.getLowerLeftX(), cropBox.getHeight(), cropBox.getWidth()); } else { return cropBox; } }
Example 13
Source File: PDType3Font.java From gcs with Mozilla Public License 2.0 | 5 votes |
private BoundingBox generateBoundingBox() { PDRectangle rect = getFontBBox(); if (rect.getLowerLeftX() == 0 && rect.getLowerLeftY() == 0 && rect.getUpperRightX() == 0 && rect.getUpperRightY() == 0) { // Plan B: get the max bounding box of the glyphs COSDictionary cp = getCharProcs(); for (COSName name : cp.keySet()) { COSBase base = cp.getDictionaryObject(name); if (base instanceof COSStream) { PDType3CharProc charProc = new PDType3CharProc(this, (COSStream) base); try { PDRectangle glyphBBox = charProc.getGlyphBBox(); if (glyphBBox == null) { continue; } rect.setLowerLeftX(Math.min(rect.getLowerLeftX(), glyphBBox.getLowerLeftX())); rect.setLowerLeftY(Math.min(rect.getLowerLeftY(), glyphBBox.getLowerLeftY())); rect.setUpperRightX(Math.max(rect.getUpperRightX(), glyphBBox.getUpperRightX())); rect.setUpperRightY(Math.max(rect.getUpperRightY(), glyphBBox.getUpperRightY())); } catch (IOException ex) { // ignore } } } } return new BoundingBox(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()); }
Example 14
Source File: PDType1Font.java From gcs with Mozilla Public License 2.0 | 5 votes |
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null && (bbox.getLowerLeftX() != 0 || bbox.getLowerLeftY() != 0 || bbox.getUpperRightX() != 0 || bbox.getUpperRightY() != 0)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return genericFont.getFontBBox(); }
Example 15
Source File: PDFTextStripper.java From gcs with Mozilla Public License 2.0 | 5 votes |
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); 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); } }
Example 16
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 5 votes |
/** * Return the quad points representation of the given rect. * * @param rect * the rectangle. * @param xOffset * the offset in x-direction to add. * @param yOffset * the offset in y-direction to add. * @return the quad points. */ public static float[] toQuadPoints(final PDRectangle rect, float xOffset, float yOffset) { float[] quads = new float[8]; quads[0] = rect.getLowerLeftX() + xOffset; // x1 quads[1] = rect.getUpperRightY() + yOffset; // y1 quads[2] = rect.getUpperRightX() + xOffset; // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = rect.getLowerLeftY() + yOffset; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 return quads; }
Example 17
Source File: PDFVisibleTextStripper.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
@Override public void processPage(PDPage page) throws IOException { PDRectangle pageSize = page.getCropBox(); lowerLeftX = pageSize.getLowerLeftX(); lowerLeftY = pageSize.getLowerLeftY(); super.processPage(page); }
Example 18
Source File: GenericSegment.java From pdfxtk with Apache License 2.0 | 4 votes |
public void setBoundingBox(PDRectangle bBox) { x1 = bBox.getLowerLeftX(); x2 = bBox.getUpperRightX(); y1 = bBox.getLowerLeftY(); y2 = bBox.getUpperRightY(); }
Example 19
Source File: RectanglesOverText.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="https://stackoverflow.com/questions/46080131/text-coordinates-when-stripping-from-pdfbox"> * Text coordinates when stripping from PDFBox * </a> * <br/> * <a href="https://download-a.akamaihd.net/files/media_mwb/b7/mwb_I_201711.pdf"> * mwb_I_201711.pdf * </a> * <p> * This test applies the OP's code to his example PDF file and indeed, there is an offset! * This is due to the <code>LegacyPDFStreamEngine</code> method <code>showGlyph</code> * which manipulates the text rendering matrix to make the lower left corner of the * crop box the origin. In the current version of this test, that offset is corrected, * see below. * </p> */ @Test public void testCoverTextByRectanglesMwbI201711() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("mwb_I_201711.pdf") ) { PDDocument doc = Loader.loadPDF(resource); myStripper stripper = new myStripper(); stripper.setStartPage(1); // fix it to first page just to test it stripper.setEndPage(1); stripper.getText(doc); TextLine line = stripper.lines.get(1); // the line i want to paint on float minx = -1; float maxx = -1; for (TextPosition pos: line.textPositions) { if (pos == null) continue; if (minx == -1 || pos.getTextMatrix().getTranslateX() < minx) { minx = pos.getTextMatrix().getTranslateX(); } if (maxx == -1 || pos.getTextMatrix().getTranslateX() > maxx) { maxx = pos.getTextMatrix().getTranslateX(); } } TextPosition firstPosition = line.textPositions.get(0); TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1); // corrected x and y PDRectangle cropBox = doc.getPage(0).getCropBox(); float x = minx + cropBox.getLowerLeftX(); float y = firstPosition.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY(); float w = (maxx - minx) + lastPosition.getWidth(); float h = lastPosition.getHeightDir(); PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false, true); contentStream.setNonStrokingColor(Color.RED); contentStream.addRect(x, y, w, h); contentStream.fill(); contentStream.close(); File fileout = new File(RESULT_FOLDER, "mwb_I_201711-withRectangles.pdf"); doc.save(fileout); doc.close(); } }
Example 20
Source File: PDAbstractAppearanceHandler.java From gcs with Mozilla Public License 2.0 | 2 votes |
/** * Get a padded rectangle. * * <p>Creates a new rectangle with padding applied to each side. * . * @param rectangle the rectangle. * @param padding the padding to apply. * @return the padded rectangle. */ PDRectangle getPaddedRectangle(PDRectangle rectangle, float padding) { return new PDRectangle(rectangle.getLowerLeftX() + padding, rectangle.getLowerLeftY() + padding, rectangle.getWidth() - 2 * padding, rectangle.getHeight() - 2 * padding); }