翻譯|使用教程|編輯:胡濤|2022-03-16 16:46:09.450|閱讀 316 次
概述:本篇文章主要介紹了如何使用Spire.Doc 在 C# 中將 HTML 文件轉換為 PDF 和 XPS,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn