5

I need to add an SVG graphic into PDF file.

Is it that possible using iText7?

Using 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

there is a way to create something similar as Template:

PdfFormXObject svgTempl = new PdfFormXObject(new Rectangle(svgWidth, svgHeight));

How can I create Graphics2D?

Ap Tsi
  • 99
  • 1
  • 9
  • any research??? – L_Church Apr 27 '18 at 09:35
  • Of course I made research. I know that in iText5 is supported. But since we now use iText7, I didn't find something that works. – Ap Tsi Apr 27 '18 at 09:42
  • https://stackoverflow.com/questions/47976251/how-to-add-an-svg-to-a-pdf-using-itext7-without-rasterizing not enough?... lol – L_Church Apr 27 '18 at 09:48
  • 1
    You're in luck. We're releasing initial SVG support today. We're pushing the artifact to Maven/Nuget as we speak. https://itextpdf.com/blog/release-notes-itext-712 Do note that the full feature set isn't support yet, we're working on improving it in Q2 and beyond. – Michaël Demey Apr 27 '18 at 09:48
  • Thanks for the comment. Which itext7 should I add into dependencies. Right now I have itext7 7.0.2 – Ap Tsi Apr 27 '18 at 09:51
  • 1
    We're only adding new features to the recentmost version, meaning the 7.1.x branch. Right now we're releasing 7.1.2. – Michaël Demey Apr 27 '18 at 09:56

1 Answers1

7

Coincidentally, we're releasing our SVG implementation today. 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. The repository is on Github. And the documentation is on our public wiki.

Code samples will be put on the web site, but it's a very simple API, similar to how pdfHtml works. There's an SvgConverter utility class that offers multiple ways to convert to PDF or 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

Michaël Demey
  • 1,567
  • 10
  • 18
  • I tried and I receive the log: [main] ERROR com.itextpdf.styledxmlparser.node.impl.jsoup.JsoupXmlParser - Could not map node type: class com.itextpdf.styledxmlparser.jsoup.nodes.XmlDeclaration – Ap Tsi Apr 27 '18 at 11:30
  • Could you make a new question and attach your SVG to it? – Michaël Demey Apr 27 '18 at 12:08
  • 2
    @ApostolosTsigkaridas your svg file can be converted with the development version of svg. As said, this 7.1.2 release isn't complete and we're still working on the implementation. You can choose to build 7.1.3-SNAPSHOT from sources or to download it through our artifactory in the near future (still setting it up) https://repo.itextsupport.com/webapp/#/artifacts/browse/simple/General/snapshot/com/itextpdf – Michaël Demey Apr 27 '18 at 12:21
  • @MichaëlDemey I made a new question https://stackoverflow.com/questions/50062460/itext7-adding-svg-into-pdfdocument-and-possible-issues. – Ap Tsi Apr 27 '18 at 12:40
  • 1
    @MichaëlDemey where is exactly that 7.1.3-SNAPSHOT.jar? – Ap Tsi Apr 27 '18 at 12:59
  • Any updates on usage documentation for SVG files, possibly as background images? – G_V Nov 25 '19 at 09:52