简体   繁体   English

如何使用iText7将SVG添加到PDF

[英]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.

相关问题 使用iText7在现有PDF的特定页面中添加表格 - Using iText7 to add a table in a specific page of an existing PDF 如何使用 itext7 Java 将多个图像添加到 PDF? - How do you add multiple images to a PDF with itext7 Java? 如何使用itext7知道文档是否声称为PDF / A - How to know if a document claims to be in PDF/A using itext7 使用iText7验证/添加PDF签名 - Verify/Add PDF Signatures with iText7 如何将签名表单添加到现有的pdf(使用iText7),以便输出文件可以用作pdf(顺序签名)的输入? - How to add signature form to an existing pdf (using iText7), so that the output file can be served as a input to pdf (sequential signature)? 使用iText7和Java生成PDF - PDF Generation using iText7 with Java 如何在iText7中包含PDF / A中的BarCode? - How to include BarCode in PDF/A with iText7? 使用iText7(Java)将表添加到现有PDF并在其他页面上继续 - Using iText7 (Java) to add a table to an existing PDF and continue on additional pages IText7>如何将PdfCanvas的内容添加为PdfName.Figure for PDF / UA Accessibility - IText7> How to add a content of PdfCanvas as PdfName.Figure for PDF/UA Accessibility 如何在 BufferReader 读取的文本中添加换行符以尝试使用 iText7 将其转换为 pdf - how to add line break in a text read by BufferReader in attempt to convert it to pdf with iText7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM