zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

2023-09-11 14:14:49 时间

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

Spire.Doc for.NET 最新下载

首先,检查将转换为 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 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产品  或加入Q群(767755948)