private boolean drawImage( Image img, Image mask, AffineTransform xform, Color bgColor, ImageObserver obs) { if (xform == null) xform = new AffineTransform(); else xform = new AffineTransform(xform); xform.translate(0, img.getHeight(obs)); xform.scale(img.getWidth(obs), img.getHeight(obs)); AffineTransform inverse = this.normalizeMatrix(); AffineTransform flipper = AffineTransform.getScaleInstance(1, -1); inverse.concatenate(xform); inverse.concatenate(flipper); double[] mx = new double[6]; inverse.getMatrix(mx); if (currentFillGState != 255) { PdfGState gs = fillGState[255]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(1); fillGState[255] = gs; } cb.setGState(gs); } try { com.lowagie.text.Image image = null; if (!convertImagesToJPEG) { image = com.lowagie.text.Image.getInstance(img, bgColor); } else { BufferedImage scaled = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g3 = scaled.createGraphics(); g3.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null); g3.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault()); iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwparam.setCompressionQuality(jpegQuality); // Set here your compression rate ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName("jpg").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); iw.setOutput(ios); iw.write(null, new IIOImage(scaled, null, null), iwparam); iw.dispose(); ios.close(); scaled.flush(); scaled = null; image = com.lowagie.text.Image.getInstance(baos.toByteArray()); } if (mask != null) { com.lowagie.text.Image msk = com.lowagie.text.Image.getInstance(mask, null, true); msk.makeMask(); msk.setInverted(true); image.setImageMask(msk); } cb.addImage( image, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); Object url = getRenderingHint(HyperLinkKey.KEY_INSTANCE); if (url != null && !url.equals(HyperLinkKey.VALUE_HYPERLINKKEY_OFF)) { PdfAction action = new PdfAction(url.toString()); cb.setAction( action, (float) mx[4], (float) mx[5], (float) (mx[0] + mx[4]), (float) (mx[3] + mx[5])); } } catch (Exception ex) { throw new IllegalArgumentException(); } if (currentFillGState != 255) { PdfGState gs = fillGState[currentFillGState]; cb.setGState(gs); } return true; }
/** * Applying masks to images. * * @param args no arguments needed */ public static void main(String[] args) { System.out.println("masked images"); Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("maskedImages.pdf")); document.open(); Paragraph p = new Paragraph("Some text behind a masked image."); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); PdfContentByte cb = writer.getDirectContent(); byte maskr[] = { (byte) 0x3c, (byte) 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3, (byte) 0xe7, (byte) 0x7e, (byte) 0x3c }; Image mask = Image.getInstance(8, 8, 1, 1, maskr); mask.makeMask(); mask.setInverted(true); Image image = Image.getInstance("otsoe.jpg"); image.setImageMask(mask); image.setAbsolutePosition(60, 550); // explicit masking cb.addImage(image); // stencil masking cb.setRGBColorFill(255, 0, 0); cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 450); cb.setRGBColorFill(0, 255, 0); cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 400); cb.setRGBColorFill(0, 0, 255); cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 350); document.close(); } catch (Exception de) { de.printStackTrace(); } }