Scalable Vector Graphics (SVG) and Portable Document Format (PDF) are two widely used formats for creating and displaying graphics and documents. Sometimes, you may need to convert an SVG file into a PDF document in your C# application. Following such scenarios, this article explains how to convert SVG to PDF in C#.
SVG to PDF Converter - C# API Installation
For converting SVG images to PDF format, you need to configure Conholdate.Total for .NET from the New Releases section or using the following NuGet installation command:
PM> NuGet\Install-Package Conholdate.Total
Convert SVG to PDF in C# using the Converter
You can convert SVG to PDF file in C# by following the steps below:
- Load the source SVG file using SVGDocument class.
- Create PdfSaveOptions class object.
- Set the background color.
- Convert SVG to PDF document.
The code snippet below elaborates on how to convert SVG to PDF in C#:
using (var document = new Aspose.Svg.SVGDocument("circle1.svg")) | |
{ | |
// Initialize an instance of the PdfSaveOptions class | |
var saveOptions = new Aspose.Svg.Saving.PdfSaveOptions(); | |
saveOptions.BackgroundColor = System.Drawing.Color.Gray; | |
// Convert SVG to PDF | |
Aspose.Svg.Converters.Converter.ConvertSVG(document, saveOptions, Path.Combine(dataDir, "circle.pdf")); | |
} |
Convert SVG to PDF in C# using the Rendering Device
Here is another approach to render SVG to PDF document format. Please follow the steps below to perform the conversion with a few API calls:
- Initialize an SVG document from a file.
- Initialize an instance of the PdfRenderingOptions class and set custom PageSetup and JpegQuality properties.
- Initialize an instance of the PdfDevice class.
- Render SVG to PDF format.
The following sample code shows how to convert SVG to PDF in C#:
// Initialize an SVG document from a file | |
using (var document = new Aspose.Svg.SVGDocument(Path.Combine(dataDir, "circle.svg"))) | |
{ | |
// Initialize an instance of the PdfRenderingOptions class and set custom PageSetup and JpegQuality properties | |
var pdfRenderingOptions = new Aspose.Svg.Rendering.Pdf.PdfRenderingOptions(); | |
pdfRenderingOptions.PageSetup.AnyPage = new Aspose.Svg.Drawing.Page(new Aspose.Svg.Drawing.Size(500, 500), new Aspose.Svg.Drawing.Margin(10, 10, 10, 10)); | |
pdfRenderingOptions.JpegQuality = 10; | |
// Initialize an instance of the PdfDevice class | |
using (Aspose.Svg.Rendering.IDevice device = new Aspose.Svg.Rendering.Pdf.PdfDevice(pdfRenderingOptions, Path.Combine(dataDir, "out.pdf"))) | |
{ | |
// Render SVG to PDF and send the document to the rendering device | |
document.RenderTo(device); | |
} | |
} |
Free Evaluation License
You may get a free temporary license to avoid the evaluation limitations and test the API to its full capacity.
Summing Up
Converting SVG to PDF can be a useful functionality in various applications, especially when dealing with graphics and documentation. By following this guide, you’ve learned how to convert SVG files to PDF documents using C#. It enables you to embed SVG to PDF conversion feature into your .NET applications with different approaches. In case you face any problem or error during this file conversion then please reach out to us while mentioning all the details at the forum.
FAQs
Can I customize the conversion process?
Yes, you can customize the conversion process. You can control aspects like page size, layout, fonts, colors, and more in the generated PDF.
Are there any limitations to be aware of?
While SVG to PDF conversion is powerful, there might be some limitations based on the complexity of the SVG content. Advanced SVG features, such as animations, might not translate perfectly to PDF.
Are there any performance considerations?
Converting complex SVGs to PDF might require significant computational resources. Optimize your SVGs when possible and consider asynchronous processing for large batches of conversions.
How do I install the required libraries for SVG to PDF conversion?
A: You can install the required libraries using NuGet Package Manager in Visual Studio. Search for and install the Conholdate.Total package.