在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
對於從事 PDF 工作的開發人員來說,擁有一個可靠的 PDF 生成和操作庫是必不可少的。 在 .NET 生態系統中,有許多 C# PDF 庫可供選擇,那么你該如何選擇最適合你需求的呢?
在處理 .NET 應用程式中的 PDF 功能時,選擇適當的函式庫對於高效開發至關重要。 本文詳細比較了兩個著名的 C# PDF 庫:IronPDF和iText 7 (前稱為iTextSharp). 我們將探索其功能、性能、授權方式及適合各種專案需求的情況,以幫助您做出明智的決定。
PDF 在 報告、發票和法律文件 中被廣泛使用,使 PDF 生成和操作對許多應用程式來說成為必需的。 在選擇庫時,需要考慮的關鍵因素包括:
IronPDF 是專門針對 .NET 開發人員設計的商業 PDF 庫。 它簡化了 PDF 的生成、操作和轉換,使其成為 C# 應用程式中最易於使用的函式庫之一。
IronPDF 支援 .NET Core、.NET Framework 和 .NET Standard,確保在各種 .NET 環境中的相容性。 其高水平的跨平台兼容性使其成為在不同應用環境中工作的團隊的理想選擇,並且能夠無縫整合進如 Visual Studio 等開發環境中。 除了其 .NET 版本外,IronPDF 也適用於 Java、Python 和 Node.js。
🔹 主要功能:
表單填寫和數據提取 – 填充互動式PDF 表單以程式方式提取表單資料。
📌 最佳適合對象: 尋求簡單直接的整合解決方案、無需額外附加元件或複雜授權的開發人員。
iText 7 是一個強大且靈活的 PDF 庫,提供了廣泛的 PDF 操作能力,包括文件創建、加密和簽署。 然而,它的核心庫並不原生支持將 HTML 轉換為 PDF。
🔹 主要功能:
PDF 表單管理: 建立和編輯 AcroForms 和 XFA 表單以製作互動式 PDF 表單。
📌 最適合:需要高度自定義 PDF 解決方案並願意購買額外附加元件以擴展功能的開發人員。
在我們討論功能及其相關的代碼範例之前,讓我們先來看看IronPDF和iText 7之間最大的功能差異之一,HTML轉PDF。
另一方面,iText 7需要pdfHTML 附加组件,这是需付费的商业授权功能。 這增加了需要網頁轉 PDF 功能的開發人員的成本。
HTML 到 PDF 比較流程圖
📌 底線: 如果您需要HTML 到 PDF 的轉換,IronPDF 是更具成本效益的解決方案,因為它包含此功能現成可用。
IronPDF 拥有丰富的功能集,用于处理 PDF 文件。 這些功能包括 PDF 的創建、PDF 操作以及安全性。 為了更清楚地了解這個庫所提供的廣泛功能,我們將看看一些關鍵的功能。
轉換 HTML 內容使用 IronPDF 強大的渲染引擎將內容轉換為高質量的 PDF 文件。IronPDF 的渲染器不僅僅是將 HTML 內容進行轉換,還能夠保持所有原始的 CSS 樣式和 JavaScript 的互動性。
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("HtmlToPdf.pdf");
}
}
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("HtmlToPdf.pdf");
}
}
輸入 HTML
輸入 HTML 內容
輸出 PDF
使用 IronPDF 將 HTML 轉換為 PDF 的輸出
在此程式碼範例中,我們首先建立了一個新的 ChromePdfRenderer
實例,這使我們能夠使用強大的渲染引擎 IronPDF 將 HTML 渲染為 PDF。 然後,我們將 HTML 文件傳遞給 RenderHtmlFileAsPdf
()方法,進而將 HTML 渲染為 PDF,並儲存在 PdfDocument
對象中。 最後,我們將 PDF 保存到指定的檔案位置。
對於希望將 URL 內容轉換為 PDF 的開發人員,IronPDF 是您的最佳選擇。 通過使用此庫,您將能夠使用ChromePdfRenderer渲染引擎來創建像素完美的PDF文件,該引擎在渲染時將保留所有原始樣式和佈局。URL轉PDF. 在這個例子中,我們將使用這個網址,以展示 IronPDF 如何處理更複雜的 CSS 樣式。
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("UrlToPdf.pdf");
}
}
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("UrlToPdf.pdf");
}
}
PDF 輸出:
使用 IronPDF 將 URL 輸出為 PDF
與我們的 HTML 到 PDF 示例非常相似,使用 IronPDF 將任何 URL 轉換為 PDF 的第一步是先創建一個新的 ChromePdfRenderer
實例。 一旦該方法使用 RenderUrlAsPdf
將 URL 內容轉換為 PDF 格式後,會將生成的 PDF 保存到一個新的 PdfDocument
對象中,然後我們使用 SaveAs
方法來保存該 PDF。
通過應用以確保 PDF 文件的真實性數位簽名到您的 PDF 文件中。 開發人員可能考慮使用數位簽章的不同方法,例如通過安全憑證對 PDF 進行數位簽名、將手寫簽名的圖片添加到 PDF,或將憑證的圖片蓋印到 PDF 本身。
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
using IronSoftware.Drawing;
public class Program
{
static void Main(string[] args)
{
// Create PdfSignature object
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);
var sig = new PdfSignature(cert);
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 350, 250));
// Sign and save PDF document
sig.SignPdfFile("product_report.pdf");
}
}
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
using IronSoftware.Drawing;
public class Program
{
static void Main(string[] args)
{
// Create PdfSignature object
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);
var sig = new PdfSignature(cert);
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 350, 250));
// Sign and save PDF document
sig.SignPdfFile("product_report.pdf");
}
}
輸出 PDF
使用 IronPDF 的數位簽章輸出
在此範例中,我們已加載證書對象,創建了簽名的視覺表現或在我們的情況下為IronPDF圖像,並創建了新的PdfSignature
對象,該對象負責PDF文件本身的簽名。 最後,我們使用 SignPdfFile
簽署並保存了我們的 PDF 文件。
如果您想了解 IronPDF 提供的更多功能,務必查看其資訊豐富的內容功能頁面,或該使用指南包含每個功能的深入代碼範例。
iText7 提供廣泛的功能來自訂和增強您的 PDF 文件。 這個 PDF 庫支援各種 PDF 標準和進階的 PDF 操作功能,內容非常豐富。 然而,如前所述,iText7可能需要額外的套件才能執行某些與PDF相關的任務,例如HTML轉換為PDF。
雖然iText7本身無法處理HTML到PDF的轉換,我們可以利用pdfHTML,一個在iText7商業許可下的付費插件,將我們在IronPDF範例中使用的HTML文件轉換成PDF文檔。
using iText.Html2pdf;
public class Program
{
static void Main(string[] args)
{
using (FileStream htmlSource = File.Open("example.html", FileMode.Open))
using (FileStream pdf = File.Open("HtmlToPdfOutput.pdf", FileMode.Create))
{
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties);
pdf.Close();
}
}
}
using iText.Html2pdf;
public class Program
{
static void Main(string[] args)
{
using (FileStream htmlSource = File.Open("example.html", FileMode.Open))
using (FileStream pdf = File.Open("HtmlToPdfOutput.pdf", FileMode.Create))
{
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties);
pdf.Close();
}
}
}
輸出 PDF
iText7 HTML 轉換為 PDF 輸出
在此範例中,我們載入了 HTML 文件,並指定了儲存已渲染 PDF 的檔案位置。 然後,使用ConvertToPdf
方法,我們可以輕鬆地將HTML檔案轉換為PDF文件。
現在,是時候比較一下 iText7 在將 URL 轉換為 PDF 時的表現與 IronPDF 的比較。 為了達成此目標,我們將使用與之前完全相同的 URL,以確保公平比較。
using System;
using System.Net.Http;
using System.IO;
using iText.Html2pdf;
public class Program
{
public static async System.Threading.Tasks.Task Main(string[] args)
{
string url = "https://www.apple.com"; // Replace with your target URL
string outputPdfPath = "output.pdf";
try
{
// Download HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Convert HTML to PDF
using (FileStream pdfStream = new FileStream(outputPdfPath, FileMode.Create))
{
ConverterProperties properties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties);
}
}
Console.WriteLine("PDF created successfully: " + outputPdfPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
using System;
using System.Net.Http;
using System.IO;
using iText.Html2pdf;
public class Program
{
public static async System.Threading.Tasks.Task Main(string[] args)
{
string url = "https://www.apple.com"; // Replace with your target URL
string outputPdfPath = "output.pdf";
try
{
// Download HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Convert HTML to PDF
using (FileStream pdfStream = new FileStream(outputPdfPath, FileMode.Create))
{
ConverterProperties properties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties);
}
}
Console.WriteLine("PDF created successfully: " + outputPdfPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
輸出 PDF
iText7 URL 轉換為 PDF 輸出
如這裡所見,iText7 將 URL 轉換為 PDF 的方法更為手動且複雜。 首先,我們需要從 URL 下載 HTML 內容,然後按照在 HTML 轉 PDF 範例中看到的類似步驟,將我們的 URL 內容渲染成 PDF 文件並儲存。 如您在輸出圖像中所見,iText7無法保持大部分原始樣式和佈局,而IronPDF則不同。
using System.Security.Cryptography.X509Certificates;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Bouncycastle.Crypto;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto;
using iText.Bouncycastle.X509;
using iText.Kernel.Crypto;
public partial class Program
{
static void Main(string[] args)
{
string inputPdf = "input.pdf"; // PDF to be signed
string outputPdf = "signed_output.pdf"; // Signed PDF output
string pfxFile = "IronSoftware.pfx"; // Path to your PFX certificate
string password = "Passw0rd"; // Password for PFX file
try
{
// Load your certificate
Pkcs12Store ks = new Pkcs12StoreBuilder().Build();
using (FileStream fs = new FileStream(pfxFile, FileMode.Open, FileAccess.Read))
{
ks.Load(fs, password.ToCharArray());
}
string? alias = null;
foreach (string al in ks.Aliases)
{
if (ks.IsKeyEntry(al))
{
alias = al;
break;
}
}
if (alias == null)
{
throw new Exception("Alias not found in the PFX file.");
}
ICipherParameters pk = ks.GetKey(alias).Key;
X509CertificateEntry[] chain = ks.GetCertificateChain(alias);
X509Certificate2 cert = new X509Certificate2(chain[0].Certificate.GetEncoded());
// Convert BouncyCastle certificate to iText certificate
var itextCertChain = new IX509Certificate[chain.Length];
for (int i = 0; i < chain.Length; i++)
{
itextCertChain[i] = new X509CertificateBC(chain[i].Certificate);
}
// Create output PDF with signed content
using (PdfReader reader = new PdfReader(inputPdf))
using (FileStream os = new FileStream(outputPdf, FileMode.Create, FileAccess.Write))
{
PdfSigner signer = new PdfSigner(reader, os, new StampingProperties().UseAppendMode());
// Set up the external signature (private key + digest algorithm)
IPrivateKey iTextPrivateKey = new PrivateKeyBC(pk);
IExternalSignature pks = new PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256);
IExternalDigest digest = new BouncyCastleDigest();
// Perform the signing (detached signature)
signer.SignDetached(digest, pks, itextCertChain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
}
Console.WriteLine($"PDF digitally signed successfully: {outputPdf}");
}
catch (Exception ex)
{
Console.WriteLine($"Error signing PDF: {ex.Message}");
}
}
}
using System.Security.Cryptography.X509Certificates;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Bouncycastle.Crypto;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto;
using iText.Bouncycastle.X509;
using iText.Kernel.Crypto;
public partial class Program
{
static void Main(string[] args)
{
string inputPdf = "input.pdf"; // PDF to be signed
string outputPdf = "signed_output.pdf"; // Signed PDF output
string pfxFile = "IronSoftware.pfx"; // Path to your PFX certificate
string password = "Passw0rd"; // Password for PFX file
try
{
// Load your certificate
Pkcs12Store ks = new Pkcs12StoreBuilder().Build();
using (FileStream fs = new FileStream(pfxFile, FileMode.Open, FileAccess.Read))
{
ks.Load(fs, password.ToCharArray());
}
string? alias = null;
foreach (string al in ks.Aliases)
{
if (ks.IsKeyEntry(al))
{
alias = al;
break;
}
}
if (alias == null)
{
throw new Exception("Alias not found in the PFX file.");
}
ICipherParameters pk = ks.GetKey(alias).Key;
X509CertificateEntry[] chain = ks.GetCertificateChain(alias);
X509Certificate2 cert = new X509Certificate2(chain[0].Certificate.GetEncoded());
// Convert BouncyCastle certificate to iText certificate
var itextCertChain = new IX509Certificate[chain.Length];
for (int i = 0; i < chain.Length; i++)
{
itextCertChain[i] = new X509CertificateBC(chain[i].Certificate);
}
// Create output PDF with signed content
using (PdfReader reader = new PdfReader(inputPdf))
using (FileStream os = new FileStream(outputPdf, FileMode.Create, FileAccess.Write))
{
PdfSigner signer = new PdfSigner(reader, os, new StampingProperties().UseAppendMode());
// Set up the external signature (private key + digest algorithm)
IPrivateKey iTextPrivateKey = new PrivateKeyBC(pk);
IExternalSignature pks = new PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256);
IExternalDigest digest = new BouncyCastleDigest();
// Perform the signing (detached signature)
signer.SignDetached(digest, pks, itextCertChain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
}
Console.WriteLine($"PDF digitally signed successfully: {outputPdf}");
}
catch (Exception ex)
{
Console.WriteLine($"Error signing PDF: {ex.Message}");
}
}
}
輸出 PDF
iText7 數位簽章輸出
如您所見,雖然 iText7 能夠數位簽署 PDF 文件,但過程通常比使用 IronPDF 複雜得多。 此程式碼載入 PFX 憑證並使用它來數位簽署 PDF。 它提取私鑰和證書,設置簽名者,並將分離的簽名添加到 PDF,然後保存已簽名的文件。
IronPDF 因其易用性和高效性能的優化而聞名,提供更簡單的 API,簡化了常見任務,如 HTML 到 PDF 的轉換。 它提供高階方法以抽象化複雜的 PDF 處理任務,使開發人員能夠以最少的代碼生成、編輯和操作 PDF。 對於多線程或大規模的文件處理,IronPDF 支持並行執行。
另一方面,iText 7 提供更詳細和細緻的控制級別,這對於需要廣泛自定義的開發人員來說是有利的。 它提供了一個強大的 API 用於處理低層次的 PDF 操作。然而,由於其複雜性,iText 7 通常需要更多的代碼才能達到與 IronPDF 類似的結果。
在為 .NET 專案選擇 PDF 程式庫時,授權及成本考量是至關重要的。 IronPDF 和 iText 7 遵循不同的許可模式,選擇合適的一個取決於您的專案需求、預算和合規性需求。
IronPDF 採用商業授權模式,這意味著雖然它提供商業授權的免費試用,並且供開發和評估免費使用,但在完整生產使用時需要付費授權。定價透明,取決於使用規模、開發人數和項目類型等因素。
🔹 IronPDF 授權:
✅ 適合需要 簡單且具成本效益的 PDF 解決方案的企業。
📌 底線: IronPDF 包含所有主要功能於單一授權中,是團隊和企業具成本效益的選擇。
iText7 運行在雙重授權模式下,其中包括:
AGPL(GNU Affero 通用公共许可证)** – 對於開源項目是免費的,但要求使用 iText7 的整個項目必須是開源的並符合 AGPL。 這意味著對專案所做的任何修改或新增內容也必須公開分享。
商業授權—對於任何拒絕公開源代碼的專有軟體或商業應用程式來說,這是必需的。 定價結構會根據使用量、支援等級和部署規模而有所不同。
對於希望將iText 7整合到專有軟體中的公司,商業授權是必須的。 成本可能很高,特別是針對企業級解決方案,因為它是按每位開發人員定價的,但它提供專業支援並確保法律合規。
🔹 為什麼 iText 7 可能更昂貴:
pdfHTML(付費)– 必需用于 HTML-to-PDF 轉換。
pdfOCR(付費)– 需要用於從圖像中識別文字。
pdfCalligraph(付費)- 改進文本渲染和字體支援。
📌 重點: 如果您的團隊需要多位開發人員以及像 HTML-to-PDF 和 OCR 這樣的關鍵功能,iText 7 可能會比 IronPDF 昂貴得多,而 IronPDF 提供這些功能而無需額外費用。
小型專案與企業專案
對於需要快速實現 PDF 功能且配置要求最小的小型到中型專案來說,IronPDF 是一個具有吸引力的選擇,因為它具有使用者友好的 API 和全面的功能集。 企業級項目需要廣泛的定制化以及遵循特定的 PDF 標準,這可能會從 iText7 的高級功能中受益。然而,由於其高性能、許可選項和可用性,IronPDF 也是這一水平工作的有力競爭者。
學術和商業用途
在學術環境或開源專案中,iText 7 的 AGPL 授權允許免費使用,前提是專案的授權相容。 對於商業應用程式,IronPDF 和 iText 7 都需要購買商業授權。 建議檢閱每個函式庫的許可條款,以確保符合您的專案目標。
選擇適合您的 .NET 專案的 PDF 函式庫是一個關鍵決策,這取決於使用的便利性、功能集及授權需求等因素。 IronPDF 和 iText 7 都提供強大的 PDF 功能,但它們滿足不同的需求。 除了這兩個庫之外,Aspose、Syncfusion 和 PDFSharp 等競爭對手都提供具有競爭力的 .NET PDF 庫。 然而,IronPDF 憑藉其易於使用的 API、全面的功能集和高成本效益,在 PDF 行業中始終處於領先地位。
以下是我們對為什麼 IronPDF 是滿足您所有 .NET PDF 函式庫需求的絕佳選擇所做的引人注目的總結。
IronPDF 與 iText7 比較總結
如果您的專案需要快速生成 PDF,特別是從網頁內容生成,並且您正在尋找一個易於使用的解決方案,IronPDF可能是更好的選擇。然而,如果您的應用需要高級 PDF 操作或嚴格符合 PDF 標準,並且您需要靈活地進行大規模自定義,iText7可能更合適。 考慮您的專案具體需求和許可限制,以確定最適合您需求的庫。
🎯 立即试用 IronPDF: 下載免費試用開始探索 IronPDF 強大功能!
立即在您的專案中使用IronPDF,並享受免費試用。