在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 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 輸出:
URL 到 PDF 輸出使用 IronPDF
正如我們的 HTML 到 PDF 範例中所示,使用 IronPDF 將任何 URL 轉換為 PDF 的第一步是首先創建一個新的 ChromePdfRenderer
實例。 一旦方法已將 URL 內容轉換為 PDF 格式,使用 RenderUrlAsPdf
,它會將生成的 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,並享受免費試用。