6

C#/VB.NET 将HTML转为XPS

 2 years ago
source link: https://blog.51cto.com/miayo/5399644
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

XPS文件是一种静态文档格式,它描述了文档结构和内容,包括布局和外观等,通常,该格式的文件可通过XPS Viewer及浏览器来打开。通过后端程序代码,我们可以将动态的Html文件转换为XPS格式;本文,将以C#和VB.NET代码为例来展示如何实现转换。


dll引入

1.通过 ​NuGet安装dll(2种方法)

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

  1.2 将以下内容复制到PM控制台安装:

Install-Package FreeSpire.Doc -Version 10.2

2.手动添加dll引用

可通过手动​ ​下载包​​到本地,然后解压,安装到本地指定路径。完成安装后,找到安装路径BIN文件夹下的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。


将HTML转为XPS

转换时,只需在调用Document.SaveToFile()方法时选择目标文件格式的枚举值为XPS即可。下面是转换时用到的主要类及方法步骤:

  1. 创建Document类的对象。
  2. 调用Document.LoadFromFile(string fileName, FileFormat fileFormat)方法加载HTML文件。
  3. 通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存为XPS格式到指定路径。

C#

using Spire.Doc;

namespace HTMLtoXPS
{
class Program
{
static void Main(string[] args)
{
//创建Document类的对象
Document doc = new Document();

//加载html文件
doc.LoadFromFile("sample.html",FileFormat.Html);

//保存为XPS格式
doc.SaveToFile("HtmlToXPS.xps",FileFormat.XPS);
}
}
}

vb.net

Imports Spire.Doc

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

'加载html文件
doc.LoadFromFile("sample.html", FileFormat.Html)

'保存为XPS格式
doc.SaveToFile("HtmlToXPS.xps", FileFormat.XPS)
End Sub
End Class
End Namespace
C#/VB.NET 将HTML转为XPS_.NET

—END—


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK