Java Code Examples for com.itextpdf.text.pdf.PdfContentByte#addImage()
The following examples show how to use
com.itextpdf.text.pdf.PdfContentByte#addImage() .
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: AddRotatedImage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
void addRotatedImage(PdfContentByte contentByte, Image image, float x, float y, float width, float height, float rotation) throws DocumentException { // Draw image at x,y without rotation contentByte.addImage(image, width, 0, 0, height, x, y); // Draw image as if the previous image was rotated around its center // Image starts out being 1x1 with origin in lower left // Move origin to center of image AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5); // Stretch it to its dimensions AffineTransform B = AffineTransform.getScaleInstance(width, height); // Rotate it AffineTransform C = AffineTransform.getRotateInstance(rotation); // Move it to have the same center as above AffineTransform D = AffineTransform.getTranslateInstance(x + width/2, y + height/2); // Concatenate AffineTransform M = (AffineTransform) A.clone(); M.preConcatenate(B); M.preConcatenate(C); M.preConcatenate(D); //Draw contentByte.addImage(image, M); }
Example 2
Source File: SimpleRedactionTest.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
static byte[] createRotatedImagePdf() throws DocumentException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); PdfContentByte directContent = writer.getDirectContent(); BufferedImage bim = new BufferedImage(1000, 250, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bim.createGraphics(); g2d.setColor(Color.BLUE); g2d.fillRect(0, 0, 500, 500); g2d.dispose(); Image image = Image.getInstance(bim, null); directContent.addImage(image, 0, 500, -500, 0, 550, 50); document.close(); return baos.toByteArray(); }
Example 3
Source File: StampInLayer.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
public static byte[] stampLayer(InputStream _pdfFile, Image iImage, int x, int y, String layername, boolean readLayers) throws IOException, DocumentException { PdfReader reader = new PdfReader(_pdfFile); try ( ByteArrayOutputStream ms = new ByteArrayOutputStream() ) { PdfStamper stamper = new PdfStamper(reader, ms); //Don't delete otherwise the stamper flattens the layers if (readLayers) stamper.getPdfLayers(); PdfLayer logoLayer = new PdfLayer(layername, stamper.getWriter()); PdfContentByte cb = stamper.getUnderContent(1); cb.beginLayer(logoLayer); //300dpi iImage.scalePercent(24f); iImage.setAbsolutePosition(x, y); cb.addImage(iImage); cb.endLayer(); stamper.close(); return (ms.toByteArray()); } }
Example 4
Source File: BinaryTransparency.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
private static void addBackground(PdfWriter writer) throws BadElementException, MalformedURLException, IOException, DocumentException { PdfContentByte canvas = writer.getDirectContentUnder(); canvas.saveState(); canvas.addImage(bkgnd); canvas.restoreState(); }
Example 5
Source File: SimpleRedactionTest.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
static byte[] createClippingTextPdf() throws DocumentException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); PdfContentByte directContent = writer.getDirectContent(); directContent.beginText(); directContent.setTextRenderingMode(PdfPatternPainter.TEXT_RENDER_MODE_CLIP); directContent.setTextMatrix(AffineTransform.getTranslateInstance(100, 400)); directContent.setFontAndSize(BaseFont.createFont(), 100); directContent.showText("Test"); directContent.endText(); BufferedImage bim = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bim.createGraphics(); g2d.setColor(Color.BLUE); g2d.fillRect(0, 0, 500, 500); g2d.dispose(); Image image = Image.getInstance(bim, null); directContent.addImage(image, 500, 0, 0, 599, 50, 50); document.close(); return baos.toByteArray(); }
Example 6
Source File: AddRotatedImage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/39364197/how-to-rotate-around-the-image-center-by-itext"> * How to rotate around the image center by itext? * </a> * <p> * This test draws an image at given coordinates without rotation and then again * as if that image was rotated around its center by some angle. * </p> */ @Test public void testAddRotatedImage() throws IOException, DocumentException { try ( FileOutputStream stream = new FileOutputStream(new File(RESULT_FOLDER, "rotatedImage.pdf")) ) { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, stream); document.open(); PdfContentByte contentByte = writer.getDirectContent(); int x = 200; int y = 300; float rotate = (float) Math.PI / 3; try (InputStream imageStream = getClass().getResourceAsStream("/mkl/testarea/itext5/layer/Willi-1.jpg")) { Image image = Image.getInstance(IOUtils.toByteArray(imageStream)); // Draw image at x,y without rotation contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y); // Draw image as if the previous image was rotated around its center // Image starts out being 1x1 with origin in lower left // Move origin to center of image AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5); // Stretch it to its dimensions AffineTransform B = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight()); // Rotate it AffineTransform C = AffineTransform.getRotateInstance(rotate); // Move it to have the same center as above AffineTransform D = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2); // Concatenate AffineTransform M = (AffineTransform) A.clone(); M.preConcatenate(B); M.preConcatenate(C); M.preConcatenate(D); //Draw contentByte.addImage(image, M); } document.close(); } }
Example 7
Source File: AddRotatedImage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/39364197/how-to-rotate-around-the-image-center-by-itext"> * How to rotate around the image center by itext? * </a> * <p> * This test draws an image at given coordinates without rotation and then again * as if that image was flipped and rotated around its center by some angle. * </p> */ @Test public void testAddRotatedFlippedImage() throws IOException, DocumentException { try ( FileOutputStream stream = new FileOutputStream(new File(RESULT_FOLDER, "rotatedFlippedImage.pdf")) ) { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, stream); document.open(); PdfContentByte contentByte = writer.getDirectContent(); int x = 200; int y = 300; float rotate = (float) Math.PI / 3; try (InputStream imageStream = getClass().getResourceAsStream("/mkl/testarea/itext5/layer/Willi-1.jpg")) { Image image = Image.getInstance(IOUtils.toByteArray(imageStream)); // Draw image at x,y without rotation contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y); // Draw image as if the previous image was flipped and rotated around its center // Image starts out being 1x1 with origin in lower left // Move origin to center of image AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5); // Flip it horizontally AffineTransform B = new AffineTransform(-1, 0, 0, 1, 0, 0); // Stretch it to its dimensions AffineTransform C = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight()); // Rotate it AffineTransform D = AffineTransform.getRotateInstance(rotate); // Move it to have the same center as above AffineTransform E = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2); // Concatenate AffineTransform M = (AffineTransform) A.clone(); M.preConcatenate(B); M.preConcatenate(C); M.preConcatenate(D); M.preConcatenate(E); //Draw contentByte.addImage(image, M); } document.close(); } }
Example 8
Source File: WatermarkPdfTests.java From kbase-doc with Apache License 2.0 | 4 votes |
/** * pdf 用图片加水印 * @author eko.zhan at 2018年9月2日 下午1:44:58 * @throws FileNotFoundException * @throws IOException * @throws DocumentException */ @Test public void testVisioAsPdfWithImg() throws FileNotFoundException, IOException, DocumentException{ File inputFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx.vsdx"); File outputFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx_libreoffice.pdf"); if (!outputFile.exists()) { convert(inputFile, outputFile); } File destFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx_libreoffice_watermark.pdf"); final String IMG = "D:\\Xiaoi\\logo\\logo.png"; //转换成 pdf 后利用 itext 加水印 PdfReader reader = new PdfReader(new FileInputStream(outputFile)); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile)); int pageNo = reader.getNumberOfPages(); // text watermark Font f = new Font(FontFamily.HELVETICA, 30); Phrase p = new Phrase("Xiaoi Robot Image", f); // image watermark Image img = Image.getInstance(IMG); float w = img.getScaledWidth(); float h = img.getScaledHeight(); // transparency PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.5f); // properties PdfContentByte over; Rectangle pagesize; float x, y; // loop over every page for (int i = 1; i <= pageNo; i++) { pagesize = reader.getPageSizeWithRotation(i); x = (pagesize.getLeft() + pagesize.getRight()) / 2; y = (pagesize.getTop() + pagesize.getBottom()) / 2; over = stamper.getOverContent(i); over.saveState(); over.setGState(gs1); if (i % 2 == 1) ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 0); else over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2)); over.restoreState(); } stamper.close(); reader.close(); }
Example 9
Source File: TrpPdfDocument.java From TranskribusCore with GNU General Public License v3.0 | 4 votes |
private void addTextAndImage(PcGtsType pc, int cutoffLeft, int cutoffTop, Image img, boolean imageOnly, ExportCache cache) throws DocumentException, IOException { lineAndColorList.clear(); PdfContentByte cb = writer.getDirectContentUnder(); cb.setColorFill(BaseColor.BLACK); cb.setColorStroke(BaseColor.BLACK); //BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", BaseFont.NOT_EMBEDDED); if (!imageOnly){ cb.beginLayer(ocrLayer); //cb.setFontAndSize(bfArial, 32); cb.setFontAndSize(mainExportBaseFont, 32); List<TrpRegionType> regions = pc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion(); /* * use reading order comparator for sorting since at this time reading order is more trustable * other sorting is not transitive and seldomly produces "Comparison violates its general contract" exception */ // Collections.sort(regions, new TrpElementReadingOrderComparator<RegionType>(true)); TrpShapeTypeUtils.sortShapesByReadingOrderOrCoordinates(regions); for(RegionType r : regions){ //TODO add paths for tables etc. if (r instanceof TrpTableRegionType){ exportTable(r, cb, cutoffLeft, cutoffTop, false, cache); } else if(r instanceof TextRegionType){ TextRegionType tr = (TextRegionType)r; //PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX(); addTextFromTextRegion(tr, cb, cutoffLeft, cutoffTop, mainExportBaseFont, cache); } } //scale after calculating lineMeanHeightForAllRegions //lineMeanHeight = lineMeanHeight/scaleFactorX; cb.endLayer(); } cb.beginLayer(imgLayer); cb.addImage(img); cb.endLayer(); if (highlightTags || highlightArticles){ highlightAllTagsOnImg(lineAndColorList, cb, cutoffLeft, cutoffTop); } /* * draw tag lines */ // addTocLinks(doc, page,cutoffTop); }