Convert SVG to PDF in C#/VB.NET

With Spire.PDF for .NET, we could save PDF to SVG easily. Starts from Spire.PDF V 6.5.6, it supports to convert SVG image to PDF. This article will demonstrate how to add a SVG image to PDF file and convert SVG to PDF.

Convert SVG to PDF

Spire.PDF offers a method of doc.LoadFromSvg() to load the SVG directly and doc.SaveToFile()method to save to PDF file.

Firstly, view the Sample SVG image:

Convert SVG to PDF in C#/VB.NET

C#
using Spire.Pdf;

namespace SVGtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromSvg("Sample.svg");

            doc.SaveToFile("Result.pdf", FileFormat.PDF);  

        }
    }
}
VB.NET
Imports Spire.Pdf

Namespace SVGtoPDF
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim doc As PdfDocument = New PdfDocument
            doc.LoadFromSvg("Sample.svg")
            doc.SaveToFile("Result.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Effective screenshot after converting SVG to PDF:

Convert SVG to PDF in C#/VB.NET

Add SVG image to PDF file

Besides convert the SVG directly to PDF, it also supports to add an existing SVG image to PDF file. It also supports to set the position and size of the SVG image after adding it to PDF. Please check the steps as below:

C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddSVGImagetoPDF
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument doc1 = new PdfDocument();
            doc1.LoadFromSvg("Sample.svg");

            PdfDocument doc2 = new PdfDocument();
            doc2.LoadFromFile("Sample2.pdf");

            PdfTemplate template = doc1.Pages[0].CreateTemplate();


            //template.Draw(doc2.Pages[0].Canvas, new PointF());

            //set the position and size of SVG image
            doc2.Pages[0].Canvas.DrawTemplate(doc1.Pages[0].CreateTemplate(), new PointF(80, 80), new SizeF(300, 300));

            doc2.SaveToFile("Result0.pdf", FileFormat.PDF);   


        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace AddSVGImagetoPDF
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim doc1 As PdfDocument = New PdfDocument
            doc1.LoadFromSvg("Sample.svg")
            Dim doc2 As PdfDocument = New PdfDocument
            doc2.LoadFromFile("Sample2.pdf")
            Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate
            'template.Draw(doc2.Pages[0].Canvas, new PointF());
            'set the posion and size of SVG image
            doc2.Pages(0).Canvas.DrawTemplate(doc1.Pages(0).CreateTemplate, New PointF(80, 80), New SizeF(300, 300))
            doc2.SaveToFile("Result0.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Effective screenshot after adding a SVG image to PDF file:

Convert SVG to PDF in C#/VB.NET