private int numberOfVisibleSignatureFieldsOnSignaturePage(AcroFields readerFields, ArrayList<String> signatureFieldsNames, int signaturePage) { int count = 0; for (String signatureFieldName : signatureFieldsNames) { Item i = readerFields.getFieldItem(signatureFieldName); int page = i.getPage(0); if(page == signaturePage){ PdfDictionary pdct = i.getMerged(0); PdfNumber flags = pdct.getAsNumber(PdfName.F); if ((flags.intValue() & PdfAnnotation.FLAGS_HIDDEN) == 0) { count = count + 1; } } } return count; }
PdfAnnotation createAnnotation(PdfWriter writer, Rectangle rect, String contents) throws DocumentException, IOException { PdfContentByte cb = writer.getDirectContent(); PdfAppearance cs = cb.createAppearance(rect.getWidth(), rect.getHeight()); cs.rectangle(0 , 0, rect.getWidth(), rect.getHeight()); cs.fill(); cs.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12); cs.beginText(); cs.setLeading(12 + 1.75f); cs.moveText(.75f, rect.getHeight() - 12 + .75f); cs.showText(contents); cs.endText(); return PdfAnnotation.createFreeText(writer, rect, contents, cs); }
/** * <a href="https://stackoverflow.com/questions/46204693/cant-get-itext-rectangle-to-work-correctly-with-annotations"> * Can't get itext Rectangle to work correctly with annotations * </a> * <p> * This test looks at a <b>Text</b> annotation added via a {@link Chunk} * as done by the OP. As this way of adding annotations resets the * annotation <b>Rect</b> to the bounding box of the rendered {@link Chunk}, * it is not really what the OP wants. * </p> */ @Test public void testAnnotationIconForTYD() throws FileNotFoundException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "annotationIcons.pdf"))); document.open(); // Not "new Rectangle(164, 190, 164, 110)" which would be empty Rectangle rect = new Rectangle(164, 190, 328, 300); // Annotation added like the OP does Chunk chunk_text = new Chunk("Let's test a Text annotation..."); chunk_text.setAnnotation(PdfAnnotation.createText(writer, rect, "Warning", "This is a Text annotation with Comment icon.", false, "Comment")); document.add(chunk_text); // Annotation added to the document without Chunk writer.addAnnotation(PdfAnnotation.createText(writer, rect, "Warning 2", "This is another Text annotation with Comment icon.", false, "Comment")); document.close(); }
@Test public void testOPCode() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-annotate.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, outputStream); Rectangle linkLocation = new Rectangle( 100, 700, 100 + 200, 700 + 25 ); PdfName highlight = PdfAnnotation.HIGHLIGHT_INVERT; PdfAnnotation linkRed = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "red" ); PdfAnnotation linkGreen = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "green" ); BaseColor baseColorRed = new BaseColor(255,0,0); BaseColor baseColorGreen = new BaseColor(0,255,0); linkRed.setColor(baseColorRed); linkGreen.setColor(baseColorGreen); double angleDegrees = 10; double angleRadians = Math.PI*angleDegrees/180; stamper.addAnnotation(linkRed, 1); linkGreen.applyCTM(AffineTransform.getRotateInstance(angleRadians)); stamper.addAnnotation(linkGreen, 1); stamper.close(); } }
/** * <a href="http://stackoverflow.com/questions/43205385/trying-to-draw-an-ellipse-annotation-and-the-border-on-the-edges-goes-thin-and-t"> * Trying to draw an ellipse annotation and the border on the edges goes thin and thik when i try to roatate pdf itext5 * </a> * <p> * This test creates an ellipse annotation without appearance on a page without rotation. Everything looks ok. * </p> * @see #testCreateEllipseAppearance() * @see #testCreateEllipseOnRotated() * @see #testCreateEllipseAppearanceOnRotated() * @see #testCreateCorrectEllipseAppearanceOnRotated() */ @Test public void testCreateEllipse() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-ellipse.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, outputStream); Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150); PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false); annotation.setFlags(PdfAnnotation.FLAGS_PRINT); annotation.setColor(BaseColor.RED); annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID)); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close(); } }
/** * <a href="http://stackoverflow.com/questions/43205385/trying-to-draw-an-ellipse-annotation-and-the-border-on-the-edges-goes-thin-and-t"> * Trying to draw an ellipse annotation and the border on the edges goes thin and thik when i try to roatate pdf itext5 * </a> * <p> * This test creates an ellipse annotation without appearance on a page with rotation. * The ellipse form looks ok but it is moved to the right of the actual appearance rectangle when viewed in Adobe Reader. * This is caused by iText creating a non-standard rectangle, the lower left not being the lower left etc. * </p> * @see #testCreateEllipse() * @see #testCreateEllipseAppearance() * @see #testCreateEllipseAppearanceOnRotated() * @see #testCreateCorrectEllipseAppearanceOnRotated() */ @Test public void testCreateEllipseOnRotated() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-rotated-ellipse.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); reader.getPageN(1).put(PdfName.ROTATE, new PdfNumber(90)); PdfStamper stamper = new PdfStamper(reader, outputStream); Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150); PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false); annotation.setFlags(PdfAnnotation.FLAGS_PRINT); annotation.setColor(BaseColor.RED); annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID)); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close(); } }
/** * <p> * A primitive attempt at copying links from page <code>sourcePage</code> * of <code>PdfReader reader</code> to page <code>targetPage</code> of * <code>PdfStamper stamper</code>. * </p> * <p> * This method is meant only for the use case at hand, i.e. copying a link * to an external URI without expecting any advanced features. * </p> */ void copyLinks(PdfStamper stamper, int targetPage, PdfReader reader, int sourcePage) { PdfDictionary sourcePageDict = reader.getPageNRelease(sourcePage); PdfArray annotations = sourcePageDict.getAsArray(PdfName.ANNOTS); if (annotations != null && annotations.size() > 0) { for (PdfObject annotationObject : annotations) { annotationObject = PdfReader.getPdfObject(annotationObject); if (!annotationObject.isDictionary()) continue; PdfDictionary annotation = (PdfDictionary) annotationObject; if (!PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) continue; PdfArray rectArray = annotation.getAsArray(PdfName.RECT); if (rectArray == null || rectArray.size() < 4) continue; Rectangle rectangle = PdfReader.getNormalizedRectangle(rectArray); PdfName hightLight = annotation.getAsName(PdfName.H); if (hightLight == null) hightLight = PdfAnnotation.HIGHLIGHT_INVERT; PdfDictionary actionDict = annotation.getAsDict(PdfName.A); if (actionDict == null || !PdfName.URI.equals(actionDict.getAsName(PdfName.S))) continue; PdfString urlPdfString = actionDict.getAsString(PdfName.URI); if (urlPdfString == null) continue; PdfAction action = new PdfAction(urlPdfString.toString()); PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(), rectangle, hightLight, action); stamper.addAnnotation(link, targetPage); } } }
/** * <a href="http://stackoverflow.com/questions/28943245/possible-to-ues-variables-in-a-pdf-doument"> * Possible to ues variables in a PDF doument? * </a> * <p> * Generates a sample PDF containing two widgets of the same text field. * </p> */ @Test public void testCreateFormWithSameFieldTwice() throws IOException, DocumentException { try ( OutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "aFieldTwice.pdf")) ) { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, os); document.open(); document.add(new Paragraph("The same field twice")); PdfFormField field = PdfFormField.createTextField(writer, false, false, 0); field.setFieldName("fieldName"); PdfFormField annot1 = PdfFormField.createEmpty(writer); annot1.setWidget(new Rectangle(30, 700, 200, 720), PdfAnnotation.HIGHLIGHT_INVERT); field.addKid(annot1); PdfFormField annot2 = PdfFormField.createEmpty(writer); annot2.setWidget(new Rectangle(230, 700, 400, 720), PdfAnnotation.HIGHLIGHT_INVERT); field.addKid(annot2); writer.addAnnotation(field); document.close(); } }
/** * <a href="http://stackoverflow.com/questions/37275267/how-to-make-pdf-annotation-as-read-only-using-itext"> * how to make pdf annotation as read only using itext? * </a> * <br/> * test-annotated.pdf <i>simple PDF with sticky note</i> * * <p> * This test shows how to set the read-only flags of all annotations of a document. * </p> */ @Test public void testMarkAnnotationsReadOnly() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("test-annotated.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "test-annotated-ro.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, outputStream); for (int page = 1; page <= reader.getNumberOfPages(); page++) { PdfDictionary pageDictionary = reader.getPageN(page); PdfArray annotationArray = pageDictionary.getAsArray(PdfName.ANNOTS); if (annotationArray == null) continue; for (PdfObject object : annotationArray) { PdfObject directObject = PdfReader.getPdfObject(object); if (directObject instanceof PdfDictionary) { PdfDictionary annotationDictionary = (PdfDictionary) directObject; PdfNumber flagsNumber = annotationDictionary.getAsNumber(PdfName.F); int flags = flagsNumber != null ? flagsNumber.intValue() : 0; flags |= PdfAnnotation.FLAGS_READONLY; annotationDictionary.put(PdfName.F, new PdfNumber(flags)); } } } stamper.close(); } }
@Test public void testOwnAppearances() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-annotate-app.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, outputStream); BaseColor baseColorRed = new BaseColor(255,0,0); BaseColor baseColorGreen = new BaseColor(0,255,0); Rectangle linkLocation = new Rectangle( 100, 700, 100 + 200, 700 + 25 ); PdfName highlight = PdfAnnotation.HIGHLIGHT_INVERT; PdfAnnotation linkGreen = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "green" ); PdfTemplate appearance = PdfTemplate.createTemplate(stamper.getWriter(), linkLocation.getWidth(), linkLocation.getHeight()); appearance.setColorFill(baseColorGreen); appearance.rectangle(0, 0, linkLocation.getWidth(), linkLocation.getHeight()); appearance.fill(); double angleDegrees = 35; double angleRadians = Math.PI*angleDegrees/180; AffineTransform at = AffineTransform.getRotateInstance(angleRadians); appearance.setMatrix((float)at.getScaleX(), (float)at.getShearY(),(float) at.getShearX(), (float)at.getScaleY(),(float) at.getTranslateX(), (float)at.getTranslateY()); linkGreen.setAppearance(PdfName.N, appearance); linkGreen.setColor(baseColorGreen); stamper.addAnnotation(linkGreen, 1); PdfAnnotation linkRed = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "red" ); linkRed.setColor(baseColorRed); stamper.addAnnotation(linkRed, 1); stamper.close(); } }
/** * <a href="http://stackoverflow.com/questions/43205385/trying-to-draw-an-ellipse-annotation-and-the-border-on-the-edges-goes-thin-and-t"> * Trying to draw an ellipse annotation and the border on the edges goes thin and thik when i try to roatate pdf itext5 * </a> * <p> * This test creates an ellipse annotation with appearance on a page without rotation. Everything looks ok. * </p> * @see #testCreateEllipse() * @see #testCreateEllipseOnRotated() * @see #testCreateEllipseAppearanceOnRotated() * @see #testCreateCorrectEllipseAppearanceOnRotated() */ @Test public void testCreateEllipseAppearance() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-ellipse-appearance.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, outputStream); Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150); PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false); annotation.setFlags(PdfAnnotation.FLAGS_PRINT); annotation.setColor(BaseColor.RED); annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID)); PdfContentByte cb = stamper.getOverContent(1); PdfAppearance app = cb.createAppearance(rect.getWidth(), rect.getHeight()); app.setColorStroke(BaseColor.RED); app.setLineWidth(3.5); app.ellipse( 1.5, 1.5, rect.getWidth() - 1.5, rect.getHeight() - 1.5); app.stroke(); annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close(); } }
/** * <a href="http://stackoverflow.com/questions/43205385/trying-to-draw-an-ellipse-annotation-and-the-border-on-the-edges-goes-thin-and-t"> * Trying to draw an ellipse annotation and the border on the edges goes thin and thik when i try to roatate pdf itext5 * </a> * <p> * This test creates an ellipse annotation with appearance on a page with rotation. * The ellipse position looks ok but it is deformed. * This is caused by iText rotating the annotation rectangle but not (how could it?) the appearance rectangle. * </p> * @see #testCreateEllipse() * @see #testCreateEllipseAppearance() * @see #testCreateEllipseOnRotated() * @see #testCreateCorrectEllipseAppearanceOnRotated() */ @Test public void testCreateEllipseAppearanceOnRotated() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-rotated-ellipse-appearance.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); reader.getPageN(1).put(PdfName.ROTATE, new PdfNumber(90)); PdfStamper stamper = new PdfStamper(reader, outputStream); Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150); PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false); annotation.setFlags(PdfAnnotation.FLAGS_PRINT); annotation.setColor(BaseColor.RED); annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID)); PdfContentByte cb = stamper.getOverContent(1); PdfAppearance app = cb.createAppearance(rect.getWidth(), rect.getHeight()); app.setColorStroke(BaseColor.RED); app.setLineWidth(3.5); app.ellipse( 1.5, 1.5, rect.getWidth() - 1.5, rect.getHeight() - 1.5); app.stroke(); annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close(); } }
/** * <a href="http://stackoverflow.com/questions/43205385/trying-to-draw-an-ellipse-annotation-and-the-border-on-the-edges-goes-thin-and-t"> * Trying to draw an ellipse annotation and the border on the edges goes thin and thik when i try to roatate pdf itext5 * </a> * <p> * This test creates an ellipse annotation with appearance with switched dimensions on a page with rotation. * Everything looks ok. * </p> * @see #testCreateEllipse() * @see #testCreateEllipseAppearance() * @see #testCreateEllipseOnRotated() * @see #testCreateEllipseAppearanceOnRotated() */ @Test public void testCreateCorrectEllipseAppearanceOnRotated() throws IOException, DocumentException { try ( InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/merge/testA4.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "testA4-rotated-ellipse-appearance-correct.pdf")) ) { PdfReader reader = new PdfReader(resourceStream); reader.getPageN(1).put(PdfName.ROTATE, new PdfNumber(90)); PdfStamper stamper = new PdfStamper(reader, outputStream); Rectangle rect = new Rectangle(202 + 6f, 300, 200 + 100, 300 + 150); PdfAnnotation annotation = PdfAnnotation.createSquareCircle(stamper.getWriter(), rect, null, false); annotation.setFlags(PdfAnnotation.FLAGS_PRINT); annotation.setColor(BaseColor.RED); annotation.setBorderStyle(new PdfBorderDictionary(3.5f, PdfBorderDictionary.STYLE_SOLID)); PdfContentByte cb = stamper.getOverContent(1); PdfAppearance app = cb.createAppearance(rect.getHeight(), rect.getWidth()); app.setColorStroke(BaseColor.RED); app.setLineWidth(3.5); app.ellipse( 1.5, 1.5, rect.getHeight() - 1.5, rect.getWidth() - 1.5); app.stroke(); annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close(); } }
/** * <a href="http://stackoverflow.com/questions/32332490/new-signature-field-rotated-90-degrees"> * New Signature Field Rotated 90 Degrees * </a> * <br/> * <a href="https://drive.google.com/open?id=0B3pKBz-WDrXnM3hYeDFtXzhldnM"> * DA3161-Template.pdf * </a> * <p> * The page in your document is rotated using the Rotate page dictionary entry. * When creating a field to be filled-in by others later, you have to add a hint * to the field indicating a counter-rotation if you want the field content to * effectively appear upright. * </p> * <p> * You do this by setting the MKRotation attribute of the field. This creates a * rotation entry R with value 90 in the appearance characteristics dictionary * MK of the field. * </p> */ @Test public void testDA3161_Template() throws IOException, GeneralSecurityException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, DocumentException { try ( InputStream resource = getClass().getResourceAsStream("DA3161-Template.pdf"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "DA3161-Template_Field.pdf")) ) { System.out.println("DA3161-Template.pdf"); PdfReader reader = new PdfReader(resource); PdfStamper pdfStamper = new PdfStamper(reader, result); AcroFields fields = pdfStamper.getAcroFields(); int itemno = 3; //Get location to the field where we will place the signature field FieldPosition NewPosition = fields.getFieldPositions("DESC_0_" + (itemno + 1)).get(0); float l1 = NewPosition.position.getLeft(); float r1 = NewPosition.position.getRight(); float t1 = NewPosition.position.getTop(); float b1 = NewPosition.position.getBottom(); PdfFormField field = PdfFormField.createSignature(pdfStamper.getWriter()); field.setFieldName("G4_SignatureX"); // Set the widget properties field.setWidget(new Rectangle(r1, t1, l1, b1), PdfAnnotation.HIGHLIGHT_NONE); field.setFlags(PdfAnnotation.FLAGS_PRINT); // !!!!!!!!!!!!!!!!!!! field.setMKRotation(90); // Add the annotation pdfStamper.addAnnotation(field, 1); pdfStamper.close(); } }
/** * <a href="http://stackoverflow.com/questions/41949253/how-to-add-columntext-as-an-annotation-in-itext-pdf"> * How to add columnText as an annotation in itext pdf * </a> * <p> * This test demonstrates how to use a columntext in combination with an annotation. * </p> */ @Test public void testAddAnnotationLikeJasonY() throws IOException, DocumentException { String html ="<html><h1>Header</h1><p>A paragraph</p><p>Another Paragraph</p></html>"; String css = "h1 {color: red;}"; ElementList elementsList = XMLWorkerHelper.parseToElementList(html, css); try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "JasonY.pdf")) ) { PdfReader reader = new PdfReader(resource); PdfStamper stamper = new PdfStamper(reader, result); Rectangle cropBox = reader.getCropBox(1); PdfAnnotation annotation = stamper.getWriter().createAnnotation(cropBox, PdfName.FREETEXT); PdfAppearance appearance = PdfAppearance.createAppearance(stamper.getWriter(), cropBox.getWidth(), cropBox.getHeight()); ColumnText ct = new ColumnText(appearance); ct.setSimpleColumn(new Rectangle(cropBox.getWidth(), cropBox.getHeight())); elementsList.forEach(element -> ct.addElement(element)); ct.go(); annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close(); } }
/** * <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(); }
@Override public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) { try { PdfWriter writer = exporterContext.getPdfWriter(); PdfIndirectObject swfRef; boolean newContext = !existingContexts.containsKey(exporterContext); if (newContext) { PdfDictionary extensions = new PdfDictionary(); PdfDictionary adobeExtension = new PdfDictionary(); adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7); adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3)); extensions.put(new PdfName("ADBE"), adobeExtension); writer.getExtraCatalog().put(new PdfName("Extensions"), extensions); byte[] swfData = getChartSwf(); PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart", swfData); swfRef = writer.addToBody(swfFile); existingContexts.put(exporterContext, swfRef); } else { swfRef = (PdfIndirectObject) existingContexts.get(exporterContext); } Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY(), element.getX() + exporterContext.getOffsetX() + element.getWidth(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight()); PdfAnnotation ann = new PdfAnnotation(writer, rect); ann.put(PdfName.SUBTYPE, new PdfName("RichMedia")); PdfDictionary settings = new PdfDictionary(); PdfDictionary activation = new PdfDictionary(); activation.put(new PdfName("Condition"), new PdfName("PV")); settings.put(new PdfName("Activation"), activation); ann.put(new PdfName("RichMediaSettings"), settings); PdfDictionary content = new PdfDictionary(); HashMap<String, PdfIndirectReference> assets = new HashMap<String, PdfIndirectReference>(); assets.put("map.swf", swfRef.getIndirectReference()); PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer); content.put(new PdfName("Assets"), assetsDictionary); PdfArray configurations = new PdfArray(); PdfDictionary configuration = new PdfDictionary(); PdfArray instances = new PdfArray(); PdfDictionary instance = new PdfDictionary(); instance.put(new PdfName("Subtype"), new PdfName("Flash")); PdfDictionary params = new PdfDictionary(); String chartData = ((ChartGenerator) element.getParameterValue(ChartGenerator.PARAMETER_CHART_GENERATOR)).generateChart(); String vars = "inline_data=" + chartData; params.put(new PdfName("FlashVars"), new PdfString(vars)); instance.put(new PdfName("Params"), params); instance.put(new PdfName("Asset"), swfRef.getIndirectReference()); PdfIndirectObject instanceRef = writer.addToBody(instance); instances.add(instanceRef.getIndirectReference()); configuration.put(new PdfName("Instances"), instances); PdfIndirectObject configurationRef = writer.addToBody(configuration); configurations.add(configurationRef.getIndirectReference()); content.put(new PdfName("Configurations"), configurations); ann.put(new PdfName("RichMediaContent"), content); writer.addAnnotation(ann); } catch (Exception e) { throw new RuntimeException(e); } }