Yesi

可扩展标记语言(XML)文件是一种标准的文本文件,它使用特定的标记来描述文档的结构以及其他特性。通过将XML转换为PDF,能够便于文件传输及共享。本文,将介绍通过C#及VB.NET代码来实现该格式转换的方法。

引入dll

方法1

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

方法2

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

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

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

      Install-Package FreeSpire.Doc -Version 10.2.0

 

将XML转为PDF

以下是实现转换的详细代码步骤:

  • 创建Document类的对象。
  • 调用Document.LoadFromFile(string fileName)方法加载XML文件。
  • 使用Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存为PDF格式到指定路径。

C#

using Spire.Doc;

namespace XMLtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document类的对象
            Document doc = new Document();
            
            //加载XML文档
            doc.LoadFromFile("sample.xml", FileFormat.Xml);

            //保存为PDF文档到指定路径
            doc.SaveToFile("XMLToPDF.pdf", FileFormat.PDF);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace XMLtoPDF
    Class Program
        Private Shared Sub Main(args As String())
            '创建Document类的对象
            Dim doc As New Document()

            '加载XML文档
            doc.LoadFromFile("sample.xml", FileFormat.Xml)

            '保存为PDF文档到指定路径
            doc.SaveToFile("XMLToPDF.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

转换结果:

 

★ 推荐阅读:Java 将XML转为PDF

 

—END—

 

相关文章: