Convert SVG to PDF in C#

Do you want to convert SVG images into PDF documents? If so, you are in the right place. This article will show you how to convert SVG to PDF in C# by following a few simple steps.

SVG (Scalable Vector Graphics) is an open-standard file type that works regardless of resolution. It is an XML-based format that uses vector graphics and supports limited animation. SVG files are just XML files, so any text editor can edit them. Most modern browsers support SVG files and are capable of rendering their markup. In certain cases, we may need to export SVG images to PDF documents.

PDF is the most widely used document format. It keeps the original formatting intact, which is why it is so popular. We can easily convert SVG documents to PDF format programmatically. So let’s get started!

The following topics will be covered in this article:

  1. C# API to Convert SVG to PDF
  2. Convert SVG to PDF
  3. Render SVG to PDF

C# API to Convert SVG to PDF

For converting SVG to PDF, we will be using the Aspose.SVG for .NET API. The API provides an easy way to load, parse, render, create, and convert SVG files into popular formats without any software dependencies.

The API provides the SVGDocument class that represents the root of the SVG hierarchy and holds the entire content. The PdfSaveOptions class allows specifying PDF save options. We have the Converter.ConvertSVG(SVGDocument, PdfSaveOptions, string) method converts SVG source to a PDF. The PdfRenderingOptions class represents rendering options for PdfDevice. The PdfDevice class allows rendering to a PDF document.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.SVG

Convert SVG to PDF using C#

We can easily convert an SVG image file to a PDF by following the steps given below:

  1. Firstly, load an SVG using the SVGDocument class.
  2. Next, create an instance of the PdfSaveOptions class.
  3. After that, specify PDF save options such as BackgroundColor, etc.
  4. Finally, call the Converter.ConvertSVG() method. It takes SVGDocument, PdfSaveOptions and the output PDF file path as arguments.

The following code sample shows how to convert SVG to PDF in C#.

Convert-SVG-to-PDF-using-CSharp

Convert SVG to PDF using C#.

Render SVG to PDF in C#

Alternatively, we can render an SVG to a PDF by following the steps given below:

  1. Firstly, load an SVG using the SVGDocument class.
  2. Next, create an instance of the PdfRenderingOptions class.
  3. Then, specify PDF save options such as PageSetup, JpegQuality, etc.
  4. After that, initialize an instance of the PdfDevice class. It takes PdfRenderingOptions and the output PDF file path as arguments.
  5. Finally, call the RenderTo() method. It takes PdfDevice as an argument.

The following code sample shows how to render SVG to PDF in C#.

Get Free Temporary License

You can get a free temporary license to try Aspose.SVG for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • load an existing SVG image;
  • define PDF save options;
  • specify PDF rendering options;
  • save SVG as PDF in C#.

Besides converting SVG to PDF in C#, you can learn more about Aspose.SVG for .NET using documentation and explore different features supported by the API. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also