How to Convert SVG to PDF Using C# and VB.NET in ASP.NET Core?
The Syncfusion Essential® PDF is a feature-rich and high performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can convert SVG to PDF using C#
Steps to convert SVG to PDF programmatically:
- Create a new console application project.
- Install the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference to your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf
- Use the following code sample in Program.cs to convert SVG to PDF.
C#
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
string url = Path.GetFullPath(@"Sample.svg");
//Convert a SVG file to PDF with HTML converter
PdfDocument document = htmlConverter.Convert(url);
//Save the PDF document
// Save the document to a memory stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
// Close the document and release all resources
document.Close(true);
// Write the contents of the memory stream to a file
File.WriteAllBytes("SvgToPDF.pdf", stream.ToArray());
' Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
' Get the full path of the SVG file
Dim url As String = Path.GetFullPath("Sample.svg")
' Convert the SVG file to a PDF document using the HTML converter
Dim document As PdfDocument = htmlConverter.Convert(url)
' Create a memory stream to save the PDF document
Dim stream As New MemoryStream()
' Save the PDF document to the memory stream
document.Save(stream)
' Close the PDF document and release all resources
document.Close(True)
' Write the contents of the memory stream to a PDF file
File.WriteAllBytes("SvgToPDF.pdf", stream.ToArray())
A complete working sample can be downloaded from SVG_To_PDF.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you will find other options like Html string to PDF, partial webpage to PDF, Html to single PDF page and Html to PDF conversion using IE Rendering with code examples.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
Conclusion
I hope you enjoyed learning on how to convert SVG to PDF using C# and VB.NET in ASP.NET Core.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data. For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!