PDFSharp HTML轉PDF範例及教程比較
IronPDF 与 PDFsharp
全面的 .NET PDF 庫比較——逐項功能對比,並提供基於事實的上下文分析
| 特點 | PDFsharp | IronPDF ✦ |
|---|---|---|
| PDF Creation & Conversion | ||
| HTML/CSS 轉 PDF |
✕ No
文件中記錄的核心功能是透過 PDF 物件模型建立/修改/合併/拆分;沒有描述 HTML 渲染引擎。
|
✓ Yes
基於 Chromium 核心的引擎,內建像素級精準的 CSS3、Flexbox 和 Grid 渲染圖。
|
| JavaScript 執行 |
✕ No
不是瀏覽器引擎;沒有描述JS執行模型。
|
✓ Yes
在渲染過程中完全執行 JS——動態圖表、SPA 和互動式內容。
|
| 程式化生成 |
✓ Yes
"即時建立 PDF 文件",並使用 PDF 物件模型來編寫文件。
|
✓ Yes
可從 HTML 模板、字串、ASPX 視圖或圖像生成內容。 Chromium 負責佈置。
|
| URL 到 PDF |
✕ No
不具備網頁瀏覽或截圖功能。
|
✓ Yes
RenderUrlAsPdf() 捕獲任何實時 URL 並進行完整的 CSS/JS 渲染。 |
| DOCX 到 PDF |
✕ No
不具備Word文檔轉換功能。
|
✓ Yes
DocxToPdfRenderer 轉換 Word 文檔,保留其結構和格式。 |
| Reading & Extraction | ||
| 文字抽取 |
~ Complex
官方常見問題中提到,只能進行低階提取;沒有高級的"單字/段落/頁面"提取 API。
|
✓ Yes
提取文字時能夠感知佈局。可與 IronOCR 結合使用,用於識別掃描文件。
|
| 將頁面渲染為圖像 |
✕ No
PDFsharp 文件/社群明確指出它"無法渲染"PDF(不支援頁面→點陣圖)。
|
✓ Built-in
原生柵格化為PNG、JPEG、BMP格式,DPI可設定。
|
| 內建OCR |
✕ No
PDFsharp 功能文件中未描述 OCR 引擎。
|
✓ Via IronOCR
與 IronOCR 原生集成,可對掃描的 PDF 文件進行 127 種以上語言的 OCR 識別。
|
| Editing & Manipulation | ||
| Merging & Splitting |
✓ Yes
官方功能明確包括"修改、合併和拆分現有 PDF 文件"。
|
✓ Yes
透過直覺的 API,實現一行式合併、分割、追加、前置和頁面重新排序。
|
| Headers, Footers & Page Numbers |
~ Complex
僅限底層;結構化頁首/頁尾被視為 MigraDoc 用例。
|
✓ Yes
基於 HTML 的頁首/頁腳,帶有自動頁碼、日期和自訂內容。
|
| 水印 |
~ Complex
水印範例雖然存在,但使用的是繪圖方法(並非真正的水印註釋)。
|
✓ Yes
ApplyWatermark() 支援 HTML/CSS——完全控制透明度、旋轉角度和位置。 |
| Stamp Text & Images |
~ Complex
可以透過使用 GDI+ 風格的 API 對現有頁面進行底層繪圖操作來實現。
|
✓ Yes
TextStamper 以及 Google Fonts、定位和頁面級控制的ImageStamper 。 |
| 編輯內容 |
✕ No
PDFsharp功能集中不支援內容編輯。
|
✓ Yes
RedactTextOnAllPages() 永久刪除一行中的敏感文字。
|
| Security & Compliance | ||
| Encryption & Passwords |
✓ Yes
支援透過安全性設定 API 進行加密和密碼保護。
|
✓ Yes
AES 加密、擁有者/使用者密碼、細粒度權限(列印、複製、註釋)。
|
| 數位簽名 |
✓ Yes
PDFsharp 文件描述了新增數位簽章;該功能在 PDFsharp 6.2.0 中引入。
|
✓ Yes
PdfSignature 支援 X509/PFX 證書。 |
| PDF/A & PDF/UA Compliance |
⚠ Partial
可以建立新的 PDF/A 文檔,但不能將現有的 PDF 轉換為 PDF/A,不能驗證一致性,也不能選擇特定的 PDF/A 等級(預覽中標記為"hack")。
|
✓ Yes
適用於企業環境的原生 PDF/A 歸檔和 PDF/UA 無障礙存取合規性。
|
| Platform & Deployment | ||
| 跨平台支援 |
✓ Yes
NuGet/套件文件指出核心建置不依賴 Windows,可以在 Linux/macOS 上運作;文件列出了現代 .NET 目標。
|
✓ Yes
Windows、Linux、macOS、x64、x86、ARM。 .NET 6–10、Core、Standard 2.0+、Framework 4.6.2+。
|
| 伺服器/Docker/雲 |
✓ Yes
NuGet 聲明核心建置不依賴 Windows;典型的伺服器部署很簡單(無需外部二進位)。
|
✓ Yes
Docker、Azure、AWS、IIS。官方 Docker 映像和部署指南。
|
| 安裝簡單 |
⚠ Partial
安裝很簡單,但產生複雜的佈局通常需要底層組合(或 MigraDoc 抽象)。
|
✓ Simple
只需一條 NuGet
Install-Package IronPdf安裝。 |
| Licensing & Support | ||
| 許可模式 |
✓ MIT
PDFsharp 專案採用 MIT 授權—可免費用於商業和專有用途。
|
✓ Commercial
永久授權。 30天全功能免費試用,無浮水印。
|
| Commercial Support & SLA |
✓ Yes
供應商網站提供"支援計畫"和針對 PDFsharp 專案的客製化解決方案。
|
✓ 24/5 Support
提供專屬工程支持,並保證服務等級協議 (SLA)——電子郵件、線上聊天、電話。
|
| 文件 |
✓ Yes
官方文件列舉了各項功能並提供了入門指南;還有範例/樣本庫。
|
✓ Extensive
完整的 API 參考、100 多個操作指南、教學課程、程式碼範例、故障排除、影片。
|
資料來源於 PDFsharp 官方文件、NuGet 套件文件和社群參考資料。
PDFsharp 是一款可靠的、採用 MIT 許可的 PDF 創建軟體,但缺少 HTML 渲染、OCR、文字擷取和頁面柵格化功能。 IronPDF提供全面覆蓋範圍——可免費試用 30 天。
在 .NET 環境中處理 PDF 檔案時,開發人員需要一個可靠且功能豐富的函式庫來處理文件建立、編輯、加密和 HTML-to-PDF 轉換等常見工作。 IronPDF 和 PDFsharp 是兩個廣受歡迎的函式庫,但它們服務的需求不同。
IronPDF 是一個全功能、高效能的 PDF 函式庫,在將 HTML 轉換為 PDF、PDF 安全性以及進階 PDF 操作方面都有傑出的表現。 PDFsharp 則是一個輕量級的開放原始碼程式庫,專注於 PDF 的建立與基本編輯。 在兩者之間做選擇取決於您專案的複雜性和需求。
在本指南中,我們將比較 IronPDF 和 PDFsharp 在主要功能、可用性、安全性、價格和支援方面的優劣--幫助您決定最適合您的 .NET 應用程式的工具。
功能概述
IronPDF:全面的 PDF 解決方案
IronPDF for .NET 是一個功能豐富的 .NET 函式庫,在許多領域都有傑出的表現,包括 HTML-to-PDF轉換、PDF編輯和安全文件處理。 可使用 NuGet Package Manager Console 輕鬆將其新增至您的專案中,並與您的 C# 專案無縫整合。
其突出功能包括
PDFsharp:輕量級的替代方案
PDFsharp 是一個免費的開放原始碼程式庫,主要設計用於基本的 PDF 建立與操作。 雖然 IronPDF 的功能不如 IronPDF 豐富,但對於不需要複雜功能的簡單專案而言,IronPDF 是絕佳的選擇。 PDFsharp 的一些核心功能包括
- PDF 建立:使用 C# 或VB.NET以程式設計方式產生 PDF,建立包含圖像和圖形的文字文件。
- PDF 修改:簡單的任務,例如合併、拆分和編輯現有 PDF。 *基本加密:為 PDF 文件新增基本密碼保護。
主要限制: PDFsharp 不支援 HTML 到 PDF 的原生轉換,需要第三方工具,如 Polybioz.HtmlRenderer.PdfSharp.Core,增加了複雜性。
相容性和可用性
跨平台支援

IronPDF。
IronPDF for .NET 的設計是跨平台的,與 .NET Core、.NET Framework 和 Azure 完全相容,並支援 Windows、macOS 和 Linux。 這使其成為需要在多種作業系統中執行的專案的理想選擇,並確保該函式庫與大多數開發環境具有通用相容性。 該資料庫可與現代網路開發環境無縫整合,是網路應用程式、雲端解決方案和企業系統的完美選擇。
PDFsharp。
PDFsharp 主要針對 .NET Framework 而設計,並對 .NET Core 提供有限的支援。 雖然有 .NET Core 版本,但它仍在開發中,某些功能尚未針對非 Windows 平台完全最佳化。 Linux 和 macOS 使用者可能會面臨相容性問題,使得 PDFsharp 成為跨平台應用程式較不理想的選擇。
易用性
IronPDF 提供開發人員友善的 API,將處理 PDF 檔案的複雜性抽象化。 不論是產生 PDF 或編輯現有檔案,語法都很乾淨直覺,讓開發人員只需幾行程式碼就能實現複雜的功能。
PDFsharp 雖然在文字產生和 PDF 操作等基本任務上相對容易使用,但在複雜的文件產生和編輯等任務上,通常需要較多的手動處理。
易用性範例
IronPDF:將 HTML 轉換為 PDF
只需一次方法調用,即可輕鬆將 HTML 程式碼片段和內容轉換為 PDF:
using IronPdf;
var renderer = new ChromePdfRenderer();
var content = "<h1>IronPDF Rocks!</h1>";
var pdf = renderer.RenderHtmlAsPdf(content);
pdf.SaveAs("output.pdf");
using IronPdf;
var renderer = new ChromePdfRenderer();
var content = "<h1>IronPDF Rocks!</h1>";
var pdf = renderer.RenderHtmlAsPdf(content);
pdf.SaveAs("output.pdf");
Imports IronPdf
Private renderer = New ChromePdfRenderer()
Private content = "<h1>IronPDF Rocks!</h1>"
Private pdf = renderer.RenderHtmlAsPdf(content)
pdf.SaveAs("output.pdf")
- 簡潔、乾淨的編碼
- 從現有的 HTML 內容輕鬆轉換
PDFsharp:使用文字建立 PDF
PDFsharp 需要手動執行,透過使用圖形物件來繪製文字和內容:
using PdfSharp.Pdf;
using PdfSharp.Drawing;
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyleEx.Bold);
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XPoint(50, 100));
document.Save("output.pdf");
using PdfSharp.Pdf;
using PdfSharp.Drawing;
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyleEx.Bold);
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XPoint(50, 100));
document.Save("output.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Private document = New PdfDocument()
Private page = document.AddPage()
Private gfx = XGraphics.FromPdfPage(page)
Private font = New XFont("Arial", 20, XFontStyleEx.Bold)
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, New XPoint(50, 100))
document.Save("output.pdf")
限制條件:
手動執行 - 您必須手動定位元素。 較複雜,適用於產生報表、發票或樣式化內容。
功能比較
HTML 至 PDF 轉換
IronPDF。:最適合 Web 應用程式與動態報表。
IronPDF 擅長將 HTML 報告、網頁和動態內容轉換為 PDF,同時保留 CSS、JavaScript 和內嵌圖片。 這使得它非常適合
- 從 HTML 模板產生發票。
- 從網路應用程式自動建立報表。
- 將網頁擷取為 PDF,以供離線使用。
IronPDF 程式碼範例 - 將 HTML 檔案轉換為 PDF 格式:
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("example.pdf");
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("example.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlFileAsPdf("example.html")
pdf.SaveAs("example.pdf")
輸出

如您所見,有了 IronPDF,我們只需簡單的三行程式碼,就能將有樣式的 HTML 檔案轉換成 PDF 文件,而且不失原有的品質和排版! 如果您想了解 IronPDF 如何處理 CSS 較重的內容,我們何不看一個範例,將 this URL 的內容轉換成 PDF?
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("Apple.pdf");
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("Apple.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.EnableJavaScript = True
renderer.RenderingOptions.WaitFor.JavaScript(5000)
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.apple.com")
pdf.SaveAs("Apple.pdf")
輸出
。
在此,我們利用 IronPDF 強大的渲染選項,將網頁內容轉換成高品質的 PDF,並保留所有原始的 CSS 和 JavaScript 內容。
PDFsharp。:最適合無網頁內容的靜態 PDF
PDFsharp 本身不支援 HTML 至 PDF 的轉換。 如果您需要這項功能,您必須使用第三方函式庫,例如 Html Renderer,它提供使用靜態渲染程式碼將 HTML 內容轉換成 PDF 文件的功能。 由於這些限制,PDFsharp 通常最適用於以下用途:
- 不依賴動態 HTML 呈現的靜態文件。
- 以程式化方式定義內容的發票、合約和報告。
PDFsharp with HtmlRenderer 示例:
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
var document = new PdfSharpCore.Pdf.PdfDocument();
string htmlContent = File.ReadAllText("index.html");
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
document.Save("html-to-pdf-pdfsharp.pdf");
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
var document = new PdfSharpCore.Pdf.PdfDocument();
string htmlContent = File.ReadAllText("index.html");
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
document.Save("html-to-pdf-pdfsharp.pdf");
Imports PdfSharp.Pdf
Imports TheArtOfDev.HtmlRenderer.PdfSharp
Private document = New PdfSharpCore.Pdf.PdfDocument()
Private htmlContent As String = File.ReadAllText("index.html")
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4)
document.Save("html-to-pdf-pdfsharp.pdf")
輸出
!a href="/static-assets/pdf/blog/pdfsharp-read-pdf/pdfsharp-read-pdf-4.webp">PDFsharp HTML-to-PDF 範例輸出。
感謝 HtmlRenderer 附加元件,我們能夠將絕大多數的 HTML 內容轉換為 PDF,同時保留大部分的原始樣式,但是當要處理 CSS 較重的內容時,它如何對抗 IronPDF 呢? 讓我們來看看將網頁內容轉換成 PDF 檔案背後的流程:
using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
public class Program
{
public static async Task Main(string[] args)
{
// Define the URL to fetch HTML content
string url = "https://www.example.com"; // Replace with the URL you want to convert
// Fetch HTML content from the URL
string htmlContent = await FetchHtmlFromUrl(url);
// Generate the PDF from the fetched HTML content
var document = new PdfSharpCore.Pdf.PdfDocument();
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
// Save the generated PDF to a file
document.Save("url-to-pdf-example.pdf");
System.Console.WriteLine("PDF created successfully!");
}
// Method to fetch HTML content from a URL
private static async Task<string> FetchHtmlFromUrl(string url)
{
using (HttpClient client = new HttpClient())
{
// Send GET request to fetch the HTML content of the URL
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
// Read the content as a string
string htmlContent = await response.Content.ReadAsStringAsync();
return htmlContent;
}
else
{
throw new Exception($"Failed to fetch content from URL. Status Code: {response.StatusCode}");
}
}
}
}
using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
public class Program
{
public static async Task Main(string[] args)
{
// Define the URL to fetch HTML content
string url = "https://www.example.com"; // Replace with the URL you want to convert
// Fetch HTML content from the URL
string htmlContent = await FetchHtmlFromUrl(url);
// Generate the PDF from the fetched HTML content
var document = new PdfSharpCore.Pdf.PdfDocument();
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
// Save the generated PDF to a file
document.Save("url-to-pdf-example.pdf");
System.Console.WriteLine("PDF created successfully!");
}
// Method to fetch HTML content from a URL
private static async Task<string> FetchHtmlFromUrl(string url)
{
using (HttpClient client = new HttpClient())
{
// Send GET request to fetch the HTML content of the URL
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
// Read the content as a string
string htmlContent = await response.Content.ReadAsStringAsync();
return htmlContent;
}
else
{
throw new Exception($"Failed to fetch content from URL. Status Code: {response.StatusCode}");
}
}
}
}
Imports PdfSharpCore.Pdf
Imports TheArtOfDev.HtmlRenderer.PdfSharp
Imports System.Net.Http
Imports System.Threading.Tasks
Imports System.IO
Public Class Program
Public Shared Async Function Main(ByVal args() As String) As Task
' Define the URL to fetch HTML content
Dim url As String = "https://www.example.com" ' Replace with the URL you want to convert
' Fetch HTML content from the URL
Dim htmlContent As String = Await FetchHtmlFromUrl(url)
' Generate the PDF from the fetched HTML content
Dim document = New PdfSharpCore.Pdf.PdfDocument()
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4)
' Save the generated PDF to a file
document.Save("url-to-pdf-example.pdf")
System.Console.WriteLine("PDF created successfully!")
End Function
' Method to fetch HTML content from a URL
Private Shared Async Function FetchHtmlFromUrl(ByVal url As String) As Task(Of String)
Using client As New HttpClient()
' Send GET request to fetch the HTML content of the URL
Dim response As HttpResponseMessage = Await client.GetAsync(url)
If response.IsSuccessStatusCode Then
' Read the content as a string
Dim htmlContent As String = Await response.Content.ReadAsStringAsync()
Return htmlContent
Else
Throw New Exception($"Failed to fetch content from URL. Status Code: {response.StatusCode}")
End If
End Using
End Function
End Class
輸出

雖然 PDFsharp 無法像 IronPDF 一樣處理密集的 CSS 和 JavaScript 內容,但對於那些正在尋找輕量級 PDF 函式庫,以執行不依賴 CSS 或 JavaScript 內容的 PDF 產生任務的人來說,PDFsharp 仍是有力的競爭者。 雖然它需要額外的函式庫來進行 HTML 轉換,但 PDFsharp 能夠在 .NET 生態系統內從頭開始建立 PDF 檔案。
加密與安全性
IronPDF。:最適合安全、合規驅動的應用程式。
IronPDF 是需要安全處理 PDF 的開發人員的首選,尤其是在需要符合嚴格資料保護規定的應用程式中。 它提供先進的安全功能,旨在確保您的 PDF 文件保持安全和防篡改,因此適用於金融、醫療保健和法律等行業。
IronPDF 的使用案例:
- 法律和財務文件: IronPDF 可讓您使用 先進的加密方法保護 PDF 中的敏感資料,確保只有授權使用者才能存取。
- 需要數位簽名的文件:它提供數位簽名功能,可確保文件的真實性,這在法律協議和合約中至關重要。
- GDPR 和 HIPAA 合規性:在處理個人或敏感信息時,IronPDF 支援符合GDPR和HIPAA等法規的加密標准,讓處理安全客户數据的開發人员高枕無忧。
IronPDF 的加密功能包括使用者層級和擁有者層級的密碼,這意味著您可以限制 PDF 的存取權限,或限制特定的動作,例如列印、複製或編輯。 此外,它還可讓您在 PDF 上套用數位簽章,確保資料的完整性和不可抵賴性,因此非常適合法律文件。 這些功能在文件安全性極為重要的工作流程中特別有用。
IronPDF 程式碼範例 - 使用密碼加密 PDF:
using IronPdf;
var pdf = PdfDocument.FromFile("ConfidentialDocument.pdf");
pdf.SecuritySettings.UserPassword = "user";
pdf.SecuritySettings.OwnerPassword = "owner";
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SaveAs("ConfidentialDocumentSecured.pdf");
using IronPdf;
var pdf = PdfDocument.FromFile("ConfidentialDocument.pdf");
pdf.SecuritySettings.UserPassword = "user";
pdf.SecuritySettings.OwnerPassword = "owner";
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SaveAs("ConfidentialDocumentSecured.pdf");
Imports IronPdf
Private pdf = PdfDocument.FromFile("ConfidentialDocument.pdf")
pdf.SecuritySettings.UserPassword = "user"
pdf.SecuritySettings.OwnerPassword = "owner"
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SaveAs("ConfidentialDocumentSecured.pdf")
輸出

在這個範例中,IronPDF 加密 PDF 並限制使用者的動作,確保文件不會受到未經授權的存取或竄改。
PDFsharp。:最適合基本安全需求
PDFsharp 提供基本的密碼保護功能,但缺乏數位簽章、水印或精密加密等進階功能。 對於只需要限制 PDF 存取權限或套用基本密碼的簡單使用個案,PDFsharp 非常適合。 但是,如果您的應用程式需要更進階的安全功能,例如安全文件簽署或加密以符合法規標準,您可能會發現 PDFsharp 的不足。
PDFsharp 的使用案例:
- 保護內部文件: PDFsharp 適合使用 基本密碼保護來保護 PDF,以防止未經授權存取內部報告、發票和合約。
- 機密檔案的基本加密:如果您的應用程式不需要合規驅動的安全性,但您仍希望確保未經授權的使用者無法開啟敏感的 PDF 檔案,PDFsharp 的密碼保護就是一個簡單的解決方案。
然而,PDFsharp 的加密功能與 IronPDF 相比有限,因此不適合需要強大安全性的場合。 它可以使用使用者密碼保護 PDF,並限制某些動作,例如列印和複製,但它無法提供金融或醫療保健等產業所需的強大加密標準。
PDFsharp 程式碼範例 - 基本 PDF 加密:
using PdfSharp.Pdf;
using PdfSharp.Pdf.Security;
using PdfSharp.Drawing;
// Create a new document
var document = new PdfSharp.Pdf.PdfDocument();
// Add a page and draw something on it
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 16, XFontStyleEx.Bold);
gfx.DrawString("Confidential Document", font, XBrushes.Black, new XPoint(50, 50));
// Set password protection
document.SecuritySettings.UserPassword = "password123";
document.SecuritySettings.OwnerPassword = "admin456";
// Save the encrypted PDF
document.Save("pdfsharp-encrypted.pdf");
using PdfSharp.Pdf;
using PdfSharp.Pdf.Security;
using PdfSharp.Drawing;
// Create a new document
var document = new PdfSharp.Pdf.PdfDocument();
// Add a page and draw something on it
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 16, XFontStyleEx.Bold);
gfx.DrawString("Confidential Document", font, XBrushes.Black, new XPoint(50, 50));
// Set password protection
document.SecuritySettings.UserPassword = "password123";
document.SecuritySettings.OwnerPassword = "admin456";
// Save the encrypted PDF
document.Save("pdfsharp-encrypted.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.Security
Imports PdfSharp.Drawing
' Create a new document
Private document = New PdfSharp.Pdf.PdfDocument()
' Add a page and draw something on it
Private page = document.AddPage()
Private gfx = XGraphics.FromPdfPage(page)
Private font = New XFont("Arial", 16, XFontStyleEx.Bold)
gfx.DrawString("Confidential Document", font, XBrushes.Black, New XPoint(50, 50))
' Set password protection
document.SecuritySettings.UserPassword = "password123"
document.SecuritySettings.OwnerPassword = "admin456"
' Save the encrypted PDF
document.Save("pdfsharp-encrypted.pdf")
輸出

在此程式碼中,PDFsharp 只需將 使用者密碼套用至 PDF 文件以限制存取。
功能應用程式
IronPDF:全面 PDF 編輯、重製和轉換的最佳選擇。
IronPDF 在需要全面的 PDF 編輯、編輯以及處理多種檔案格式(如 DOCX 到 PDF 的轉換)的場景中大放異彩。 對於開發需要進階 PDF 操作的應用程式,並能輕鬆整合至業務工作流程的開發人員而言,這是最理想的選擇。
IronPDF 的實際使用案例:
- 重制敏感信息: IronPDF 在法律、醫療保健和政府等行業的使用非常有效,這些行業需要在 PDF 文件中移除或隱藏敏感信息(如個人資料、財務細節或醫療記錄)。 重編不僅涉及覆蓋文字,還要確保重編過的資訊無法復原。
- Automating PDF Creation from DOCX Files: IronPDF 使 Microsoft Word 文檔 (DOCX) 轉换為 PDF 变得簡单。 這在企業軟體或電子商務等環境中特別有用,因為在這些環境中,發票、報告或合約通常是由 Word 文件產生,需要轉換成安全的 PDF 以便歸檔或分發。
- 可編輯的 PDF 表單: 如果您正在建立可讓使用者輸入資料的互動式 PDF 表單(例如,應用程式、調查、回饋表單),IronPDF 可讓您建立並修改可填寫的表單。 此功能常用於人力資源、教育和客戶支援等產業。
示例用例 - 將 DOCX 轉換為 PDF 並添加水印: 在企業環境中,IronPDF 可用於將 Word 文件轉換為 PDF,然後為品牌或安全目的添加水印。 使用 IronPDF,您可以將水印套用至指定頁面,或套用至所有 PDF 頁面。
在此,我們將採用一個 DOCX 檔案範例,並套用水印將其標示為草稿,再加上公司品牌的範例。
using IronPdf;
using IronPdf.Editing;
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("document.docx");
var watermark = "<h1 style='Color: red; font-size: 60px'>DRAFT</h1><img style='width: 200px' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";
pdf.ApplyWatermark(watermark, opacity: 75, VerticalAlignment.Top, HorizontalAlignment.Right);
pdf.SaveAs("output.pdf");
using IronPdf;
using IronPdf.Editing;
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("document.docx");
var watermark = "<h1 style='Color: red; font-size: 60px'>DRAFT</h1><img style='width: 200px' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";
pdf.ApplyWatermark(watermark, opacity: 75, VerticalAlignment.Top, HorizontalAlignment.Right);
pdf.SaveAs("output.pdf");
Imports IronPdf
Imports IronPdf.Editing
Private renderer As New DocxToPdfRenderer()
Private pdf As PdfDocument = renderer.RenderDocxAsPdf("document.docx")
Private watermark = "<h1 style='Color: red; font-size: 60px'>DRAFT</h1><img style='width: 200px' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>"
pdf.ApplyWatermark(watermark, opacity:= 75, VerticalAlignment.Top, HorizontalAlignment.Right)
pdf.SaveAs("output.pdf")
輸出
。
在這個範例中,IronPDF 首先將 DOCX 文件轉換為 PDF,然後再加入 watermark 以確保文件標示為 "DRAFT"。 此功能對於需要散佈敏感資訊的公司非常有用,同時確保文件的安全性,或追蹤哪些文件是草稿。
PDFsharp。:非常適合輕量級 PDF 編輯和簡單文件產生。
PDFsharp 比較適合基本的 PDF 作業,需要輕量編輯和從頭建立文件。 它擅長的任務包括從頭開始建立簡單的 PDF、修改現有的 PDF,以及套用基本的編輯。 然而,當涉及到更複雜的操作時,例如進階節錄、文件轉換或處理表單資料,PDFsharp 的功能相較於 IronPDF 則較為有限。
PDFsharp 的實際使用案例:
- Generating Simple Reports and Documents: PDFsharp 非常適合建立簡單的靜態文件,例如報告、發票或小冊子。 它通常用於小型商業應用程式,在這些應用程式中,PDF 的產生不需要過於複雜,但需要包含文字、圖片和表格等基本格式。
- Basic PDF Editing and Merging:在需要將多個 PDF 合併為單一文件或將大型 PDF 分割為較小部分的情況下,PDFsharp 提供簡單的功能來處理這些工作。 它很適合需要輕量級操作而不需要進階 PDF 操作的文件管理系統。
- Basic Redaction: 雖然 PDFsharp 並不提供 IronPDF 般進階的編輯功能,但它可用於基本的編輯,例如用形狀或實色遮蓋文字或圖片。 這對於低安全性的文件或不需要遵守嚴格資料隱私權法律的應用程式來說就足夠了。
範例用例:在商業環境中,PDFsharp 可以用來從資料庫中擷取的資料產生報告、將多個檔案合併為一個檔案,或是編輯部分文件供內部使用。
PDFsharp 程式碼範例 - 合併兩個 PDF:
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
// Open the first PDF
PdfDocument document1 = PdfReader.Open("document1.pdf", PdfDocumentOpenMode.Import);
// Open the second PDF
PdfDocument document2 = PdfReader.Open("document2.pdf", PdfDocumentOpenMode.Import);
// Create a new PDF document for the merged file
PdfDocument outputDocument = new PdfDocument();
// Add pages from the first document
foreach (PdfPage page in document1.Pages)
{
outputDocument.AddPage(page);
}
// Add pages from the second document
foreach (PdfPage page in document2.Pages)
{
outputDocument.AddPage(page);
}
// Save the merged PDF
outputDocument.Save("merged-document.pdf");
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
// Open the first PDF
PdfDocument document1 = PdfReader.Open("document1.pdf", PdfDocumentOpenMode.Import);
// Open the second PDF
PdfDocument document2 = PdfReader.Open("document2.pdf", PdfDocumentOpenMode.Import);
// Create a new PDF document for the merged file
PdfDocument outputDocument = new PdfDocument();
// Add pages from the first document
foreach (PdfPage page in document1.Pages)
{
outputDocument.AddPage(page);
}
// Add pages from the second document
foreach (PdfPage page in document2.Pages)
{
outputDocument.AddPage(page);
}
// Save the merged PDF
outputDocument.Save("merged-document.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
' Open the first PDF
Private document1 As PdfDocument = PdfReader.Open("document1.pdf", PdfDocumentOpenMode.Import)
' Open the second PDF
Private document2 As PdfDocument = PdfReader.Open("document2.pdf", PdfDocumentOpenMode.Import)
' Create a new PDF document for the merged file
Private outputDocument As New PdfDocument()
' Add pages from the first document
For Each page As PdfPage In document1.Pages
outputDocument.AddPage(page)
Next page
' Add pages from the second document
For Each page As PdfPage In document2.Pages
outputDocument.AddPage(page)
Next page
' Save the merged PDF
outputDocument.Save("merged-document.pdf")
輸出
。
此程式碼示範如何使用 PDFsharp 將兩個 PDF 文件合併為一個。 這是需要將多個報告、發票或其他文件合併為單一 PDF 的應用程式的常見用例。
定價與授權
IronPDF 和 PDFsharp 提供不同的定價和授權方式,滿足各種需求。 IronPDF 提供 永久授權模式,而 PDFsharp 則是 開放原始碼且免費使用。
IronPDF 定價與授權:
IronPDF 採用 永久授權模式,意即使用者可終生使用,並享有 每年更新。 它根據使用情況提供 分層定價,包括個人、團隊或企業 License。 提供免費試用,允許使用者在購買前測試工具。 商業授權使用者還可獲得 技術支援和功能更新。
*永久許可:終身存取權限,包括年度更新*。 分級許可:**個人、團隊或企業許可。 *免費試用:*用戶可以在購買前試用。 商業支援:獲得支援**和定期更新。
PDFsharp 授權:
PDFsharp 在 MIT License 下是免費的,這意味著它是 開放原始碼,無論是個人或商業使用都是免費的。 但是,它不提供 高級支援,讓使用者只能依賴 社群論壇來尋求協助。 這適合小型或開放原始碼專案,但對於較複雜的需求,可能需要外部協助。
- MIT許可證:可免費用於商業用途。 *不提供高級支援:可透過社群論壇*尋求協助。 靈活性:非常適合小型專案**。
終端使用者支援與文件
IronPDF 在開發人員支援方面表現優異,提供詳細的說明文件和多種協助渠道。 與 PDFsharp 不同,IronPDF 提供結構化的資源,在整個整合過程中幫助開發人員。
IronPDF 開發人員支援:
IronPDF 提供全面的文件,包括分步指南、API 引用和代碼示例。 專門的支援團隊可提供技術上的諮詢。 此外,線上知識庫提供疑難排解解決方案和有用的提示。 對於企業客戶,優先支援可確保更快速地解決問題。
*全面的文件:詳細的指南和程式碼範例*。 支援團隊:直接獲得技術支援。 知識庫:**常見問題**與故障排除技巧。 優先支援:為企業快速解決問題**。
參與社群方面:
IronPDF 也培養了一個活躍的開發者社群。 使用者可以參與社群論壇,提出問題、分享經驗並尋找解決方案。 此外,教學和視訊指南可幫助使用者快速上手。 IronPDF 也出現在 GitHub 上,允許社群回報錯誤並建議功能。
*社群論壇:一個提出問題和分享解決方案*的空間。 教學與指南:**逐步說明和影片資源。
- GitHub 協作:使用者可以貢獻程式碼並提出新功能建議。
PDFsharp 開發人員支援:
PDFsharp 的支援主要來自其 GitHub 儲存庫和 Stack Overflow 等社群論壇。 開發人員可以報告問題、瀏覽解決方案或提出問題。 然而,文件有限,詳細的指南或官方教學也很少,開發人員需要依賴社群貢獻或試誤法。
- GitHub 倉庫:回報問題並探索解決方案。 *社群論壇:在Stack Overflow*尋求協助。 文檔匱乏:**指南和資源有限。
社區驅動的貢獻:
由於缺乏官方支援,因此開發人員可以透過 GitHub 為 PDFsharp 做出貢獻,改善文件或新增功能。 雖然有使用者貢獻的教學,但由於沒有專門的支援團隊,因此故障排除可能需要較長的時間。
*開源貢獻:使用者可以改進文件*。 協作式故障排除:依靠社群**的幫助。
授權、文件與支援摘要
。
結論
在 IronPDF 和 PDFsharp 之間作出選擇時,主要取決於您的專案需求。 IronPDF 是一款功能強大、特性豐富的解決方案,具有將 HTML 轉換為 PDF、穩健的加密功能以及強大的跨平台支援等先進功能。 它非常適合需要高品質 PDF 操作、安全功能和專屬支援的企業、企業和開發人員。
另一方面,PDFsharp 對於不需要進階功能的簡單專案來說,是一個可靠的開放原始碼選擇,在基本的 PDF 建立與編輯上提供易用性與彈性。
如果您正在尋找功能廣泛的多合一 PDF 解決方案,我們鼓勵您探索 IronPDF。 IronPDF 具備商業支援和持續更新功能,可依據您的開發需求進行擴充,確保您的 PDF 檔能夠有效且安全地處理。 立即試用 IronPDF 並發現它如何簡化您的 PDF 工作流程!
常見問題解答
怎樣在 C# 中將 HTML 轉換為 PDF?
您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字符串轉換為 PDF。您還可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。
IronPDF 和 PDFsharp 之間的主要區別是什麼?
IronPDF 是一個功能全面的函式庫,具有高級功能,如 HTML 到 PDF 的轉換、PDF 編輯和安全功能。PDFsharp 更為基本,集中於 PDF 的創建和編輯,缺乏本地的 HTML 到 PDF 轉換功能。
IronPDF 適合跨平台的應用嗎?
是的,IronPDF 支援 Windows、macOS 和 Linux,並與 .NET Core 和 .NET Framework 相容,使其成為跨平台應用程式的理想選擇。
IronPDF 提供哪些安全功能?
IronPDF 提供高級安全功能,包括數字簽名和 256 位加密,適合需要強大文件保護的應用程式。
PDFsharp 能否處理複雜的網頁轉換為 PDF?
否,PDFsharp 本身不支援 HTML 到 PDF 的轉換。它需要第三方工具,例如 Polybioz.HtmlRenderer.PdfSharp.Core,來呈現 HTML 內容。
IronPDF 如何支援開發者?
IronPDF 提供完整的文件、專門的支援團隊和活躍的社區論壇,提供豐富的資源給開發者。
IronPDF 的許可選項有哪些?
IronPDF 使用永久授權模式,提供分層定價、免費試用、年度更新和商業支援。
PDFsharp 是免費的嗎?
是的,PDFsharp 是免費的,並在 MIT 許可下開放源碼,適用於個人和商業用途。
哪一個函式庫在處理 PDF 加密方面提供更好的支援?
IronPDF 提供高級的加密功能,包括 256 位加密和數字簽名,使其適合安全的 PDF 處理。
在 .NET 中進行複雜的 PDF 操作哪個函式庫最適合?
IronPDF 更適合複雜的 PDF 任務,提供高級功能,如 HTML 到 PDF 的轉換、編輯和文件安全。

