@Test public void testSimple() throws FileNotFoundException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "transparency.pdf"))); writer.setCompressionLevel(0); document.open(); PdfContentByte content = writer.getDirectContent(); content.setRGBColorStroke(0, 255, 0); for (int y = 0; y <= 400; y+= 10) { content.moveTo(0, y); content.lineTo(500, y); } for (int x = 0; x <= 500; x+= 10) { content.moveTo(x, 0); content.lineTo(x, 400); } content.stroke(); content.saveState(); PdfGState state = new PdfGState(); state.setFillOpacity(0.5f); content.setGState(state); content.setRGBColorFill(255, 0, 0); content.moveTo(162, 86); content.lineTo(162, 286); content.lineTo(362, 286); content.lineTo(362, 86); content.closePath(); //content.fillStroke(); content.fill(); content.restoreState(); document.close(); }
public static void applyWatermarkOnAllPages( PdfReader reader, PdfStamper stamper, String text, BaseColor color, float opacity ) throws DocumentException, IOException { int numberOfPages = reader.getNumberOfPages(); PdfGState gs = new PdfGState(); gs.setFillOpacity(opacity); for (int i = 1; i <= numberOfPages; i++) { Rectangle pageSize = reader.getPageSize(i); int pageRotation = reader.getPageRotation(i); boolean isLandscape = false; if(pageRotation > 0){ isLandscape = true; } int pageDiagonalSize = getDiagonalSize(pageSize); int watermarkAngle = getDiagonalAngle(pageSize, pageDiagonalSize, isLandscape); LinkedHashSet<Integer> fontSizeSet = new LinkedHashSet<Integer>(); float fontSize = determineFontSize(pageDiagonalSize, a4WatermarkFontSize, fontSizeSet); fontSize = fontSize - 1; int cornerPlacement = getCornerPlacementDiagonal(fontSize); PdfContentByte underContent = stamper.getUnderContent(i); underContent.setGState(gs); underContent.setFontAndSize(getWaterkmarkFont(), fontSize); underContent.setColorFill(color); underContent.beginText(); underContent.showTextAligned( Element.ALIGN_BASELINE, text, cornerPlacement/2, cornerPlacement/2, watermarkAngle ); underContent.endText(); } }
/** * <a href="http://stackoverflow.com/questions/34734669/define-background-color-and-transparency-of-link-annotation-in-pdf"> * Define background color and transparency of link annotation in PDF * </a> * <p> * This test creates a link annotation with custom appearance. Adobe Reader chooses * to ignore it but other viewers use it. Interestingly Adobe Acrobat export-as-image * does use the custom appearance... * </p> */ @Test public void testCreateLinkWithAppearance() throws IOException, DocumentException { Document doc = new Document(); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(RESULT_FOLDER, "custom-link.appearance.pdf"))); writer.setCompressionLevel(0); doc.open(); BaseFont baseFont = BaseFont.createFont(); int fontSize = 15; doc.add(new Paragraph("Hello", new Font(baseFont, fontSize))); PdfContentByte content = writer.getDirectContent(); String text = "Test"; content.setFontAndSize(baseFont, fontSize); content.beginText(); content.moveText(100, 500); content.showText(text); content.endText(); Rectangle linkLocation = new Rectangle(95, 495 + baseFont.getDescentPoint(text, fontSize), 105 + baseFont.getWidthPoint(text, fontSize), 505 + baseFont.getAscentPoint(text, fontSize)); PdfAnnotation linkGreen = PdfAnnotation.createLink(writer, linkLocation, PdfName.HIGHLIGHT, "green" ); PdfTemplate appearance = PdfTemplate.createTemplate(writer, linkLocation.getWidth(), linkLocation.getHeight()); PdfGState state = new PdfGState(); //state.FillOpacity = .3f; // IMPROVEMENT: Use blend mode Darken instead of transparency; you may also want to try Multiply. state.setBlendMode(new PdfName("Darken")); appearance.setGState(state); appearance.setColorFill(BaseColor.GREEN); appearance.rectangle(0, 0, linkLocation.getWidth(), linkLocation.getHeight()); appearance.fill(); linkGreen.setAppearance(PdfName.N, appearance); writer.addAnnotation(linkGreen); doc.open(); doc.close(); }
@Test public void testComplex() throws FileNotFoundException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "transparencyComplex.pdf"))); writer.setCompressionLevel(0); document.open(); PdfContentByte content = writer.getDirectContent(); content.setRGBColorStroke(0, 255, 0); for (int y = 0; y <= 400; y+= 10) { content.moveTo(0, y); content.lineTo(500, y); } for (int x = 0; x <= 500; x+= 10) { content.moveTo(x, 0); content.lineTo(x, 400); } content.stroke(); PdfTemplate template = content.createTemplate(500, 400); PdfTransparencyGroup group = new PdfTransparencyGroup(); group.put(PdfName.CS, PdfName.DEVICEGRAY); group.setIsolated(false); group.setKnockout(false); template.setGroup(group); PdfShading radial = PdfShading.simpleRadial(writer, 262, 186, 10, 262, 186, 190, BaseColor.WHITE, BaseColor.BLACK, true, true); template.paintShading(radial); PdfDictionary mask = new PdfDictionary(); mask.put(PdfName.TYPE, PdfName.MASK); mask.put(PdfName.S, new PdfName("Luminosity")); mask.put(new PdfName("G"), template.getIndirectReference()); content.saveState(); PdfGState state = new PdfGState(); state.put(PdfName.SMASK, mask); content.setGState(state); content.setRGBColorFill(255, 0, 0); content.moveTo(162, 86); content.lineTo(162, 286); content.lineTo(362, 286); content.lineTo(362, 86); content.closePath(); //content.fillStroke(); content.fill(); content.restoreState(); document.close(); }
private void createPDFFile(String filename) throws FileNotFoundException, DocumentException { PNEditorComponent editor = getEditor(); PNGraph pnGraph = editor.getGraphComponent().getGraph(); JFrame frm = new JFrame(); PNGraphComponent forPrint = new PNGraphComponent(pnGraph) { }; mxRectangle size = forPrint.getGraph().getGraphBounds(); double space = 4; float x = (float) (size.getRectangle().getWidth() + size.getRectangle().getX() + space); float y = (float) (size.getRectangle().getHeight() + size.getRectangle().getY() + space); Document document = new Document(new Rectangle(x, y)); PdfWriter writer = null; writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // set crop of pdf doc = ll=lowerleft; ur=upper right float llx = (float) size.getX(); float lly = 0; float urx = x; float ury = (float) ((float) size.getRectangle().getHeight() + space*4); com.itextpdf.text.Rectangle crop = new com.itextpdf.text.Rectangle(llx, lly, urx, ury); writer.setCropBoxSize(crop); document.open(); PdfContentByte canvas = writer.getDirectContent(); // make pdf-background transparent PdfGState gState = new PdfGState(); gState.setFillOpacity(0.0f); canvas.setGState(gState); forPrint.setGridVisible(false); PdfGraphics2D g2 = new PdfGraphics2D(canvas, x, y); frm.getContentPane().add(forPrint); frm.pack(); forPrint.paint(g2); g2.dispose(); document.close(); }
@Override protected void doFancyStuff(ActionEvent e) throws Exception { setUpGui(); fch.addChoosableFileFilter(new FileFilter() { public String getDescription() { return "Portable Network Graphics (*.png)"; } public boolean accept(File f) { if (f.isDirectory()) { return true; } else { return f.getName().toLowerCase().endsWith(".png"); } } }); fch.setDialogTitle("Save PNG"); int returnVal = fch.showDialog(getEditor().getGraphComponent(), "save PNG"); if (returnVal == JFileChooser.APPROVE_OPTION) { String filename = fch.getSelectedFile().getAbsolutePath(); if (!filename.toLowerCase().endsWith(".png")) filename += ".png"; PNEditorComponent editor = getEditor(); PNGraph pnGraph = editor.getGraphComponent().getGraph(); JFrame frm = new JFrame(); PNGraphComponent forPrint = new PNGraphComponent(pnGraph) { }; mxRectangle size = forPrint.getGraph().getGraphBounds(); double space = 4; float x = (float) (size.getRectangle().getWidth() + size.getRectangle().getX() + space); float y = (float) (size.getRectangle().getHeight() + size.getRectangle().getY() + space); Document document = new Document(new Rectangle(x, y)); PdfWriter writer = null; writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // set crop of pdf doc = ll=lowerleft; ur=upper right float llx = (float) size.getX(); float lly = 0; float urx = x; float ury = (float) ((float) size.getRectangle().getHeight() + space); com.itextpdf.text.Rectangle crop = new com.itextpdf.text.Rectangle(llx, lly, urx, ury); writer.setCropBoxSize(crop); document.open(); PdfContentByte canvas = writer.getDirectContent(); // make pdf-background transparent PdfGState gState = new PdfGState(); gState.setFillOpacity(0.0f); canvas.setGState(gState); forPrint.setGridVisible(false); // PdfGraphics2D g2 = new PdfGraphics2D(canvas, x, y); BufferedImage b = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB); /* change sizes of course */ Graphics2D g2 = b.createGraphics(); // component.print(g); frm.getContentPane().add(forPrint); frm.pack(); forPrint.paint(g2); ImageIO.write(b,"png",new File(filename)); g2.dispose(); document.close(); } }