【发布时间】:2018-10-08 03:00:33
【问题描述】:
我需要将 SVG 图形添加到 PDF 文件中。
使用 iText7 可以吗?
使用 iText5:
BufferedReader in = new BufferedReader(new InputStreamReader(svgUrl.openStream()));
String xmlParser = XMLResourceDescriptor.getXMLParserClassName();
SVGDocument svgDoc = new SAXSVGDocumentFactory(xmlParser).createSVGDocument(null, in);
in.close();
// Try to read embedded height and width
float svgWidth = Float.parseFloat(svgDoc.getDocumentElement().getAttribute("width").replaceAll("[^0-9.,]",""));
float svgHeight = Float.parseFloat(svgDoc.getDocumentElement().getAttribute("height").replaceAll("[^0-9.,]",""));
PdfTemplate svgTempl = PdfTemplate.createTemplate(writer, svgWidth, svgHeight);
Graphics2D g2d = svgTempl.createGraphics(svgWidth,svgHeight);
GraphicsNode chartGfx = (new GVTBuilder()).build(new BridgeContext(new UserAgentAdapter()), svgDoc);
chartGfx.paint(g2d);
g2d.dispose();
Image img = new ImgTemplate(svgTempl);
我在以下页面中发现了这一点: PdfPTable and PdfTemplate
有一种方法可以创建类似于模板的东西:
PdfFormXObject svgTempl = new PdfFormXObject(new Rectangle(svgWidth, svgHeight));
如何创建 Graphics2D?
【问题讨论】:
-
任何研究???
-
我当然做了研究。我知道在 iText5 中是受支持的。但是由于我们现在使用 iText7,所以我没有找到可行的方法。
-
stackoverflow.com/questions/47976251/… 还不够?...哈哈
-
你很幸运。我们今天发布了初始 SVG 支持。我们正在将工件推送到 Maven/Nuget。 itextpdf.com/blog/release-notes-itext-712 请注意,还不支持完整的功能集,我们正在努力在第二季度及以后对其进行改进。
-
我们只是在最新版本中添加新功能,即 7.1.x 分支。现在我们正在发布 7.1.2。