Word控件Spire.Doc 转换教程(十五):在 C# 中将 HTML 文件转换为 PDF 和 XPS

本篇文章主要介绍了如何使用Spire.Doc 在 C# 中将 HTML 文件转换为 PDF 和 XPS,欢迎查阅!

HTML 文件广泛用于收集数据,出于安全,我们需要将其转换为 PDF 和 XPS 格式的可打印文档。您可以使用Spire.PDF轻松地将 URL 中的 HTML 页面转换为高质量的 PDF。本文将重点演示如何借助

首先,检查将转换为 PDF 和 XPS 的 html 文件。

其次,下载 Spire.Doc 并安装在您的系统上。Spire.Doc 安装干净、专业,并包含在 MSI 安装程序中。

然后,   通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..Spire.DocBinNET4.0 Spire.Doc.dll”。

Convert HTML file to PDF and XPS

现在介绍如何将 HTML 转换为 PDF 和 XPS 的步骤。

第 1 步:从文件加载 HTML 文件。

Document document = new Document();document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None) ;

第 2 步:将 HTML 保存为 PDF 和 XPS 的文件格式。

//Save html to PDF.document.SaveToFile("Sample.pdf", FileFormat.PDF);//Save html to XPS.document.SaveToFile("Sample.xps", FileFormat.XPS);

调试后,请检查以下 PDF 和 XPS 文件作为结果。

HTML 转 PDF

完整代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace HTML2PDFXPS{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Document document = new Document();document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None);//Save html to PDF.document.SaveToFile("Sample.pdf", FileFormat.PDF);}private void button2_Click(object sender, EventArgs e){Document document = new Document();document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None);//Save html to PDF.document.SaveToFile("Sample.xps", FileFormat.XPS);}}}


欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询在线客服 
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月12日
下一篇 2022年1月12日

相关推荐

发表回复

登录后才能评论