iTextSharp 和 IronPDF 在 PDF 編輯的比較
PDF(便攜式文件格式)是一種廣泛使用的文件格式,因其能夠保持文件格式、安全性和便攜性而廣受歡迎。
PDF 文件已成為世界上使用最廣泛的文件格式之一,並且有多個庫可用於在 C# 語言中建立和操作 PDF。
了解如何使用 IronPDF 和 iTextSharp 透過 C# 編輯 PDF 文件,利用這些強大的庫使這項任務變得簡單。
在本文中,我們將比較 C# 中兩個流行的 PDF 處理庫:iTextSharp 和 IronPDF。 我們將討論如何使用這兩個庫編輯 PDF 文件,然後我們將探討 IronPDF 與 iTextSharp 相比有哪些優勢,尤其是在輸出列印、效能和價格方面。
iTextSharp DLL 與 IronPDF 庫簡介
iTextSharp 和IronPDF 的功能和試用資訊可以幫助開發人員在 C# 中有效地處理 PDF 文件。 這兩個庫都提供了建立、編輯和操作 PDF 文件的各種特性和功能。
iTextSharp DLL 是基於 Java 的 iText 函式庫的 C# 移植版。 它提供了一個簡單易用的 API,用於建立和操作 PDF 文件。 iTextSharp 是一個開源函式庫,採用 AGPL 授權。
IronPDF 是一個 .NET 函式庫,旨在以 C# 建立、編輯和操作 PDF 檔案。 它提供了一個現代化且直觀的 API,用於處理 PDF 文件。 IronPDF 是一個商業庫,提供免費試用版和訂閱選項,以滿足更廣泛的需求。
iTextSharp 和 IronPDF 庫的比較
iTextSharp 和 IronPDF 庫都提供了廣泛的功能和功能,用於建立、編輯和操作 PDF 文件。 然而,IronPDF 比 iTextSharp 有幾個優勢,這使得它成為在 C# 中處理 PDF 文件的首選。
使用 iTextSharp 和 IronPDF 編輯 PDF 文件
現在我們已經討論了 iTextSharp 和 IronPDF 之間的區別,接下來讓我們看看如何使用這兩個庫來編輯 PDF 文件。 我們將透過範例示範如何使用 iTextSharp 和 IronPDF 在現有 PDF 文件中新增文字、表單欄位和填寫表單。
使用 iTextSharp 編輯 PDF 文件
先決條件
開始之前,您需要準備以下物品:
- 您的電腦上已安裝 Visual Studio。
- 具備 C# 程式語言的基礎知識。
- 您的專案中已安裝 iTextSharp 庫。
! iTextSharp 和 IronPDF 在編輯 PDF 方面的比較:圖 1 - 使用 C# 中的 iTextSharp 建立 PDF。
要將 iTextSharp 庫安裝到您的專案中,您可以使用 NuGet 套件管理器。 開啟 Visual Studio 項目,然後在解決方案資源管理器中以滑鼠右鍵按一下專案名稱。 從上下文選單中選擇"管理 NuGet 套件"。 在 NuGet 套件管理員中,搜尋"iTextSharp",並安裝該套件的最新版本。
iTextSharp 和 IronPDF 在 PDF 編輯方面的比較:圖 2 - 探索如何在 ASP.NET C# 中使用 iTextSharp
建立新的 PDF 文件
要使用 iTextSharp 建立一個新的 PDF 文件,我們需要建立一個"Document"類別的新實例,並將一個新的FileStream物件傳遞給它的建構子。 舉個例子:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
// Create a new PDF document
using (var writer = new PdfWriter(new FileStream("newfile.pdf", FileMode.Create)))
{
using (var pdf = new PdfDocument(writer))
{
var document = new Document(pdf);
// Create a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
// Add the header to the document
document.Add(header);
// Loop through pages and align header text
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add the header text to each page
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
}
}using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
// Create a new PDF document
using (var writer = new PdfWriter(new FileStream("newfile.pdf", FileMode.Create)))
{
using (var pdf = new PdfDocument(writer))
{
var document = new Document(pdf);
// Create a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
// Add the header to the document
document.Add(header);
// Loop through pages and align header text
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add the header text to each page
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
}
}在上面的程式碼中,我們建立了一個名為"newfile.pdf"的新PDF文件,並在其中添加了一個段落標題。
iTextSharp 和 IronPDF 在 PDF 編輯方面的比較:圖 3 - iTextSharp C# PDF 建立教學課程
編輯現有PDF文件
要使用 iTextSharp 編輯現有的 PDF 文件,您需要一個PdfReader物件來讀取現有的 PDF 文檔,還需要一個PdfStamper物件來修改它。 舉個例子:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Html2pdf;
using System.IO;
/**
* iText URL to PDF
* anchor-itext-url-to-pdf
**/
private void ExistingWebURL()
{
// Initialize PDF writer
PdfWriter writer = new PdfWriter("wikipedia.pdf");
// Initialize PDF document
using PdfDocument pdf = new PdfDocument(writer);
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format");
// Convert HTML to PDF
Document document = HtmlConverter.ConvertToDocument(
new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);
// Create and add a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
document.Add(header);
// Align header text for each page
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add header text aligned at the top
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the top and bottom margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
document.Close();
}using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Html2pdf;
using System.IO;
/**
* iText URL to PDF
* anchor-itext-url-to-pdf
**/
private void ExistingWebURL()
{
// Initialize PDF writer
PdfWriter writer = new PdfWriter("wikipedia.pdf");
// Initialize PDF document
using PdfDocument pdf = new PdfDocument(writer);
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format");
// Convert HTML to PDF
Document document = HtmlConverter.ConvertToDocument(
new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);
// Create and add a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
document.Add(header);
// Align header text for each page
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add header text aligned at the top
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the top and bottom margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
document.Close();
}在這段程式碼中,會開啟一個現有的 PDF 文件,並為其頁面添加標題,同時保持文字對齊方式正確。
使用 IronPDF 編輯 PDF 文件
IronPDF 是一個功能強大的 C# PDF 庫,可以輕鬆編輯 PDF 文件。 本教學將逐步介紹如何使用 IronPDF 編輯現有 PDF 文件,包括建立新的 PDF 文件、新增頁面、合併 PDF 等。
iTextSharp 和 IronPDF 在 PDF 編輯方面的比較:圖 4 - IronPDF 功能概述
先決條件
請確保您已具備:
- Visual Studio IDE IronPDF庫
步驟 1:建立新項目
在 Visual Studio 中建立一個新的 C# 專案。 選擇"控制台應用程式"項目類型。
步驟 2:安裝 IronPDF
iTextSharp 和 IronPDF 在 PDF 編輯方面的比較:圖 5 - 安裝 IronPDF NuGet 套件
使用 NuGet 套件管理器將 IronPDF 庫安裝到您的專案中。
// Execute this command in the Package Manager Console
Install-Package IronPdf// Execute this command in the Package Manager Console
Install-Package IronPdf步驟 3:載入現有 PDF 文檔
使用PdfDocument類別載入現有 PDF 文件:
using IronPdf;
// Path to an existing PDF file
var existingPdf = @"C:\path\to\existing\pdf\document.pdf";
// Load the PDF document
var pdfDoc = PdfDocument.FromFile(existingPdf);using IronPdf;
// Path to an existing PDF file
var existingPdf = @"C:\path\to\existing\pdf\document.pdf";
// Load the PDF document
var pdfDoc = PdfDocument.FromFile(existingPdf);iTextSharp 和 IronPDF 在 PDF 編輯方面的比較:圖 6 - 使用 IronPDF 建立 PDF
步驟 4:為現有 PDF 文件新增頁面
新增頁面:
// Add a new page with default size
var newPage = pdfDoc.AddPage();
newPage.Size = PageSize.Letter;// Add a new page with default size
var newPage = pdfDoc.AddPage();
newPage.Size = PageSize.Letter;步驟五:從網站建立 PDF
直接從網頁URL產生PDF文件。 舉個例子:
using IronPdf;
/**
* IronPDF URL to PDF
* anchor-ironpdf-website-to-pdf
**/
private void ExistingWebURL()
{
// Create PDF from a webpage
var Renderer = new IronPdf.ChromePdfRenderer();
// Set rendering options
Renderer.RenderingOptions.MarginTop = 50; // millimeters
Renderer.RenderingOptions.MarginBottom = 50;
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
Renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
CenterText = "{pdf-title}",
DrawDividerLine = true,
FontSize = 16
};
Renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
DrawDividerLine = true,
FontSize = 14
};
Renderer.RenderingOptions.EnableJavaScript = true;
Renderer.RenderingOptions.RenderDelay = 500; // milliseconds
// Render URL as PDF
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}using IronPdf;
/**
* IronPDF URL to PDF
* anchor-ironpdf-website-to-pdf
**/
private void ExistingWebURL()
{
// Create PDF from a webpage
var Renderer = new IronPdf.ChromePdfRenderer();
// Set rendering options
Renderer.RenderingOptions.MarginTop = 50; // millimeters
Renderer.RenderingOptions.MarginBottom = 50;
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
Renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
CenterText = "{pdf-title}",
DrawDividerLine = true,
FontSize = 16
};
Renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
DrawDividerLine = true,
FontSize = 14
};
Renderer.RenderingOptions.EnableJavaScript = true;
Renderer.RenderingOptions.RenderDelay = 500; // milliseconds
// Render URL as PDF
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}iTextSharp 和 IronPDF 的區別
iTextSharp 和 IronPDF 在 PDF 編輯的比較:圖 7 - iTextSharp 和 IronPDF 的選擇
iTextSharp 是一個流行的開源程式庫,用於在 C# 中建立、操作和提取 PDF 文件中的資料。 它有據可查,並被廣泛應用。 而 IronPDF 則更加現代化,它具有更多功能和優勢,使其成為開發人員的更好選擇。
從 HTML 輸入字串產生 PDF
以下是如何使用 IronPDF 從 HTML 建立 PDF 的方法:
using IronPdf;
/**
* IronPDF HTML to PDF
* anchor-ironpdf-document-from-html
**/
private void HTMLString()
{
// Render HTML to PDF
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Renderer.RenderingOptions.TextFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"
};
var OutputPath = "ChromeHtmlToPdf.pdf";
PDF.SaveAs(OutputPath);
}using IronPdf;
/**
* IronPDF HTML to PDF
* anchor-ironpdf-document-from-html
**/
private void HTMLString()
{
// Render HTML to PDF
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Renderer.RenderingOptions.TextFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"
};
var OutputPath = "ChromeHtmlToPdf.pdf";
PDF.SaveAs(OutputPath);
}iText 7 HTML 轉 PDF
使用 iText 7 將 HTML 文字轉換為 PDF:
using iText.Html2pdf;
using System.IO;
/**
* iText HTML to PDF
* anchor-itext-html-to-pdf
**/
private void HTMLString()
{
HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", new FileStream("iText7HtmlToPdf.pdf", FileMode.Create));
}using iText.Html2pdf;
using System.IO;
/**
* iText HTML to PDF
* anchor-itext-html-to-pdf
**/
private void HTMLString()
{
HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", new FileStream("iText7HtmlToPdf.pdf", FileMode.Create));
}表現
IronPDF 的設計目標是比 iTextSharp 更快、更有效率,因此能夠使用更少的資源更快地產生 PDF 檔案。 這種效率對於大型或複雜的文件至關重要。
定價
iTextSharp 的某些使用場景需要商業許可,費用可能較高。而 IronPDF 則提供更經濟實惠的定價模式,並提供多種選項以滿足不同的需求和預算。
授權和定價
iTextSharp 和 IronPDF 的主要區別之一在於它們的許可和定價模式。
- iTextSharp:根據 AGPL 授權協議授權,非開源專案需要商業許可。 商業許可證的費用各不相同。
- IronPDF:提供免費試用版和靈活的許可方式,包括開發者許可和伺服器許可,使其適合商業用途。
結論
總而言之,雖然 iTextSharp 和 IronPDF 都能在 C# 中處理 PDF 文件,但 IronPDF 憑藉其更全面的功能和更高的效率脫穎而出。它提供了更高級的功能、更直覺的 API 和更優異的效能。 其靈活的定價方式使其適用於商業項目和大型組織。
IronPDF 具有卓越的 HTML 轉 PDF 功能,開發人員可以輕鬆產生包含豐富媒體或互動式內容的報告或文件。 IronPDF 價格實惠,對於需要功能強大且高效的 C# 專案 PDF 庫的開發人員來說,它是一個絕佳的選擇。
常見問題解答
如何在C#中編輯PDF檔案而不遺失格式?
您可以使用 IronPDF 在 C# 中編輯 PDF 文件,並確保格式保持不變。 IronPDF 提供進階功能和現代化的 API,可實現高效的 PDF 操作。
在 Visual Studio 中安裝 PDF 程式庫需要哪些步驟?
若要在 Visual Studio 中安裝 IronPDF 等 PDF 庫,請開啟 NuGet 套件管理器,搜尋 IronPDF,然後將該套件安裝到您的專案中。
如何在C#中將網頁URL轉換為PDF?
IronPDF 讓您可以使用ChromePdfRenderer類別將網頁 URL 轉換為 PDF,從而確保高品質的輸出。
iTextSharp 和 IronPDF 在許可證上有什麼不同?
iTextSharp 採用 AGPL 許可,非開源專案需要商業許可,而 IronPDF 提供靈活的許可選項,包括免費試用。
如何使用 C# 為現有 PDF 新增文字?
使用 IronPDF,您可以透過PdfDocument物件上的AddText等方法向現有 PDF 添加文本,從而實現無縫的 PDF 編輯。
與 iTextSharp 相比,使用 IronPDF 有哪些優點?
IronPDF 提供卓越的效能、現代化的 API 和靈活的定價方案。它還提供高級的 HTML 轉 PDF 功能和更高的輸出質量,使其成為 C# 中 PDF 編輯的首選工具。
在 C# 專案中使用 IronPDF 需要哪些條件?
要開始在 C# 專案中使用 IronPDF,您需要安裝 Visual Studio IDE 和透過 NuGet 套件管理器安裝的 IronPDF 庫。
我可以用 C# 從 HTML 字串建立 PDF 嗎?
是的,IronPDF 允許您使用RenderHtmlAsPdf等方法從 HTML 字串建立 PDF,為 HTML 到 PDF 的轉換提供了一個強大的工具。
IronPDF為何能成為C#開發人員的多功能工具?
IronPDF 直覺的 API、高效的效能、先進的 HTML 到 PDF 轉換功能以及經濟實惠的價格,使其成為 C# 開發人員的多功能工具。
開發人員如何在 C# 中保證高品質的 PDF 輸出?
IronPDF 憑藉其先進的渲染引擎和專為專業 PDF 處理量身定制的全面功能集,為開發人員提供了高品質的 PDF 輸出。






