@Override protected void configurePDFWriter(PdfWriter writer) throws IOException { final boolean initialPrecisionFlat = ByteBuffer.HIGH_PRECISION; try { ByteBuffer.HIGH_PRECISION = true; PdfDictionary dicMeasure = new PdfDictionary(new PdfName("Measure")); dicMeasure.put(PdfName.SUBTYPE, new PdfName("GEO")); PdfArray bounds = new PdfArray(); // lower left, upper left, upper right, lower right bounds.add(new float[]{0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f}); // geographic coordinates of corner points as lat/lon pairs PdfArray gpts = new PdfArray(lonLatCornerPoints); // lower left, upper left, upper right, lower right PdfArray lpts = new PdfArray(new float[]{0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f}); dicMeasure.put(new PdfName("Bounds"), bounds); dicMeasure.put(new PdfName("LPTS"), lpts); dicMeasure.put(new PdfName("GPTS"), gpts); PdfDictionary dicGCS = new PdfDictionary(new PdfName("PROJCS")); dicGCS.put(new PdfName("WKT"), new PdfString(wkt)); PdfIndirectObject indObjGCS = writer.addToBody(dicGCS); PdfIndirectReference indRefGCS = indObjGCS.getIndirectReference(); dicMeasure.put(new PdfName("GCS"), indRefGCS); PdfDictionary viewport = new PdfDictionary(new PdfName("Viewport")); viewport.put(new PdfName("Name"), new PdfString("Scree")); float left = (float) xToPagePx(pageFormat.getPageLeft()); float lower = (float) yToPagePx(pageFormat.getPageBottom()); float right = (float) xToPagePx(pageFormat.getPageRight()); float upper = (float) yToPagePx(pageFormat.getPageTop()); viewport.put(new PdfName("BBox"), new PdfRectangle(left, lower, right, upper)); PdfIndirectObject indObjMeasure = writer.addToBody(dicMeasure); PdfIndirectReference indRefMeasure = indObjMeasure.getIndirectReference(); viewport.put(new PdfName("Measure"), indRefMeasure); writer.setPageViewport(new PdfArray(viewport)); } finally { ByteBuffer.HIGH_PRECISION = initialPrecisionFlat; } }
@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); } }