Convert SVG to PDF in C#

To convert SVG to PDF in C#, load the SVG with SVGDocument, create PdfSaveOptions, and call Converter.ConvertSVG(). Use ConvertSVG() for straightforward file, stream, or document conversion. Use RenderTo() with PdfDevice when your application needs lower-level rendering control.

Converting SVG documents to other formats is one of the main features of Aspose.SVG for .NET API. Converting is required for various reasons: to work in a familiar, convenient format or to take advantage of different formats for specific tasks. PDF is a file format supported by all operating systems, used for presenting images, documents and books. Files in PDF can be easily viewed, printed, and shared online.

The article provides information on a list of supported SVG to PDF conversion scenarios and how to execute them. You can convert SVG to PDF and other popular formats in any way – online or programmatically. For a broader overview of conversion targets and APIs, see Convert SVG Files in C# and Supported File Formats.

SVG to PDF Conversion Methods

Aspose.SVG for .NET supports two practical SVG to PDF workflows. Choose the API based on how much control your application needs over rendering and output.

ScenarioRecommended APIWhy use it
Convert an SVG file, URL, in-memory string, or loaded SVGDocument to PDFConverter.ConvertSVG() with PdfSaveOptionsBest default choice for concise conversion code and typical server-side workflows. For stream input, load the stream into SVGDocument first.
Render an already configured SVGDocument to a PDF output deviceSVGDocument.RenderTo() with PdfDevice and PdfRenderingOptionsUseful when your rendering pipeline already works with devices or needs explicit rendering setup.
Set PDF background, page size, margins, resolution, document information, or encryptionPdfSaveOptions or PdfRenderingOptionsThese options control PDF output without changing the source SVG markup.
Convert uploaded SVG content in a web applicationSVGDocument(stream, baseUri) plus PdfDevice or Converter.ConvertSVG()Useful for ASP.NET Core upload workflows where the SVG is received as a stream and the PDF is written to a response stream, cloud storage, or an application-managed output path.

Online SVG Converter

You can check the Aspose.SVG API functionality and convert SVG in real-time. Please load SVG from the local file system, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.

                
            

If you want to convert SVG to PDF programmatically, please see the following C# code examples.

Convert SVG to PDF Using ConvertSVG() Method

The static methods of the Converter class can convert SVG to PDF with a single line of code. It is the easiest way for conversion. Converting an SVG file to another format using ConvertSVG() methods is a sequence of operations among which document loading and saving:

The following code snippet can be used to convert an SVG file to PDF format:

1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Converters;
4using System.Drawing;
5using Aspose.Svg.Saving;
 1// Convert an SVG file to PDF in C# using ConvertSVG()
 2
 3// Initialize an SVG document from a file
 4using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "lineto.svg")))
 5{
 6    // Initialize an instance of the PdfSaveOptions class
 7    PdfSaveOptions saveOptions = new PdfSaveOptions();
 8    saveOptions.BackgroundColor = System.Drawing.Color.Gray;
 9
10    // Convert SVG to PDF
11    Converter.ConvertSVG(document, saveOptions, Path.Combine(OutputDir, "lineto_out.pdf"));
12}

The PdfSaveOptions() constructor initializes an instance of the PdfSaveOptions class that is passed to ConvertSVG() method. The ConvertSVG() method takes the document, saveOptions, output file path and performs the conversion operation. In the example above, we add the BackgroundColor property that sets Color, which will fill the background of every page.

You can download the complete examples and data files from GitHub. You find out about downloading from GitHub and running examples from the How to Run the Examples section.

Save Options

You can convert SVG to PDF using default or custom save options. PdfSaveOptions or PdfRenderingOptions usage enables you to customize the rendering process; you may specify the page size, margins, background color, file permissions, Css, etc.

PropertyDescription
JpegQualitySpecifies the quality of JPEG compression for images. The default value is 95.
CssGets a CssOptions object which is used for configuration of CSS properties processing.
DocumentInfoThis property contains information about the output PDF document.
BackgroundColorThis property sets the color that will fill the background of every page. By default, this property is Transparent.
PageSetupThis property gets a page setup object and uses it for configuration output page-set.
HorizontalResolutionSets the horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolutionSets the vertical resolution for output images in pixels per inch. The default value is 300 dpi.
EncryptionThis property gets or sets encryption details. If it is not set, then no encryption will be performed.

Note: The options that are implementing with the PdfSaveOptions class are inheriting from the PdfRenderingOptions class.

Convert SVG to PDF Using RenderTo() Method

Consider SVG to PDF conversion scenario using RenderTo() method:

The following code snippet can be used to convert an SVG file to PDF format:

1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Drawing;
4using Aspose.Svg.Rendering;
5using Aspose.Svg.Rendering.Pdf;
 1// Render SVG to PDF in C# with custom page settings
 2
 3// Initialize an SVG document from a file
 4using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "light.svg")))
 5{
 6    // Initialize an instance of the PdfRenderingOptions class and set custom PageSetup and JpegQuality properties
 7    PdfRenderingOptions pdfOptions = new PdfRenderingOptions();
 8    pdfOptions.PageSetup.AnyPage = new Page(new Drawing.Size(500, 500), new Margin(10, 10, 10, 10));
 9    pdfOptions.JpegQuality = 10;
10
11    // Initialize an instance of the PdfDevice class
12    using (IDevice device = new PdfDevice(pdfOptions, Path.Combine(OutputDir, "light_out.pdf")))
13    {
14        // Render SVG to PDF, send the document to the rendering device
15        document.RenderTo(device);
16    }
17}

The PdfRenderingOptions() constructor initializes a new object of the PdfRenderingOptions class that is passed as an argument to the PdfDevice(options, file) constructor. The last initializes a new instance of the PdfDevice class by rendering options and output file name. The RenderTo(device) method converts SVG to PDF and sends the current document to the output rendering device.

The JpegQuality specifies the quality of JPEG compression for images. The default is 95. In the example above, the used JpegQuality value is 10. The figure illustrates conversion SVG to PDF for two files light.svg and lineto.svg: a) The JpegQuality value is default; b) The JpegQuality value is 10.

Images rendered to PDF with various JpegQuality values

Convert SVGZ to PDF in C#

SVGZ is a GZIP-compressed SVG file. Aspose.SVG for .NET can load SVGZ content with SVGDocument, so you do not need to manually decompress an .svgz file before rendering it to PDF. After loading the SVGZ document, use the same PdfSaveOptions and Converter.ConvertSVG() workflow as for ordinary SVG files:

1using Aspose.Svg;
2using Aspose.Svg.Converters;
3using Aspose.Svg.Saving;
4
5using (var document = new SVGDocument("input.svgz"))
6{
7    var options = new PdfSaveOptions();
8    Converter.ConvertSVG(document, options, "output.pdf");
9}

Use this workflow when compressed SVG assets need to become printable documents, reports, or PDF previews. If you need to edit or inspect the markup first, convert the SVGZ file back to an editable SVG document; see Convert SVGZ to SVG in C#.

Practical Recommendations

When you convert SVG to PDF in a production .NET application, keep the source SVG, linked resources, and output requirements under explicit control. SVG documents may reference fonts, CSS files, raster images, or remote resources; make these dependencies available to the rendering process or embed them in the SVG when consistent output is required.

Production taskRecommendation
Convert uploaded SVG filesLoad the SVG from a stream or controlled storage location, validate the input, and write the PDF to a stream or application-managed output path. See Create, Load and Read SVG Files in C# for stream loading patterns.
Keep text rendering consistent across Windows, Linux, and containersConfigure or embed the fonts used by the SVG. If a font is unavailable, text may fall back to another font and change layout. See Work with SVG Fonts and Text in C#.
Produce PDF thumbnails, previews, or reportsSet PageSetup, background color, and resolution explicitly so every output file has predictable dimensions. For size-related conversion options, see Resizing a Document During Conversion from SVG.
Process many SVG filesReuse application configuration, dispose SVGDocument instances, and handle conversion errors per file instead of stopping the entire batch.
Convert untrusted SVG contentTreat SVG as active XML-based content. Validate or sanitize uploaded SVG files and restrict external dependencies according to your application security policy. For resource and runtime settings, see Environment Configuration.

Common Mistakes and Fixes

ProblemCommon causeFix
PDF page is cropped or has unexpected sizeThe SVG lacks clear width, height, or viewBox, or the output page setup is not configuredDefine the source SVG size or set PageSetup in PdfSaveOptions or PdfRenderingOptions. See SVG viewBox and Resize Document.
Text looks different in the PDFRequired fonts are not available in the runtime environmentInstall, configure, or embed the required fonts. See Work with SVG Fonts and Text in C#.
Transparent areas render differently than expectedPDF viewers and downstream tools may display transparency against different backgroundsSet BackgroundColor when a fixed white, black, or brand-colored background is required. See Change SVG Background Color.
External images or styles are missingThe SVG references files or URLs that are not accessible during conversionUse valid resource paths, embed required assets, or configure resource access for the conversion environment. See Fix SVG Styling and Font Issues in C#.
Output file is larger than expectedSVG contains large embedded bitmaps or high-quality raster dataTune JpegQuality, optimize embedded images, or simplify the source SVG before conversion. See Optimizing SVG Files for Web and Performance.

FAQ

When should I use ConvertSVG() instead of RenderTo()?
Use ConvertSVG() for direct conversion tasks. Use RenderTo() when your code already manages rendering devices or needs explicit control through PdfDevice and PdfRenderingOptions.

Can I convert an SVG stream to a PDF stream?
Yes, but use the correct stream workflow. Load the input SVG stream with an SVGDocument constructor, for example SVGDocument(stream, baseUri). For PDF output, render to a stream with PdfDevice, for example new PdfDevice(options, outputStream), or use Converter.ConvertSVG() with an ICreateStreamProvider when your application needs provider-based output.

How can I control PDF page size?
Configure PageSetup in PdfSaveOptions or PdfRenderingOptions. You can also make the SVG dimensions predictable by defining width, height, and viewBox in the source SVG.

Why are fonts different after SVG to PDF conversion?
The conversion process must resolve the fonts used by the SVG. If the original font is unavailable, another font may be substituted. Configure custom fonts or use @font-face where appropriate.

Related Resources

You can convert SVG to PDF with our free online SVG to PDF Converter that works with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

SVG to PDF Converter