700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Word控件Spire.Doc 转换教程(十五):在 C# 中将 HTML 文件转换为 PDF 和 XPS

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

时间:2018-10-17 14:04:08

相关推荐

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

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

Spire.Doc 最新下载

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

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

然后, 通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..\Spire.Doc\Bin\NET4.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 ponentModel;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);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。