C × / vb.net add SVG picture to PDF, convert to PDF

Keywords: ASP.NET

The following content describes how to add SVG pictures to PDF documents and how to convert SVG pictures into PDF documents in C ා program.

1, Environmental preparation

Download the PDF class library tool, fire.pdf for. Net hotfix 6.5.6 and above (pay attention to the version information when downloading). After downloading, extract the file, and add the Spire.Pdf.dll file under the lib folder to the solution explorer in VS. In addition, it can be downloaded through Nuget.
dll reference effect is as follows:

The SVG image used for the test is as follows:

2, Code example

1. Add SVG to PDF document
C#

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

namespace InsertSVGImage_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load SVG picture
            PdfDocument file1 = new PdfDocument();
            file1.LoadFromSvg("Image.svg");

            //Create a PDF document and add a page
            PdfDocument pdf = new PdfDocument();
            pdf.AppendPage();

            //Create template from SVG image and draw template to PDF
            PdfTemplate template = file1.Pages[0].CreateTemplate();
            template.Draw(pdf.Pages[0].Canvas, new PointF());

            //Save PDF document
            pdf.SaveToFile("AddSVGtoPDF.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("AddSVGtoPDF.pdf");
        }
    }
}

SVG image adding effect:

VB.NET

Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace InsertSVGImage_PDF

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            ''load SVG picture
            Dim file1 As PdfDocument = New PdfDocument
            file1.LoadFromSvg("Image.svg")
            ''Create a PDF Documents, adding a page
            Dim pdf As PdfDocument = New PdfDocument
            pdf.AppendPage
            ''according to SVG Picture create template and draw template to PDF  
            Dim template As PdfTemplate = file1.Pages(0).CreateTemplate
            template.Draw(pdf.Pages(0).Canvas, New PointF)
            ''preservation PDF file
            pdf.SaveToFile("AddSVGtoPDF.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("AddSVGtoPDF.pdf")
        End Sub
    End Class
End Namespace

2. Convert SVG image to PDF document
C#

using Spire.Pdf;

namespace SVGtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load SVG picture
            PdfDocument doc = new PdfDocument();
            doc.LoadFromSvg("Image.svg");

            //Call SaveToFile() to save as PDF
            doc.SaveToFile("ConvertSVGtoPDF.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("ConvertSVGtoPDF.pdf");
        }
    }
}

SVG to PDF effect:

VB.NET

Imports Spire.Pdf

Namespace SVGtoPDF

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            ''load SVG picture
            Dim doc As PdfDocument = New PdfDocument
            doc.LoadFromSvg("Image.svg")
            ''Call method SaveToFile()Save as PDF format
            doc.SaveToFile("ConvertSVGtoPDF.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("ConvertSVGtoPDF.pdf")
        End Sub
    End Class
End Namespace

< done >

Posted by heerajee on Thu, 14 May 2020 07:32:56 -0700