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

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);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值