SVG to PDF conversion is available starting with XFINIUM.PDF 5.3.
A SVG file is loaded in a PdfSvgDrawing
object and then displayed on the PDF page using DrawFormXObject
method (PdfSvgDrawing
class inherits from PdfFormXObject
).
The well known tiger SVG image is converted to PDF like this:
PdfFixedDocument document = new PdfFixedDocument(); PdfPage page = document.Pages.Add(); PdfSvgDrawing svg = new PdfSvgDrawing("tiger.svg"); page.Graphics.DrawFormXObject(svg, 20, 20, page.Width - 40, page.Width - 40); document.Save("tiger.pdf");
Dim document As New PdfFixedDocument() Dim page As PdfPage = document.Pages.Add() Dim svg As New PdfSvgDrawing("tiger.svg") page.Graphics.DrawFormXObject(svg, 20, 20, page.Width - 40, page.Height - 40) document.Save("tiger.pdf")
After the SVG file is loaded in the PdfSvgDrawing
object you can initiate explicitly the conversion by calling the PdfSvgDrawing.RenderSvgDrawing
method. This method causes the parsing of the SVG file and now the SVG image size can be read from the PdfSvgDrawing.Width
and PdfSvgDrawing.Height
properties. The SVG drawing still needs to be drawn on the page using DrawFormXObject
method.
By default the conversion process uses Helvetica family of fonts for text. If you want to use another font for drawing text you can set the DefaultRegularFont
, DefaultBoldFont
, DefaultItalicFont
and DefaultBoldItalic
properties of the PdfSvgDrawing
class.
I’ve been trying to use this feature, but it seems is not working 100% ok,
for instance I’m trying to load an icon from google material website (https://material.io/tools/icons/)
and when it loads it is kind of distorted, also if we use SVG files with text, they are not displayed correclty…
am I doing something wrong?
var file = new FileStream(“../../../assets/icon.svg”, FileMode.Open, FileAccess.Read);
PdfSvgDrawing svg1 = new PdfSvgDrawing(file);
svg1.RenderSvgDrawing();
page.Graphics.DrawFormXObject(svg1, (20, 20, 400, 400);
Please send us the SVG files you tested and we’ll investigate this problem.