Java Code Examples for org.apache.pdfbox.pdmodel.PDPageContentStream#transform()
The following examples show how to use
org.apache.pdfbox.pdmodel.PDPageContentStream#transform() .
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: PdfGenerator.java From blog-tutorials with MIT License | 6 votes |
public byte[] createPdf() throws IOException { try (PDDocument document = new PDDocument()) { PDPage page = new PDPage(PDRectangle.A4); page.setRotation(90); float pageWidth = page.getMediaBox().getWidth(); float pageHeight = page.getMediaBox().getHeight(); PDPageContentStream contentStream = new PDPageContentStream(document, page); PDImageXObject chartImage = JPEGFactory.createFromImage(document, createChart((int) pageHeight, (int) pageWidth)); contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0)); contentStream.drawImage(chartImage, 0, 0); contentStream.close(); document.addPage(page); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); document.save(byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); } }
Example 2
Source File: RotatedTextOnLine.java From testarea-pdfbox2 with Apache License 2.0 | 6 votes |
/** * <a href="https://stackoverflow.com/questions/52054396/rotate-text-in-pdfbox-with-java"> * Rotate text in pdfbox with java * </a> * <p> * This test shows how to show rotated text above a line. * </p> */ @Test public void testRotatedTextOnLineForCedrickKapema() throws IOException { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDPageContentStream cos = new PDPageContentStream(doc, page); cos.transform(Matrix.getRotateInstance(-Math.PI / 6, 100, 650)); cos.moveTo(0, 0); cos.lineTo(125, 0); cos.stroke(); cos.beginText(); String text = "0.72"; cos.newLineAtOffset(50, 5); cos.setFont(PDType1Font.HELVETICA_BOLD, 12); cos.showText(text); cos.endText(); cos.close(); doc.save(new File(RESULT_FOLDER, "TextOnLine.pdf")); doc.close(); }
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 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 4
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 * and then crop it to make all previously visible content fit. * </p> */ @Test public void testRotateCenterScale() 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); PDRectangle cropBox = page.getCropBox(); float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2; float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2; Rectangle rectangle = cropBox.transform(matrix).getBounds(); float scale = Math.min(cropBox.getWidth() / (float)rectangle.getWidth(), cropBox.getHeight() / (float)rectangle.getHeight()); cs.transform(Matrix.getTranslateInstance(tx, ty)); cs.transform(matrix); cs.transform(Matrix.getScaleInstance(scale, scale)); cs.transform(Matrix.getTranslateInstance(-tx, -ty)); cs.close(); document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center-scale.pdf")); } }
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 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 6
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 set the crop * box and media box to the bounding rectangle of the rotated page area. * </p> */ @Test public void testRotateExpandBox() 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(); Rectangle rectangle = cropBox.transform(matrix).getBounds(); PDRectangle newBox = new PDRectangle((float)rectangle.getX(), (float)rectangle.getY(), (float)rectangle.getWidth(), (float)rectangle.getHeight()); page.setCropBox(newBox); page.setMediaBox(newBox); document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-expand-box.pdf")); } }
Example 7
Source File: NativePdfBoxVisibleSignatureDrawer.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
private void rotateSignature(PDPageContentStream cs, PDRectangle rectangle, SignatureFieldDimensionAndPosition dimensionAndPosition) throws IOException { switch (dimensionAndPosition.getGlobalRotation()) { case ImageRotationUtils.ANGLE_90: // pdfbox rotates in the opposite way cs.transform(Matrix.getRotateInstance(Math.toRadians(ImageRotationUtils.ANGLE_270), 0, 0)); cs.transform(Matrix.getTranslateInstance(-rectangle.getHeight(), 0)); break; case ImageRotationUtils.ANGLE_180: cs.transform(Matrix.getRotateInstance(Math.toRadians(ImageRotationUtils.ANGLE_180), 0, 0)); cs.transform(Matrix.getTranslateInstance(-rectangle.getWidth(), -rectangle.getHeight())); break; case ImageRotationUtils.ANGLE_270: cs.transform(Matrix.getRotateInstance(Math.toRadians(ImageRotationUtils.ANGLE_90), 0, 0)); cs.transform(Matrix.getTranslateInstance(0, -rectangle.getWidth())); break; case ImageRotationUtils.ANGLE_360: // do nothing break; default: throw new IllegalStateException(ImageRotationUtils.SUPPORTED_ANGLES_ERROR_MESSAGE); } }
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: AddImage.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/49958604/draw-image-at-mid-position-using-pdfbox-java"> * Draw image at mid position using pdfbox Java * </a> * <p> * This is the OP's original code. It mirrors the image. * This can be fixed as shown in {@link #testImageAppendNoMirror()}. * </p> */ @Test public void testImageAppendLikeShanky() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/pdfbox2/sign/test.pdf"); InputStream imageResource = getClass().getResourceAsStream("Willi-1.jpg") ) { PDDocument doc = Loader.loadPDF(resource); PDImageXObject pdImage = PDImageXObject.createFromByteArray(doc, ByteStreams.toByteArray(imageResource), "Willi"); int w = pdImage.getWidth(); int h = pdImage.getHeight(); PDPage page = doc.getPage(0); PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true); float x_pos = page.getCropBox().getWidth(); float y_pos = page.getCropBox().getHeight(); float x_adjusted = ( x_pos - w ) / 2; float y_adjusted = ( y_pos - h ) / 2; Matrix mt = new Matrix(1f, 0f, 0f, -1f, page.getCropBox().getLowerLeftX(), page.getCropBox().getUpperRightY()); contentStream.transform(mt); contentStream.drawImage(pdImage, x_adjusted, y_adjusted, w, h); contentStream.close(); doc.save(new File(RESULT_FOLDER, "test-with-image-shanky.pdf")); doc.close(); } }
Example 10
Source File: JoinPages.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * @see #testJoinSmallAndBig() */ void join(PDDocument target, PDDocument topSource, PDDocument bottomSource) throws IOException { LayerUtility layerUtility = new LayerUtility(target); PDFormXObject topForm = layerUtility.importPageAsForm(topSource, 0); PDFormXObject bottomForm = layerUtility.importPageAsForm(bottomSource, 0); float height = topForm.getBBox().getHeight() + bottomForm.getBBox().getHeight(); float width, topMargin, bottomMargin; if (topForm.getBBox().getWidth() > bottomForm.getBBox().getWidth()) { width = topForm.getBBox().getWidth(); topMargin = 0; bottomMargin = (topForm.getBBox().getWidth() - bottomForm.getBBox().getWidth()) / 2; } else { width = bottomForm.getBBox().getWidth(); topMargin = (bottomForm.getBBox().getWidth() - topForm.getBBox().getWidth()) / 2; bottomMargin = 0; } PDPage targetPage = new PDPage(new PDRectangle(width, height)); target.addPage(targetPage); PDPageContentStream contentStream = new PDPageContentStream(target, targetPage); if (bottomMargin != 0) contentStream.transform(Matrix.getTranslateInstance(bottomMargin, 0)); contentStream.drawForm(bottomForm); contentStream.transform(Matrix.getTranslateInstance(topMargin - bottomMargin, bottomForm.getBBox().getHeight())); contentStream.drawForm(topForm); contentStream.close(); }
Example 11
Source File: RotatePageContent.java From testarea-pdfbox2 with Apache License 2.0 | 5 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> * Indeed, using the code of the OP the image mostly is rotated out of the page area. * </p> */ @Test public void testRotateLikeSagar() 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); cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0)); cs.close(); document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-like-sagar.pdf")); } }
Example 12
Source File: NativePdfBoxVisibleSignatureDrawer.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
/** * Draws the given image with specified dimension and position * @param cs * {@link PDPageContentStream} current stream * @param doc * {@link PDDocument} to draw the picture on * @param dimensionAndPosition * {@link SignatureFieldDimensionAndPosition} size and position to place the picture to * @param image * {@link DSSDocument} image to draw * @throws IOException * in case of error */ private void setImage(PDPageContentStream cs, PDDocument doc, SignatureFieldDimensionAndPosition dimensionAndPosition, DSSDocument image) throws IOException { if (image != null) { try (InputStream is = image.openStream()) { cs.saveGraphicsState(); byte[] bytes = IOUtils.toByteArray(is); PDImageXObject imageXObject = PDImageXObject.createFromByteArray(doc, bytes, image.getName()); // divide to scale factor, because PdfBox due to the matrix transformation also changes position parameters of the image float xAxis = dimensionAndPosition.getImageX(); if (parameters.getTextParameters() != null) xAxis *= dimensionAndPosition.getxDpiRatio(); float yAxis = dimensionAndPosition.getImageY(); if (parameters.getTextParameters() != null) yAxis *= dimensionAndPosition.getyDpiRatio(); float width = getWidth(dimensionAndPosition); float height = getHeight(dimensionAndPosition); cs.drawImage(imageXObject, xAxis, yAxis, width, height); cs.transform(Matrix.getRotateInstance(((double) 360 - ImageRotationUtils.getRotation(parameters.getRotation())), width, height)); cs.restoreGraphicsState(); } } }
Example 13
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 4 votes |
public static void transform(final PDPageContentStream contentStream, float a, float b, float c, float d, float e, float f) throws IOException { contentStream.transform(new Matrix(a, b, c, d, e, f)); }
Example 14
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 4 votes |
public static void moveTextPosition( final PDPageContentStream contentStream, final float x, final float y) throws IOException { contentStream.transform(new Matrix(1, 0, 0, 1, x, y)); }
Example 15
Source File: PdfManipulator.java From estatio with Apache License 2.0 | 4 votes |
private byte[] stamp( final PDDocument onePageDoc, final List<Line> leftLines, final List<Line> rightLines) throws IOException { PDPage pdPage = onePageDoc.getPage(0); final float pageHeight = pdPage.getMediaBox().getHeight(); final PDPageContentStream prependStream = new PDPageContentStream(onePageDoc, pdPage, PDPageContentStream.AppendMode.PREPEND, false); try { prependStream.transform(Matrix.getScaleInstance(SCALE_X, SCALE_Y)); prependStream.transform(Matrix.getTranslateInstance(0f, pageHeight * (1 - SCALE_Y))); } finally { prependStream.close(); } final PDPageContentStream appendStream = new PDPageContentStream(onePageDoc, pdPage, PDPageContentStream.AppendMode.APPEND, true, true); try { final int leftLineSize = leftLines.size(); final int rightLineSize = rightLines.size(); final float height = Math.max(leftLineSize, rightLineSize) * TEXT_LINE_HEIGHT + BOX_Y_PADDING; final float y = 36; float x = X_MARGIN_LEFT; float yLine = addLines(x, y, height, leftLines, appendStream); String hyperlink = leftLines.get(leftLineSize - 1).hyperlink; addHyperlink(x, yLine + TEXT_LINE_HEIGHT - 6, hyperlink, pdPage); x = X_MARGIN_RIGHT; addLines(x, y, height, rightLines, appendStream); } finally { appendStream.close(); } return asBytes(onePageDoc); }