protected void embedFlash( String flashPath, byte[] flashData, float x, float y, float height, float width, String helpText, Map params ) throws IOException { y = transformY( y, height ); contentByte.saveState( ); PdfFileSpecification fs = PdfFileSpecification.fileEmbedded( writer, flashPath, helpText, flashData ); PdfAnnotation annot = PdfAnnotation.createScreen( writer, new Rectangle( x, y, x + width, y + height ), helpText, fs, "application/x-shockwave-flash", true ); writer.addAnnotation( annot ); if ( helpText != null ) { showHelpText( x, y, width, height, helpText ); } contentByte.restoreState( ); }
public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException { switch(annot.annotationType()) { case Annotation.URL_NET: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL))); case Annotation.URL_AS_STRING: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE))); case Annotation.FILE_DEST: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION))); case Annotation.SCREEN: boolean sparams[] = (boolean[])annot.attributes().get(Annotation.PARAMETERS); String fname = (String) annot.attributes().get(Annotation.FILE); String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE); PdfFileSpecification fs; if (sparams[0]) fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null); else fs = PdfFileSpecification.fileExtern(writer, fname); PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), fname, fs, mimetype, sparams[1]); return ann; case Annotation.FILE_PAGE: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), ((Integer) annot.attributes().get(Annotation.PAGE)).intValue())); case Annotation.NAMED_DEST: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer) annot.attributes().get(Annotation.NAMED)).intValue())); case Annotation.LAUNCH: return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION),(String) annot.attributes().get(Annotation.PARAMETERS),(String) annot.attributes().get(Annotation.OPERATION),(String) annot.attributes().get(Annotation.DEFAULTDIR))); default: return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE)); } }
/** * Creates a document with some PdfAnnotations. * */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Annotations.pdf")); // step 3: writer.setPdfVersion(PdfWriter.VERSION_1_5); document.open(); // step 4: PdfContentByte cb = writer.getDirectContent(); // page 1 PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, PdfTestBase.RESOURCES_DIR + "cards.mpg"); writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs, "video/mpeg", true)); PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript( "app.alert('Hello');\r", writer)); document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top")); writer.addAnnotation(a); writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", "some text".getBytes(), null, "some.txt")); writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help")); writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment")); cb.rectangle(200, 700, 100, 100); cb.rectangle(200, 550, 100, 100); cb.rectangle(200, 400, 100, 100); cb.rectangle(200, 250, 100, 100); cb.stroke(); document.newPage(); // page 2 writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer))); writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top")); writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT))); writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f), "This Comment annotation was made with 'createSquareCircle'", false)); document.newPage(); // page 3 PdfContentByte pcb = new PdfContentByte(writer); pcb.setColorFill(new Color(0xFF, 0x00, 0x00)); writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f), "This is some free text, blah blah blah", pcb)); writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line", 200, 550, 300, 650)); writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f), "This is a stamp", "Stamp")); writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f), "Hello, I'm a popup!", true)); cb.rectangle(200, 700, 100, 100); cb.rectangle(200, 550, 100, 100); cb.rectangle(200, 250, 100, 100); cb.stroke(); // step 5: we close the document document.close(); }
public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) { try { PdfWriter writer = exporterContext.getPdfWriter(); PdfIndirectObject swfRef; boolean newContext = !existingContexts.containsKey(exporterContext); if (newContext) { // add the Adobe 1.7 extensions catalog dictionary 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); // add the swf file byte[] swfData = readSwf(); 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 = (String) element.getParameterValue(PARAMETER_CHART_DATA); 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); } }
private File addAttachments(File basePDF, List<PDFManagerResponse> managerResponses, FlowInstanceManager instanceManager, FlowInstanceEvent event) throws IOException, DocumentException { File pdfWithAttachments = File.createTempFile("pdf-with-attachments", instanceManager.getFlowInstanceID() + "-" + getFileSuffix(event) + ".pdf", getTempDir()); OutputStream outputStream = null; InputStream inputStream = null; try{ outputStream = new FileOutputStream(pdfWithAttachments); inputStream = new FileInputStream(basePDF); PdfReader reader = new PdfReader(inputStream); PdfStamper stamper = new PdfStamper(reader, outputStream); PdfWriter writer = stamper.getWriter(); for(PDFManagerResponse managerResponse : managerResponses){ for(PDFQueryResponse queryResponse : managerResponse.getQueryResponses()){ if(queryResponse.getAttachments() != null){ for(PDFAttachment attachment : queryResponse.getAttachments()){ try { PdfFileSpecification fs = StreamPdfFileSpecification.fileEmbedded(writer, attachment.getInputStream(), attachment.getName()); writer.addFileAttachment(attachment.getDescription(), fs); } catch (Exception e) { log.error("Error appending attachment " + attachment.getName() + " from query " + queryResponse.getQueryDescriptor(), e); } } } } } stamper.close(); }finally{ StreamUtils.closeStream(inputStream); StreamUtils.closeStream(outputStream); } return pdfWithAttachments; }
protected static void addAttachment(PdfWriter writer, File file, String description) throws IOException { PdfFileSpecification fs = StreamPdfFileSpecification.fileEmbedded(writer, new FileInputStream(file), file.getName()); writer.addFileAttachment(description, fs); }
/** * Creates a document with some PdfAnnotations. * * @param args * no arguments needed */ public static void main(String[] args) { System.out.println("PdfAnnotations"); // step 1: creation of a document-object Document document = new Document(); try { // step 2: PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "Annotations.pdf")); // step 3: writer.setPdfVersion(PdfWriter.VERSION_1_5); document.open(); // step 4: PdfContentByte cb = writer.getDirectContent(); // page 1 PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, "cards.mpg"); writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs, "video/mpeg", true)); PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript( "app.alert('Hello');\r", writer)); document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top")); writer.addAnnotation(a); writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", "some text".getBytes(), null, "some.txt")); writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help")); writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment")); cb.rectangle(200, 700, 100, 100); cb.rectangle(200, 550, 100, 100); cb.rectangle(200, 400, 100, 100); cb.rectangle(200, 250, 100, 100); cb.stroke(); document.newPage(); // page 2 writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer))); writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top")); writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT))); writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f), "This Comment annotation was made with 'createSquareCircle'", false)); document.newPage(); // page 3 PdfContentByte pcb = new PdfContentByte(writer); pcb.setColorFill(new Color(0xFF, 0x00, 0x00)); writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f), "This is some free text, blah blah blah", pcb)); writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line", 200, 550, 300, 650)); writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f), "This is a stamp", "Stamp")); writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f), "Hello, I'm a popup!", true)); cb.rectangle(200, 700, 100, 100); cb.rectangle(200, 550, 100, 100); cb.rectangle(200, 250, 100, 100); cb.stroke(); } catch (Exception de) { de.printStackTrace(); } // step 5: we close the document document.close(); }