Java Code Examples for org.apache.pdfbox.pdmodel.PDPageContentStream#drawImage()
The following examples show how to use
org.apache.pdfbox.pdmodel.PDPageContentStream#drawImage() .
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: ImageCellDrawer.java From easytable with MIT License | 6 votes |
@Override @SneakyThrows public void drawContent(DrawingContext drawingContext) { final PDPageContentStream contentStream = drawingContext.getContentStream(); final float moveX = drawingContext.getStartingPoint().x; final Point2D.Float size = cell.getFitSize(); final Point2D.Float drawAt = new Point2D.Float(); // Handle horizontal alignment by adjusting the xOffset float xOffset = moveX + cell.getPaddingLeft(); if (cell.getSettings().getHorizontalAlignment() == HorizontalAlignment.RIGHT) { xOffset = moveX + (cell.getWidth() - (size.x + cell.getPaddingRight())); } else if (cell.getSettings().getHorizontalAlignment() == HorizontalAlignment.CENTER) { final float diff = (cell.getWidth() - size.x) / 2; xOffset = moveX + diff; } drawAt.x = xOffset; drawAt.y = drawingContext.getStartingPoint().y + getAdaptionForVerticalAlignment() - size.y; contentStream.drawImage(cell.getImage(), drawAt.x, drawAt.y, size.x, size.y); }
Example 3
Source File: DrawImage.java From testarea-pdfbox2 with Apache License 2.0 | 6 votes |
/** * <a href="https://stackoverflow.com/questions/58606529/pdf-size-too-large-generating-through-android-pdfdocument-and-while-using-pdfbo"> * PDF size too large generating through Android PDFDocument. And while using pdfbox it is cutting image in output * </a> * <p> * This code shows how to draw an image onto a page with * the image "default size". * </p> */ @Test public void testDrawImageToFitPage() throws IOException { try ( InputStream imageResource = getClass().getResourceAsStream("Willi-1.jpg")) { PDDocument document = new PDDocument(); PDImageXObject ximage = JPEGFactory.createFromStream(document,imageResource); PDPage page = new PDPage(new PDRectangle(ximage.getWidth(), ximage.getHeight())); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.drawImage(ximage, 0, 0); contentStream.close(); document.save(new File(RESULT_FOLDER, "Willi-1.pdf")); document.close(); } }
Example 4
Source File: PDFWriter.java From attic-polygene-java with Apache License 2.0 | 6 votes |
private void writeGraphPage( GraphDisplay graphDisplay ) throws IOException { File tFile = File.createTempFile( "envisage", ".png" ); graphDisplay.saveImage( new FileOutputStream( tFile ), "png", 1d ); BufferedImage img = ImageIO.read( tFile ); int w = img.getWidth(); int h = img.getHeight(); int inset = 40; PDRectangle pdRect = new PDRectangle( w + inset, h + inset ); PDPage page = new PDPage(); page.setMediaBox( pdRect ); doc.addPage( page ); PDImageXObject xImage = PDImageXObject.createFromFileByExtension( tFile, doc ); PDPageContentStream contentStream = new PDPageContentStream( doc, page ); contentStream.drawImage( xImage, ( pdRect.getWidth() - w ) / 2, ( pdRect.getHeight() - h ) / 2 ); contentStream.close(); }
Example 5
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 6
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 a fixed version of the the OP's original code, cf. * {@link #testImageAppendLikeShanky()}. It does not mirrors the image. * </p> */ @Test public void testImageAppendNoMirror() 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 + page.getCropBox().getLowerLeftX(); float y_adjusted = ( y_pos - h ) / 2 + page.getCropBox().getLowerLeftY(); contentStream.drawImage(pdImage, x_adjusted, y_adjusted, w, h); contentStream.close(); doc.save(new File(RESULT_FOLDER, "test-with-image-no-mirror.pdf")); doc.close(); } }
Example 7
Source File: AddImage.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/50988007/clip-an-image-with-pdfbox"> * Clip an image with PDFBOX * </a> * <p> * This test demonstrates how to clip an image and frame the clipping area. * </p> */ @SuppressWarnings("deprecation") @Test public void testImageAddClipped() throws IOException { try ( InputStream imageResource = getClass().getResourceAsStream("Willi-1.jpg") ) { PDDocument doc = new PDDocument(); PDImageXObject pdImage = PDImageXObject.createFromByteArray(doc, ByteStreams.toByteArray(imageResource), "Willi"); int w = pdImage.getWidth(); int h = pdImage.getHeight(); PDPage page = new PDPage(); doc.addPage(page); PDRectangle cropBox = page.getCropBox(); PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.setStrokingColor(25, 200, 25); contentStream.setLineWidth(4); contentStream.moveTo(cropBox.getLowerLeftX(), cropBox.getLowerLeftY() + h/2); contentStream.lineTo(cropBox.getLowerLeftX() + w/3, cropBox.getLowerLeftY() + 2*h/3); contentStream.lineTo(cropBox.getLowerLeftX() + w, cropBox.getLowerLeftY() + h/2); contentStream.lineTo(cropBox.getLowerLeftX() + w/3, cropBox.getLowerLeftY() + h/3); contentStream.closePath(); //contentStream.clip(); contentStream.appendRawCommands("W "); contentStream.stroke(); contentStream.drawImage(pdImage, cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), w, h); contentStream.close(); doc.save(new File(RESULT_FOLDER, "image-clipped.pdf")); doc.close(); } }
Example 8
Source File: AddImageSaveIncremental.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** @see #testAddImagesLikeUser11465050() */ void addImageLikeUser11465050(PDDocument document, PDImageXObject image) throws IOException { PDPage page = document.getPage(0); PDRectangle pageSize = page.getMediaBox(); PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true); contentStream.drawImage(image, pageSize.getLowerLeftX(), pageSize.getLowerLeftY(), pageSize.getWidth(), pageSize.getHeight()); contentStream.close(); page.getCOSObject().setNeedToBeUpdated(true); page.getResources().getCOSObject().setNeedToBeUpdated(true); document.getDocumentCatalog().getPages().getCOSObject().setNeedToBeUpdated(true); document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true); }
Example 9
Source File: AddImageSaveIncremental.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** @see #testAddImagesLikeUser11465050Improved() */ void addImageLikeUser11465050Improved(PDDocument document, PDImageXObject image) throws IOException { PDPage page = document.getPage(0); PDRectangle pageSize = page.getMediaBox(); PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true); contentStream.drawImage(image, pageSize.getLowerLeftX(), pageSize.getLowerLeftY(), pageSize.getWidth(), pageSize.getHeight()); contentStream.close(); page.getCOSObject().setNeedToBeUpdated(true); page.getResources().getCOSObject().setNeedToBeUpdated(true); page.getResources().getCOSObject().getCOSDictionary(COSName.XOBJECT).setNeedToBeUpdated(true); document.getDocumentCatalog().getPages().getCOSObject().setNeedToBeUpdated(true); document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true); }
Example 10
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 5 votes |
public static void drawImage(final BufferedImage image, final PDDocument document, final PDPageContentStream contentStream, Position upperLeft, final float width, final float height) throws IOException { PDImageXObject cachedImage = getCachedImage(document, image); float x = upperLeft.getX(); float y = upperLeft.getY() - height; contentStream.drawImage(cachedImage, x, y, width, height); }
Example 11
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 12
Source File: ReportImage.java From cat-boot with Apache License 2.0 | 3 votes |
/** * <p>Call this method to print images. <b>Make sure that the streams are closed before calling this method </b></p> * <p>Normal print method doesn't work since: http://stackoverflow.com/questions/9326245/how-to-exactly-position-an-image-inside-an-existing-pdf-page-using-pdfbox</p> * * @param document the pdDocument. * @param pageNumber page of image * @param x location of image * @param y location of image * @throws java.io.IOException in case there are problems at reading or writing the image */ public void printImage(PDDocument document, int pageNumber, float x, float y) throws IOException { PDImageXObject obj = LosslessFactory.createFromImage(document, img); PDPageContentStream currentStream = new PDPageContentStream(document, document.getDocumentCatalog().getPages().get(pageNumber), PDPageContentStream.AppendMode.APPEND, false); currentStream.drawImage(obj, x, y - height, width, height); currentStream.close(); }