2

C#/VB.NET:快速而简单的免费SVG到PDF转换技巧 - Carina-baby

 1 year ago
source link: https://www.cnblogs.com/Carina-baby/p/17486067.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在日常工作中,我们常常需要将SVG转换为PDF格式。这是因为SVG格式的图像在打印时可能会出现问题,例如失去分辨率或无法正确适应纸张大小。与此相比,PDF格式则专门用于打印和共享文档,可以确保高质量输出,并且能够自动适应不同的纸张大小。在本文中,我们将介绍如何使用编程方式将SVG文件转换为PDF,并以C#代码示例演示该过程。一起学起来吧!

1. 准备工作

方法1

​​Free Spire.PDF for .NET​​ 下载到本地,解压,找到 BIN 文件夹下的 Spire.PDF.dll。然后在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径 BIN 文件夹下的 dll 文件添加引用至程序

方法2:

通过​NuGet​​安装。可通过以下 2 种方法安装:

  1. 可以在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理 NuGet 包”,然后搜索“Free Spire.PDF”,点击“安装”。等待程序安装完成。

  2. 将以下内容复制到 PM 控制台安装。

Install-Package FreeSpire.PDF -Version 8.6.0

按照下列操作,仅需三步即可实现对SVG文件的转换。

  1. 创建PdfDocument对象。
  2. 使用PdfDocument.LoadFromFile() 方法加载一个示例SVG文件。
  3. 使用PdfDocument.SaveToFile(String, FileFormat) 方法将SVG文件转换为PDF。

完整代码

using Spire.Pdf;

namespace SVGtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PdfDocument对象
            PdfDocument doc = new PdfDocument();

            //加载一个示例SVG文件
            doc.LoadFromSvg("Sample.svg");

            //保存结果文档
            doc.SaveToFile("Result.pdf", FileFormat.PDF);
            doc.Dispose();
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace SVGtoPDF
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '创建PdfDocument对象
            Dim doc As PdfDocument = New PdfDocument()

            '加载一个示例SVG文件
            doc.LoadFromSvg("Sample.svg")

            '保存结果文档
            doc.SaveToFile("Result.pdf", FileFormat.PDF)
            doc.Dispose()
        End Sub
    End Class
End Namespace
2859233-20230616171103627-647714301.jpg

—本文完—


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK