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:
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:
- Firstly, load an SVG using the SVGDocument class.
- Next, create an instance of the PdfSaveOptions class.
- After that, specify PDF save options such as BackgroundColor, etc.
- 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#.
Render SVG to PDF in C#
Alternatively, we can render an SVG to a PDF by following the steps given below:
- Firstly, load an SVG using the SVGDocument class.
- Next, create an instance of the PdfRenderingOptions class.
- Then, specify PDF save options such as PageSetup, JpegQuality, etc.
- After that, initialize an instance of the PdfDevice class. It takes PdfRenderingOptions and the output PDF file path as arguments.
- 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.