[英]How to add an SVG to a PDF using iText7
I need to add an SVG graphic into PDF file. 我需要将SVG图形添加到PDF文件中。
Is it that possible using iText7? 使用iText7可以吗?
Using iText5: 使用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);
I found out that in the following page: PdfPTable and PdfTemplate 我在以下页面中发现了这一点: PdfPTable和PdfTemplate
there is a way to create something similar as Template: 有一种创建类似于Template的方法:
PdfFormXObject svgTempl = new PdfFormXObject(new Rectangle(svgWidth, svgHeight));
How can I create Graphics2D? 如何创建Graphics2D?
Coincidentally, we're releasing our SVG implementation today. 巧合的是,我们今天发布了SVG实现。 We don't support the full feature set just yet, we're still working on that in Q2 and beyond, but you can use it already. 我们目前尚不支持全部功能集,我们仍将在第二季度及以后的时间里进行开发,但是您已经可以使用它了。 The artifact is on Maven . 该工件在Maven上 。 The repository is on Github . 该存储库位于Github上 。 And the documentation is on our public wiki . 该文档位于我们的公共Wiki上 。
Code samples will be put on the web site, but it's a very simple API, similar to how pdfHtml works. 代码示例将放在网站上,但这是一个非常简单的API,类似于pdfHtml的工作方式。 There's an SvgConverter utility class that offers multiple ways to convert to PDF or PDF XObjects. 有一个SvgConverter实用程序类,提供了多种转换为PDF或PDF XObjects的方法。
PdfDocument doc = new PdfDocument(
new PdfWriter(pdfOutputStream,
new WriterProperties().setCompressionLevel(0)));
doc.addNewPage();
SvgConverter.drawOnDocument(svg, doc, 1);
doc.close();
Source: I'm an iText developer working on the SVG implementation 资料来源:我是从事SVG实施的iText开发人员
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.