文檔金喜正規買球>>E-iceblue中文文檔>>將 HTML 文件轉換為 PDF 和 XPS
將 HTML 文件轉換為 PDF 和 XPS
HTML 文件廣泛用于收集數據,出于安全,我們需要將其轉換為 PDF 和 XPS 格式的可打印文檔。您可以使用Spire.PDF輕松地將 URL 中的 HTML 頁面轉換為高質量的 PDF。本文將重點演示如何借助Spire.Doc將 HTML 文件轉換為 PDF 和 XPS 格式的可打印文檔。
首先,檢查將轉換為 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產品
如需獲取更多產品相關信息請咨詢