本篇文章主要介绍了如何使用Spire.Doc 在 C# 中将 HTML 文件转换为 PDF 和 XPS,欢迎查阅!
首先,检查将转换为 PDF 和 XPS 的 html 文件。
其次,下载 Spire.Doc 并安装在您的系统上。Spire.Doc 安装干净、专业,并包含在 MSI 安装程序中。
然后, 通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..Spire.DocBinNET4.0 Spire.Doc.dll”。

现在介绍如何将 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 文件作为结果。

完整代码:
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进行处理,非常感谢!