在編輯PDF方面iTextSharp與IronPDF之間的比較
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.
PDF (Portable Document Format,可攜式文件格式) 是一種廣泛使用的文件格式,因其能保留文件格式、安全性和可攜性而廣受歡迎。
PDF 檔案已經成為世界上使用最廣泛的文件格式之一,目前有多個函式庫可用於以 C# 語言建立和處理 PDF。
發現如何使用 C# 與 IronPDF 和 iTextSharp 來編輯 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 for .NET 是一個 .NET 函式庫,可使用 C# 來建立、編輯和處理 PDF 檔案。 它提供了一個現代化且直覺的 API 來處理 PDF 文件。 IronPDF 是一個商業函式庫,附有 免費試用版和訂閱選項,可供更廣泛的使用。
比較 iTextSharp 和 IronPDF 函式庫
iTextSharp 和 IronPDF 程式庫都提供了廣泛的特性和功能來建立、編輯和處理 PDF 文件。 然而,與 iTextSharp 相比,IronPDF 有幾項優勢,使其成為用 C# 處理 PDF 文件的首選。
使用 iTextSharp 和 IronPDF 編輯 PDF 檔案
既然我們已經討論過 iTextSharp 與 IronPDF 的差異,讓我們來看看如何使用這兩個函式庫編輯 PDF 檔案。 我們將通過使用 iTextSharp 和 IronPDF 在現有 PDF 文件中新增文字、表單欄位和填寫表單的實例。
使用 iTextSharp 編輯 PDF 檔案
先決條件
在我們開始之前,您將需要下列文件:
1.您的電腦已安裝 Visual Studio。 2.具備 C# 程式語言的基本知識。
- 在專案中安裝 iTextSharp 函式庫。
。
若要在專案中安裝 iTextSharp 函式庫,您可以使用 NuGet 套件管理程式。 開啟您的 Visual Studio 專案,在"解決方案總管"中的專案名稱上按一下滑鼠右鍵。 從上下文功能表中選擇"管理 NuGet 套件"。 在 NuGet 套件管理員中,搜尋"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);
}
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports System.IO
' Create a new PDF document
Using writer = New PdfWriter(New FileStream("newfile.pdf", FileMode.Create))
Using pdf = New PdfDocument(writer)
Dim document As New Document(pdf)
' Create a header paragraph
Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
' Add the header to the document
document.Add(header)
' Loop through pages and align header text
Dim i As Integer = 1
Do While i <= pdf.GetNumberOfPages()
Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim x As Single = pageSize.GetWidth() / 2
Dim y As Single = pageSize.GetTop() - 20
' Add the header text to each page
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
i += 1
Loop
' Set the margins
document.SetTopMargin(50)
document.SetBottomMargin(50)
End Using
End Using
在上面的程式碼中,我們建立了一個新的 PDF 檔案,名為"newfile.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();
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports iText.Html2pdf
Imports System.IO
'''
''' * iText URL to PDF
''' * anchor-itext-url-to-pdf
''' *
Private Sub ExistingWebURL()
' Initialize PDF writer
Dim writer As New PdfWriter("wikipedia.pdf")
' Initialize PDF document
Using pdf As New PdfDocument(writer)
Dim properties As New ConverterProperties()
properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format")
' Convert HTML to PDF
Dim document As Document = HtmlConverter.ConvertToDocument(New FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties)
' Create and add a header paragraph
Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
document.Add(header)
' Align header text for each page
Dim i As Integer = 1
Do While i <= pdf.GetNumberOfPages()
Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim x As Single = pageSize.GetWidth() / 2
Dim y As Single = pageSize.GetTop() - 20
' Add header text aligned at the top
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
i += 1
Loop
' Set the top and bottom margins
document.SetTopMargin(50)
document.SetBottomMargin(50)
document.Close()
End Using
End Sub
在此程式碼中,會開啟一個現有的 PDF,我們會以正確的文字對齊方式為其頁面加上標題。
使用 IronPDF 編輯 PDF 文件
IronPDF 是一個功能強大的 C# PDF 函式庫,方便編輯 PDF 文件。 本教程將介紹使用 IronPDF 編輯現有 PDF 檔案的步驟,包括建立新的 PDF 文件、新增頁面、合併 PDF 等。
。
先決條件
確保您擁有
- Visual Studio IDE
- IronPDF 資料庫
步驟 1:建立新專案
在 Visual Studio 中創建一個新 C# 專案。 選擇"控制台應用程式"專案類型。
步驟 2:安裝 IronPDF
。
使用 NuGet Package Manager 將 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);
Imports IronPdf
' Path to an existing PDF file
Private existingPdf = "C:\path\to\existing\pdf\document.pdf"
' Load the PDF document
Private pdfDoc = PdfDocument.FromFile(existingPdf)

步驟 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;
' Add a new page with default size
Dim newPage = pdfDoc.AddPage()
newPage.Size = PageSize.Letter
步驟 5:從網站建立 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");
}
Imports IronPdf
'''
''' * IronPDF URL to PDF
''' * anchor-ironpdf-website-to-pdf
''' *
Private Sub ExistingWebURL()
' Create PDF from a webpage
Dim 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() With {
.CenterText = "{pdf-title}",
.DrawDividerLine = True,
.FontSize = 16
}
Renderer.RenderingOptions.TextFooter = New TextHeaderFooter() With {
.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
Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")
PDF.SaveAs("wikipedia.pdf")
End Sub
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);
}
Imports IronPdf
'''
''' * IronPDF HTML to PDF
''' * anchor-ironpdf-document-from-html
''' *
Private Sub HTMLString()
' Render HTML to PDF
Dim Renderer = New IronPdf.ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")
Renderer.RenderingOptions.TextFooter = New HtmlHeaderFooter() With {.HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"}
Dim OutputPath = "ChromeHtmlToPdf.pdf"
PDF.SaveAs(OutputPath)
End Sub
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));
}
Imports iText.Html2pdf
Imports System.IO
'''
''' * iText HTML to PDF
''' * anchor-itext-html-to-pdf
''' *
Private Sub HTMLString()
HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", New FileStream("iText7HtmlToPdf.pdf", FileMode.Create))
End Sub
效能
IronPDF 的設計比 iTextSharp 更快、更有效率,可以使用更少的資源快速產生 PDF。 這種效率對於大型或複雜的文件至關重要。
定價
iTextSharp 在某些使用情況下需要商業授權,這可能會很昂貴。IronPDF 則提供更經濟實惠的定價模式,並針對不同需求和預算提供多種選項。
授權與定價
iTextSharp 與 IronPDF 的主要差異之一在於其授權與定價模式。
- iTextSharp:根據 AGPL 授權協議授權,非開源專案需要商業許可。 商業授權的費用各有不同。
- IronPDF:提供免費試用版和靈活的許可方式,包括開發者許可和伺服器許可,使其適合商業用途。
。
結論
總而言之,雖然 iTextSharp 和 IronPDF 都能以 C# 處理 PDF,但 IronPDF 卻是更多功能、更有效率的選擇。它提供了先進的功能、直覺式 API 以及更佳的效能。 其靈活的定價使其適用於商業專案和較大型的組織。
透過 IronPDF 優異的 HTML 至 PDF 轉換功能,開發人員可以輕鬆產生富媒體或互動內容的報表或文件。 IronPDF 的價格極具成本效益,對於需要為 C# 專案提供功能強大且有效率的 PDF 函式庫的開發人員而言,IronPDF 是絕佳的選擇。
常見問題解答
如何在C#中編輯PDF文件而不丟失格式?
您可以使用IronPDF在C#中編輯PDF文件,確保格式保留。IronPDF提供先進的功能和現代的API,用於高效的PDF操作。
安裝PDF庫在Visual Studio中有哪些步驟?
要在Visual Studio中安裝像IronPDF這樣的PDF庫,打開NuGet包管理器,搜索IronPDF,然後安裝包到您的項目中。
如何在C#中將網頁URL轉換為PDF?
IronPDF允許您使用ChromePdfRenderer類將網頁URL轉換為PDF,確保高品質輸出。
iTextSharp和IronPDF在授權方面有何不同?
iTextSharp在AGPL下授權,對於非開源項目需要商業授權,而IronPDF提供靈活的授權選項,包括免費試用。
如何使用C#向現有PDF添加文本?
通過IronPDF,您可以使用像AddText這樣的方法在PdfDocument對象上向現有PDF添加文本,實現無縫PDF編輯。
使用IronPDF相比iTextSharp有什麼優勢?
IronPDF提供更好的性能、現代的API和靈活的定價。它還提供先進的HTML到PDF的轉換和更好的輸出質量,使其成為C#中PDF編輯的首選。
開始在C#項目中使用IronPDF需要什麼?
您需要Visual Studio IDE和通過NuGet包管理器安裝的IronPDF庫以開始在您的C#項目中使用IronPDF。
我可以在C#中從HTML字符串創建PDF嗎?
可以,IronPDF允許使用RenderHtmlAsPdf等方法從HTML字符串創建PDF,提供一個強大的HTML到PDF轉換工具。
是什麼讓IronPDF成為C#開發者的多用途工具?
IronPDF的直觀API、高效表現、先進的HTML到PDF轉換以及具有成本效益的定價使其成為C#開發者的多用途工具。
開發人員如何確保C#中的高質量PDF輸出?
通過使用IronPDF,開發人員可以確保高質量的PDF輸出,因為其先進的渲染引擎和全面的功能集專為專業PDF操作而設計。

