Class SvgConverter
Converts SVG (Scalable Vector Graphics) documents into PdfTemplate objects for embedding in PDF documents.
Inherited Members
Namespace: Syncfusion.Pdf
Assembly: Syncfusion.Pdf.NET.dll
Syntax
public class SvgConverter
Remarks
SvgConverter provides multi-format input support with automatic detection:
- File path: automatically loads and parses SVG files from the filesystem.
- SVG markup: detects and parses inline SVG XML strings.
- Stream: reads SVG content from any readable stream.
Font resolution follows a 3-tier chain:
- SVG-embedded fonts (@font-face or inline font data).
- System fonts (.NET Framework) or user-supplied fonts via SubstituteFont event (portable builds).
- Standard PDF base fonts.
For PDF/A conformance levels, embedded or system fonts are required and an exception is thrown if unavailable.
Examples
//Create new instance for SvgConverter
SvgConverter converter = new SvgConverter();
//Converts SVG to PdfTemplate
PdfTemplate svg = converter.Convert("Input.svg");
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Configure page settings to match SVG dimensions and remove margins
document.PageSettings.Margins.All = 0;
document.PageSettings.Size = new SizeF(svg.Width, svg.Height);
//Create a new page in the PDF document
PdfPage page = document.Pages.Add();
//Draw the SVG template onto the PDF page at position (0,0) with original dimensions
page.Graphics.DrawPdfTemplate(svg, new PointF(0, 0), new SizeF(svg.Width, svg.Height));
//Save the PDF document
pdfDocument.Save("Output.pdf");
//Closing the PDF document
pdfDocument.Close(true);
'Create new instance for SvgConverter
Dim converter As New SvgConverter()
'Converts SVG to PdfTemplate
Dim svg As PdfTemplate = converter.Convert("Input.svg")
Dim pdfDocument As PdfDocument = New PdfDocument()
'Configure page settings to match SVG dimensions and remove margins
pdfDocument.PageSettings.Margins.All = 0
pdfDocument.PageSettings.Size = New SizeF(svg.Width, svg.Height)
'Create a new page in the PDF document
Dim page As PdfPage = pdfDocument.Pages.Add()
'Draw the SVG template onto the PDF page at position (0,0) with original dimensions
page.Graphics.DrawPdfTemplate(svg, New PointF(0, 0), New SizeF(svg.Width, svg.Height))
'Save the PDF document
pdfDocument.Save("Output.pdf")
'Closing the PDF document
pdfDocument.Close(True)
Constructors
SvgConverter()
Initializes a new instance of the SvgConverter class.
Declaration
public SvgConverter()
Remarks
Creates a stateless converter instance ready for SVG-to-PDF conversions. Multiple conversions can be performed sequentially or in parallel using the same instance.
Each Convert call operates independently:
- A new SessionFontCache is created and discarded after conversion.
- Embedded fonts are extracted and freed after use.
- No state is retained between conversions.
Methods
Convert(Stream)
Converts SVG content from a stream into a PdfTemplate.
Declaration
public PdfTemplate Convert(Stream svgStream)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | svgStream | A readable stream containing valid SVG XML markup. The stream position is preserved; reading starts from the current position. |
Returns
| Type | Description |
|---|---|
| PdfTemplate | A PdfTemplate containing the rendered SVG with all shapes, text, and transformations preserved. |
Remarks
The stream is read from its current position but not closed by this method.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| InvalidOperationException | Stream could not be parsed as valid SVG XML. |
Convert(string)
Converts the specified SVG source into a PdfTemplate.
Declaration
public PdfTemplate Convert(string svgPath)
Parameters
| Type | Name | Description |
|---|---|---|
| string | svgPath | The SVG input, interpreted as either a file path or inline SVG markup string. If the path starts with '<', it is treated as SVG XML markup. Otherwise, it is treated as a file path. |
Returns
| Type | Description |
|---|---|
| PdfTemplate | A PdfTemplate containing the rendered SVG with all shapes, text, and transformations preserved. |
Remarks
Input detection:
- Starts with '<': Parsed as SVG markup.
- File exists: Loaded and parsed from filesystem.
- Otherwise: Attempted as markup with fallback error message.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| InvalidOperationException | Input is neither a valid SVG file path nor valid SVG markup. Check that the file exists and is readable, or that the string contains valid SVG XML. |
Events
SubstituteFont
The event raised when the SVG converter requires a font that is not embedded in the SVG document.
Declaration
public event SvgConverter.PdfFontEventHandler SubstituteFont
Event Type
| Type |
|---|
| SvgConverter.PdfFontEventHandler |
Examples
SvgConverter converter = new SvgConverter();
PdfTemplate svg = converter.Convert(filePath);
converter.SubstituteFont += Converter_SubstituteFont;
PdfDocument document = new PdfDocument();
document.PageSettings.Margins.All = 0;
document.PageSettings.Size = new SizeF(svg.Width, svg.Height);
PdfPage page = document.Pages.Add();
page.Graphics.DrawPdfTemplate(svg, new PointF(0, 0), new SizeF(svg.Width, svg.Height));
string output = Path.Combine("Output.pdf");
document.Save(output);
document.Close(true);
void Converter_SubstituteFont(object sender, PdfFontEventArgs args)
{
string fontName = args.FontName
SKTypeface typeface = SKTypeface.FromFamilyName(fontName, SkiaSharp.SKFontStyleWeight.Light, SkiaSharp.SKFontStyleWidth.Normal, SkiaSharp.SKFontStyleSlant.Upright);
SKStreamAsset typeFaceStream = typeface.OpenStream();
MemoryStream memoryStream = null;
typeFaceStream
typeFaceStream.Length
0
{
//Create fontData from type face stream.
byte[] fontData = new byte[typeFaceStream.Length - 1];
typeFaceStream.Read(fontData, typeFaceStream.Length);
typeFaceStream.Dispose();
//Create the new memory stream from font data.
memoryStream = new MemoryStream(fontData);
}
//set the font stream to the event args.
args.FontStream = memoryStream;
}