Java Code Examples for org.apache.pdfbox.util.Matrix#transformPoint()
The following examples show how to use
org.apache.pdfbox.util.Matrix#transformPoint() .
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: PDRectangle.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Returns a path which represents this rectangle having been transformed by the given matrix. Note that the * resulting path need not be rectangular. * * @param matrix the matrix to be used for the transformation. * * @return the resulting path. */ public GeneralPath transform(Matrix matrix) { float x1 = getLowerLeftX(); float y1 = getLowerLeftY(); float x2 = getUpperRightX(); float y2 = getUpperRightY(); Point2D.Float p0 = matrix.transformPoint(x1, y1); Point2D.Float p1 = matrix.transformPoint(x2, y1); Point2D.Float p2 = matrix.transformPoint(x2, y2); Point2D.Float p3 = matrix.transformPoint(x1, y2); GeneralPath path = new GeneralPath(); path.moveTo(p0.getX(), p0.getY()); path.lineTo(p1.getX(), p1.getY()); path.lineTo(p2.getX(), p2.getY()); path.lineTo(p3.getX(), p3.getY()); path.closePath(); return path; }
Example 2
Source File: AppearanceGeneratorHelper.java From gcs with Mozilla Public License 2.0 | 6 votes |
private PDAppearanceStream prepareNormalAppearanceStream(PDAnnotationWidget widget) { PDAppearanceStream appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument()); // Calculate the entries for the bounding box and the transformation matrix // settings for the appearance stream int rotation = resolveRotation(widget); PDRectangle rect = widget.getRectangle(); Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0); Point2D.Float point2D = matrix.transformPoint(rect.getWidth(), rect.getHeight()); PDRectangle bbox = new PDRectangle(Math.abs((float) point2D.getX()), Math.abs((float) point2D.getY())); appearanceStream.setBBox(bbox); AffineTransform at = calculateMatrix(bbox, rotation); if (!at.isIdentity()) { appearanceStream.setMatrix(at); } appearanceStream.setFormType(1); appearanceStream.setResources(new PDResources()); return appearanceStream; }
Example 3
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 4
Source File: GouraudShadingContext.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Read a vertex from the bit input stream performs interpolations. * * @param input bit input stream * @param maxSrcCoord max value for source coordinate (2^bits-1) * @param maxSrcColor max value for source color (2^bits-1) * @param rangeX dest range for X * @param rangeY dest range for Y * @param colRangeTab dest range array for colors * @param matrix the pattern matrix concatenated with that of the parent content stream * @return a new vertex with the flag and the interpolated values * @throws IOException if something went wrong */ protected Vertex readVertex(ImageInputStream input, long maxSrcCoord, long maxSrcColor, PDRange rangeX, PDRange rangeY, PDRange[] colRangeTab, Matrix matrix, AffineTransform xform) throws IOException { float[] colorComponentTab = new float[numberOfColorComponents]; long x = input.readBits(bitsPerCoordinate); long y = input.readBits(bitsPerCoordinate); float dstX = interpolate(x, maxSrcCoord, rangeX.getMin(), rangeX.getMax()); float dstY = interpolate(y, maxSrcCoord, rangeY.getMin(), rangeY.getMax()); LOG.debug("coord: " + String.format("[%06X,%06X] -> [%f,%f]", x, y, dstX, dstY)); Point2D p = matrix.transformPoint(dstX, dstY); xform.transform(p, p); for (int n = 0; n < numberOfColorComponents; ++n) { int color = (int) input.readBits(bitsPerColorComponent); colorComponentTab[n] = interpolate(color, maxSrcColor, colRangeTab[n].getMin(), colRangeTab[n].getMax()); LOG.debug("color[" + n + "]: " + color + "/" + String.format("%02x", color) + "-> color[" + n + "]: " + colorComponentTab[n]); } // "Each set of vertex data shall occupy a whole number of bytes. // If the total number of bits required is not divisible by 8, the last data byte // for each vertex is padded at the end with extra bits, which shall be ignored." int bitOffset = input.getBitOffset(); if (bitOffset != 0) { input.readBits(8 - bitOffset); } return new Vertex(p, colorComponentTab); }
Example 5
Source File: PageVerticalAnalyzer.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
@Override public void drawImage(PDImage pdImage) throws IOException { Matrix ctm = getGraphicsState().getCurrentTransformationMatrix(); Section section = null; for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { Point2D.Float point = ctm.transformPoint(x, y); if (section == null) section = new Section(point.y); else section.extendTo(point.y); } } addVerticalUseSection(section.from, section.to); }
Example 6
Source File: EditPageContent.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="http://stackoverflow.com/questions/38498431/how-to-remove-filtered-content-from-a-pdf-with-itext"> * How to remove filtered content from a PDF with iText * </a> * <br/> * <a href="https://1drv.ms/b/s!AmNST-TRoPSemi2k0UnGFsjQM1Yt"> * document.pdf * </a> * <p> * This test shows how to remove text filtered by actual font size. * </p> */ @Test public void testRemoveBigTextDocument() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("document.pdf"); PDDocument document = Loader.loadPDF(resource)) { for (PDPage page : document.getDocumentCatalog().getPages()) { PdfContentStreamEditor identity = new PdfContentStreamEditor(document, page) { @Override protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException { String operatorString = operator.getName(); if (TEXT_SHOWING_OPERATORS.contains(operatorString)) { float fs = getGraphicsState().getTextState().getFontSize(); Matrix matrix = getTextMatrix().multiply(getGraphicsState().getCurrentTransformationMatrix()); Point2D.Float transformedFsVector = matrix.transformPoint(0, fs); Point2D.Float transformedOrigin = matrix.transformPoint(0, 0); double transformedFs = transformedFsVector.distance(transformedOrigin); if (transformedFs > 100) return; } super.write(contentStreamWriter, operator, operands); } final List<String> TEXT_SHOWING_OPERATORS = Arrays.asList("Tj", "'", "\"", "TJ"); }; identity.processPage(page); } document.save(new File(RESULT_FOLDER, "document-noBigText.pdf")); } }
Example 7
Source File: EditPageContent.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/61202822/remove-large-tokens-from-pdf-using-pdfbox-or-equivalent-library"> * Remove Large Tokens from PDF using PDFBox or equivalent library * </a> * <br/> * <a href="https://drive.google.com/file/d/184waC6PjjDi8yolIZN5R-6vgWGR5SvKl/view?usp=sharing"> * kommers_annons_elite.pdf * </a> * <p> * This test shows how to remove text filtered by actual font size. * </p> */ @Test public void testRemoveBigTextKommersAnnonsElite() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("kommers_annons_elite.pdf"); PDDocument document = Loader.loadPDF(resource)) { PDPage page = document.getPage(0); PdfContentStreamEditor editor = new PdfContentStreamEditor(document, page) { @Override protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException { String operatorString = operator.getName(); if (TEXT_SHOWING_OPERATORS.contains(operatorString)) { float fs = getGraphicsState().getTextState().getFontSize(); Matrix matrix = getTextMatrix().multiply(getGraphicsState().getCurrentTransformationMatrix()); Point2D.Float transformedFsVector = matrix.transformPoint(0, fs); Point2D.Float transformedOrigin = matrix.transformPoint(0, 0); double transformedFs = transformedFsVector.distance(transformedOrigin); if (transformedFs > 50) return; } super.write(contentStreamWriter, operator, operands); } final List<String> TEXT_SHOWING_OPERATORS = Arrays.asList("Tj", "'", "\"", "TJ"); }; editor.processPage(page); document.save(new File(RESULT_FOLDER, "kommers_annons_elite-noBigText.pdf")); } }
Example 8
Source File: PatchMeshesShadingContext.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Read a single patch from a data stream, a patch contains information of its coordinates and * color parameters. * * @param input the image source data stream * @param isFree whether this is a free patch * @param implicitEdge implicit edge when a patch is not free, otherwise it's not used * @param implicitCornerColor implicit colors when a patch is not free, otherwise it's not used * @param maxSrcCoord the maximum coordinate value calculated from source data * @param maxSrcColor the maximum color value calculated from source data * @param rangeX range for coordinate x * @param rangeY range for coordinate y * @param colRange range for color * @param matrix the pattern matrix concatenated with that of the parent content stream * @param xform transformation for user to device space * @param controlPoints number of control points, 12 for type 6 shading and 16 for type 7 shading * @return a single patch * @throws IOException when something went wrong */ protected Patch readPatch(ImageInputStream input, boolean isFree, Point2D[] implicitEdge, float[][] implicitCornerColor, long maxSrcCoord, long maxSrcColor, PDRange rangeX, PDRange rangeY, PDRange[] colRange, Matrix matrix, AffineTransform xform, int controlPoints) throws IOException { float[][] color = new float[4][numberOfColorComponents]; Point2D[] points = new Point2D[controlPoints]; int pStart = 4, cStart = 2; if (isFree) { pStart = 0; cStart = 0; } else { points[0] = implicitEdge[0]; points[1] = implicitEdge[1]; points[2] = implicitEdge[2]; points[3] = implicitEdge[3]; for (int i = 0; i < numberOfColorComponents; i++) { color[0][i] = implicitCornerColor[0][i]; color[1][i] = implicitCornerColor[1][i]; } } try { for (int i = pStart; i < controlPoints; i++) { long x = input.readBits(bitsPerCoordinate); long y = input.readBits(bitsPerCoordinate); float px = interpolate(x, maxSrcCoord, rangeX.getMin(), rangeX.getMax()); float py = interpolate(y, maxSrcCoord, rangeY.getMin(), rangeY.getMax()); Point2D p = matrix.transformPoint(px, py); xform.transform(p, p); points[i] = p; } for (int i = cStart; i < 4; i++) { for (int j = 0; j < numberOfColorComponents; j++) { long c = input.readBits(bitsPerColorComponent); color[i][j] = interpolate(c, maxSrcColor, colRange[j].getMin(), colRange[j].getMax()); } } } catch (EOFException ex) { LOG.debug("EOF"); return null; } return generatePatch(points, color); }