
在沒有Adobe Pro的情況下如何編輯PDF(初學者教程)
問題:您可以在ASP.NET中使用iText來建立PDF嗎?是否有更好的替代方案? 是的 -- iText是一個長期存在的.NET PDF程式庫,但IronPDF提供了現代的HTML到PDF方法,消除了學習底層PDF文件結構的需求。 本指南比較了這兩個程式庫並提供工作程式碼範例,讓您可以選擇適合您專案的工具。
iText和IronPDF之間的主要區別是什麼?
iText是Java iText程式庫的.NET移植版,通過其文件類和底層PDF內容操作提供程式化的PDF建立。 雖然iText功能強大,但使用它需要了解PDF文件結構,工作於文件物件,並使用座標和頁面大小規範手動定位元素。 API因必要性而顯得冗長 -- 程式庫暴露了PDF規範的完整複雜性,這意味著在生成優雅的輸出之前有很多東西需要學習。
IronPDF採用不同的方法,專注於使用Chrome渲染引擎的HTML到PDF轉換。開發者可以使用熟悉的HTML和CSS生成PDF文件,使PDF建立像設計網頁一樣簡單。 IronPDF在幕後處理PDF生成邏輯,允許您生成具有現代風格和JavaScript支持的文件。 由於渲染管道由Chromium驅動,任何在現代瀏覽器中工作的佈局會忠實地轉換為PDF輸出 -- 包括flexbox、網格、網頁字體和JavaScript生成的內容。
這種架構差異的實際後果是,iText獎勵那些希望對PDF文件中的每個字節進行細粒度控制的開發人員,而IronPDF獎勵那些希望利用已有技能來快速生成視覺精美文件的開發人員。 對於大多數網頁應用場景 -- 發票、報告、訂單確認書和資料導出 -- HTML方法的構建速度更快且更易於維護。
| 功能 | iText | IronPDF |
|---|---|---|
| HTML到PDF | 有限(通過XMLWorker插件) | 完整的Chrome引擎渲染 |
| CSS支持 | 部分 | 完整CSS3支持 |
| JavaScript支持 | None | 是的(通過Chrome引擎) |
| 授權 | AGPL(需要商業授權) | 商業的,免版稅 |
| 學習曲線 | 陡峭(需要PDF API知識) | 低(HTML / CSS足夠) |
| NuGet安裝 | Install-Package iTextSharp | Install-Package IronPdf |
| .NET相容性 | .NET Framework, .NET Core | .NET 8, .NET 9, .NET 10, Framework |
如何將這些程式庫安裝到.NET專案中?
安裝任一程式庫均從NuGet Package Manager開始。 對於iText,請注意,新版本的iText是在AGPL授權下運行的,這需要開源您的應用或購買商業授權:
# Install iTextSharp via Package Manager Console
Install-Package iTextSharp
對於IronPDF,您可以通過NuGet Package Manager Console、.NET CLI或直接在Visual Studio的NuGet UI中搜索進行安裝:
安裝後,IronPDF可以通過一個using語句立即使用。 對於基本的PDF生成功能不需要額外配置。 對於高級場景 -- 如設置許可金鑰、配置渲染選項或在雲環境中生成PDF -- 請參閱IronPDF文件。 IronPDF支持Linux、macOS和Windows部署,包括在Docker或Kubernetes運行的容器化環境,使其非常適合現代雲原生ASP.NET應用。
如何使用每個程式庫建立基本的PDF文件?
理解API區別的最清晰方式是用兩個程式庫並排建立一個簡單的"Hello World" PDF。
使用iText生成PDF
使用iText,您必須直接與PdfWriter交互:
using iTextSharp.text;
using iTextSharp.text.pdf;
var memoryStream = new MemoryStream();
Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
var paragraph = new Paragraph("Hello World - PDF Document");
paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 16);
pdfDoc.Add(paragraph);
pdfDoc.Add(new Paragraph("Creating PDF documents with iTextSharp"));
pdfDoc.Close();
// Return as a downloadable file
var pdfBytes = memoryStream.ToArray();Imports iTextSharp.text
Imports iTextSharp.text.pdf
Dim memoryStream As New MemoryStream()
Dim pdfDoc As New Document(PageSize.A4, 25, 25, 25, 15)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
Dim paragraph As New Paragraph("Hello World - PDF Document")
paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 16)
pdfDoc.Add(paragraph)
pdfDoc.Add(New Paragraph("Creating PDF documents with iTextSharp"))
pdfDoc.Close()
' Return as a downloadable file
Dim pdfBytes As Byte() = memoryStream.ToArray()這需要知道FontFactory如何交互 -- 對於不熟悉PDF生成的開發者來說,這是一項不小的學習投資。
使用IronPDF生成PDF
使用IronPDF,等效的任務使用熟悉的HTML:
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<h1>Hello World - PDF Document</h1>
<p>Creating PDFs with IronPDF is straightforward!</p>
");
var pdfBytes = pdf.BinaryData;Imports IronPdf
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("
<h1>Hello World - PDF Document</h1>
<p>Creating PDFs with IronPDF is straightforward!</p>
")
Dim pdfBytes = pdf.BinaryDataIronPDF的方法允許您直接寫HTML,消除了處理底層PDF元素的需求。 ChromePdfRenderer類使用基於Chromium的引擎內部處理所有渲染,確保像素精確的輸出。
如何建立帶有圖片和CSS的樣式化PDF?
樣式化文件揭示了兩個程式庫之間的最大差異。 在生成發票、報告或品牌文件時,IronPDF中的CSS樣式大大減少了所需程式碼量。
使用IronPDF生成發票
using IronPdf;
var html = @"
<style>
body { font-family: Arial, sans-serif; margin: 0; }
.invoice-header { background: #4CAF50; color: white; padding: 20px; }
.invoice-table { width: 100%; border-collapse: collapse; margin-top: 20px; }
.invoice-table th, .invoice-table td {
border: 1px solid #ddd; padding: 8px; text-align: left;
}
.invoice-table th { background-color: #f2f2f2; }
.total { font-size: 18px; font-weight: bold; margin-top: 16px; }
</style>
<div class='invoice-header'>
<h1>Invoice #2024-001</h1>
<p>Due: March 15, 2024</p>
</div>
<table class='invoice-table'>
<tr><th>Item</th><th>Quantity</th><th>Unit Price</th><th>Total</th></tr>
<tr><td>PDF License</td><td>1</td><td>$599</td><td>$599</td></tr>
<tr><td>Support Package</td><td>1</td><td>$199</td><td>$199</td></tr>
</table>
<p class='total'>Grand Total: $798</p>
";
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(html);
var pdfBytes = pdfDocument.BinaryData;Imports IronPdf
Dim html As String = "
<style>
body { font-family: Arial, sans-serif; margin: 0; }
.invoice-header { background: #4CAF50; color: white; padding: 20px; }
.invoice-table { width: 100%; border-collapse: collapse; margin-top: 20px; }
.invoice-table th, .invoice-table td {
border: 1px solid #ddd; padding: 8px; text-align: left;
}
.invoice-table th { background-color: #f2f2f2; }
.total { font-size: 18px; font-weight: bold; margin-top: 16px; }
</style>
<div class='invoice-header'>
<h1>Invoice #2024-001</h1>
<p>Due: March 15, 2024</p>
</div>
<table class='invoice-table'>
<tr><th>Item</th><th>Quantity</th><th>Unit Price</th><th>Total</th></tr>
<tr><td>PDF License</td><td>1</td><td>$599</td><td>$599</td></tr>
<tr><td>Support Package</td><td>1</td><td>$199</td><td>$199</td></tr>
</table>
<p class='total'>Grand Total: $798</p>
"
Dim renderer As New ChromePdfRenderer()
Dim pdfDocument = renderer.RenderHtmlAsPdf(html)
Dim pdfBytes = pdfDocument.BinaryData使用iText生成發票
使用iText實現類似輸出需要程式化地構建每個視覺元素:
using iTextSharp.text;
using iTextSharp.text.pdf;
var output = new MemoryStream();
var document = new Document(PageSize.A4);
PdfWriter.GetInstance(document, output);
document.Open();
// Header -- manual font and color setup
var titleFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20,
new BaseColor(255, 255, 255));
var headerParagraph = new Paragraph("Invoice #2024-001", titleFont);
document.Add(headerParagraph);
// Table -- each cell must be created individually
PdfPTable table = new PdfPTable(4);
table.WidthPercentage = 100;
string[] headers = { "Item", "Quantity", "Unit Price", "Total" };
foreach (var h in headers)
{
var cell = new PdfPCell(new Phrase(h,
FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
cell.BackgroundColor = new BaseColor(242, 242, 242);
table.AddCell(cell);
}
table.AddCell("PDF License");
table.AddCell("1");
table.AddCell("$599");
table.AddCell("$599");
document.Add(table);
var totalFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14);
document.Add(new Paragraph("Grand Total: $798", totalFont));
document.Close();Imports iTextSharp.text
Imports iTextSharp.text.pdf
Dim output As New MemoryStream()
Dim document As New Document(PageSize.A4)
PdfWriter.GetInstance(document, output)
document.Open()
' Header -- manual font and color setup
Dim titleFont As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20, New BaseColor(255, 255, 255))
Dim headerParagraph As New Paragraph("Invoice #2024-001", titleFont)
document.Add(headerParagraph)
' Table -- each cell must be created individually
Dim table As New PdfPTable(4)
table.WidthPercentage = 100
Dim headers As String() = {"Item", "Quantity", "Unit Price", "Total"}
For Each h As String In headers
Dim cell As New PdfPCell(New Phrase(h, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
cell.BackgroundColor = New BaseColor(242, 242, 242)
table.AddCell(cell)
Next
table.AddCell("PDF License")
table.AddCell("1")
table.AddCell("$599")
table.AddCell("$599")
document.Add(table)
Dim totalFont As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14)
document.Add(New Paragraph("Grand Total: $798", totalFont))
document.Close()差異顯而易見:IronPDF處理CSS、現代HTML和JavaScript,而iText則需要手動建立每個元素、字體規範和逐單元格構造表格。 對於生成數十個不同模板的文件密集型應用來說,這種程式碼量上的差異隨著時間的推移會顯著增加。
如何在ASP.NET中處理伺服器端PDF生成?
兩個程式庫都支持ASP.NET應用的伺服器端PDF生成。 返回PDF作為可下載文件響應的模式無論哪個程式庫生成字節都是相似的。 生產中使用的主要考慮因素是記憶體管理、執行緒安全和響應配置。 兩個程式庫使用記憶體流,因此您需要確保不將大型PDF長時間保存在記憶體中。 IronPDF的ChromePdfRenderer設計成每個請求實例化,因此無需擔心並發請求之間的共享狀態。
ASP.NET Core Controller Action with IronPDF
using IronPdf;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class PdfController : ControllerBase
{
[HttpGet("invoice/{id}")]
public IActionResult GenerateInvoice(int id)
{
var html = BuildInvoiceHtml(id); // your HTML template
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 20;
renderer.RenderingOptions.MarginBottom = 20;
var pdf = renderer.RenderHtmlAsPdf(html);
return File(pdf.BinaryData, "application/pdf", $"invoice-{id}.pdf");
}
private static string BuildInvoiceHtml(int id)
{
return $"<h1>Invoice #{id}</h1><p>Generated on {DateTime.UtcNow:yyyy-MM-dd}</p>";
}
}Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
<ApiController>
<Route("api/[controller]")>
Public Class PdfController
Inherits ControllerBase
<HttpGet("invoice/{id}")>
Public Function GenerateInvoice(id As Integer) As IActionResult
Dim html As String = BuildInvoiceHtml(id) ' your HTML template
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
renderer.RenderingOptions.MarginTop = 20
renderer.RenderingOptions.MarginBottom = 20
Dim pdf = renderer.RenderHtmlAsPdf(html)
Return File(pdf.BinaryData, "application/pdf", $"invoice-{id}.pdf")
End Function
Private Shared Function BuildInvoiceHtml(id As Integer) As String
Return $"<h1>Invoice #{id}</h1><p>Generated on {DateTime.UtcNow:yyyy-MM-dd}</p>"
End Function
End Class對於ASP.NET MVC(非Core)專案,以相同的方式返回FileResult。 IronPDF還支持從URL生成PDF,這在轉換現有網頁而不是構建HTML字串時非常有用。
您還可以使用新增頁眉和頁腳,應用數位簽名,設定密碼保護,或使用IronPDF的文件編輯API合併多個PDF -- 所有這些功能都來自同一個軟體包。
如何從iText遷移到IronPDF?
將現有的iText專案遷移到IronPDF遵循一個簡單的模式:
- 將文件模型程式碼替換為HTML模板。 不用構建
PdfPCell物件,構建一個HTML字串或載入HTML文件。您現有的CSS樣式表和Razor部分視圖可以直接重用。 - 替換渲染調用。 用
new ChromePdfRenderer().RenderHtmlAsPdf(html)。 - 更新字節提取。 用
pdf.BinaryData。 - 轉換高級設定。 iText的頁邊距、加密和文件元資料等功能在IronPDF的
PdfDocumentAPI中有直接對應。 - 驗證輸出保真度。 在代表性文件上並排運行兩個輸出。 IronPDF通常產生更好的視覺效果,因為它使用的是全瀏覽器渲染引擎而不是PDF原生佈局引擎。
對於已經擁有HTML模板(來自電子郵件生成器、Razor視圖或報告生成器)的團隊來說,遷移通常能在幾小時而不是幾天內完成。 IronPDF可以在ASP.NET Core中直接渲染Razor視圖為PDF,這進一步加速了遷移過程。 已投入基於CSS的文件設計(例如使用列印樣式表來控制換頁和邊距)的團隊會發現這些技能可以直接轉移到IronPDF。
檢查IronPDF遷移指南以獲得針對加密、蓋章和其他高級iText功能的詳細模式。
您應該選擇哪個程式庫?
對於開始新專案或從iText遷移的開發者來說,以下因素適用:
授權: iText的新版本使用AGPL授權,這需要開源您的應用或從iText Group購買商業授權。IronPDF提供了簡單的商業授權,無需開源義務。 如果您的專案是封閉源或商業性質,僅此一項區別就可能決定您的選擇。
學習曲線: IronPDF的基於HTML的方法意味著花在學習PDF特定API上的時間更少。如果您的團隊了解HTML和CSS,那麼使用IronPDF生成PDF可以立即開始。 不需要研究PDF座標系統、字形編碼或字體嵌入 -- IronPDF會透明地處理這些。
功能覆蓋: IronPDF支持PDF/A合規性,表單填寫,加水印等 -- 所有這些都來自單個NuGet包。 高級功能如數位簽名和PDF合併也包括在內,無需額外依賴。
遷移路徑: 從iText遷移到IronPDF涉及將文件操作程式碼替換為HTML模板並更新渲染調用。 由於IronPDF使用的是全瀏覽器引擎,輸出質量通常更高,結果程式碼顯著更短,且易於維護。
關於PDF渲染引擎的工作原理以及它們的區別,Mozilla的PDF文件和Adobe的PDF規範資源提供了有用的背景知識。 iText Group官方網站詳細描述了AGPL條款。
您的下一步是什麼?
在您的ASP.NET專案中開始使用IronPDF:
- 安裝NuGet包:
Install-Package IronPdf - 將
using IronPdf;新增到您的文件中 - 建立一個
RenderHtmlAsPdf() - 作為
pdf.BinaryData
瀏覽這些資源以進一步探索:
- IronPDF HTML到PDF文件 -- 完整的渲染選項和範例
- ASP.NET Core PDF生成指南 -- Razor視圖渲染和控制器模式
- PDF安全性和加密 -- 密碼保護和許可
- PDF合併和拆分 -- 結合多個文件
- IronPDF免費試用 -- 在開發過程中無水印地測試所有功能

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


