Convert SVG to PDF – C# Examples

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.

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 a 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;
 6...
 7
 8    // Initialize an SVG document from a file
 9    using (var document = new SVGDocument(Path.Combine(DataDir, "lineto.svg")))
10    {
11        // Initialize an instance of the PdfSaveOptions class
12        var saveOptions = new PdfSaveOptions();
13        saveOptions.BackgroundColor = Color.Gray;
14
15        // Convert SVG to PDF
16        Converter.ConvertSVG(document, saveOptions, Path.Combine(OutputDir, "lineto_out.pdf"));
17    }

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;
 6...
 7
 8	// Initialize an SVG document from a file
 9	using (var document = new SVGDocument(Path.Combine(DataDir, "light.svg")))
10	{
11	    // Initialize an instance of the PdfRenderingOptions class and set custom PageSetup and JpegQuality properties
12		var pdfRenderingOptions = new PdfRenderingOptions();
13	    pdfRenderingOptions.PageSetup.AnyPage = new Page(new Drawing.Size(500, 500), new Margin(10, 10, 10, 10));
14	    pdfRenderingOptions.JpegQuality = 10;
15
16		// Initialize an instance of the PdfDevice class
17	    using (IDevice device = new PdfDevice(pdfRenderingOptions, Path.Combine(OutputDir, "light_out.pdf")))
18	    {
19	        // Render SVG to PDF and send the document to the rendering device
20			document.RenderTo(device);
21	    }
22	}

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.

Text “Images rendered to PDF with various JpegQuality values”

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!

Text “Banner SVG to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.