跳至頁尾內容
使用IRONPDF

在ASP.NET Core中使用C#將HTML轉換為PDF -- IronPDF指南

Full Comparison

Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Aspose PDF on pricing, HTML support, and licensing.

View Full Comparison

Aspose.PDF和IronPDF都可以讓您使用C#以程式化方式建立PDF文件,IronPDF利用Chrome呈現提供更簡單的HTML到PDF轉換,而Aspose.PDF則透過其文件物件模型提供詳細控制。

IronPDF的C# PDF程式庫首頁橫幅展示主要功能,包括HTML到PDF轉換、PDF編輯功能、靈活的部署選項以及免費試用提供

您需要什麼先決條件?

在開始在C#中建立PDF文件之前,請確保您具有:

  • .NET 10或相容的.NET 6+執行環境
  • Visual Studio 2022或相容的IDE
  • C#編程的基本知識
  • 配置了NuGet Package Manager

這兩個函式庫都支援Windows、Linux和macOS環境,並對每個平台有特定的安裝考量。 IronPDF還支援Docker容器和所有主要的雲平台。

跨平台支援圖展示了多個.NET版本、操作系統和部署環境(包括Windows、Linux、Mac、Docker、Azure和AWS)中與C#、F#和VB.NET的相容性

您如何安裝Aspose.PDF和IronPDF?

您如何通過Package Manager Console安裝Aspose.PDF?

在Visual Studio中打開Package Manager Console,然後運行:

Install-Package Aspose.PDF
Install-Package Aspose.PDF
SHELL

安裝會下載Aspose.PDF及其依賴項。 該程式庫需要大約150 MB的磁碟空間並包含完整的文件。 對於使用自定義包來源的高級安裝場景,您可能需要手動配置其他依賴項。

Package Manager Console展示了在Visual Studio中成功安裝Aspose.PDF 25.10.0版本及其依賴項

您如何通過NuGet安裝IronPDF?

可使用Package Manager Console或.NET CLI:

Install-Package IronPdf
dotnet add package IronPdf

IronPDF的安裝自動包含Chrome呈現引擎及所有必需的依賴項。 對Docker部署來說,可能需要額外的Linux專用配置以確保正確的呈現能力。

Package Manager Console展示IronPDF NuGet包的安裝進度,包括多個依賴項的下載

為什麼IronPDF包含嵌入式Chrome引擎?

IronPDF包含嵌入式Chrome引擎,以實現Windows、Linux、macOS和Docker中的出色HTML呈現。 這確保了像素級精準的呈現,與瀏覽器輸出相匹配,包括對現代CSS、JavaScript和網頁字體的支援。

IronPDF功能比較展示三個主要優勢:像素級精準呈現、5分鐘安裝以及跨平台相容性,每個都有詳細的支援資訊

您如何建立您的第一個PDF文件?

使用這兩個函式庫建立PDF文件揭示了它們基本的不同理念。 Aspose通過明確的物件模型構建文件,而IronPDF使用Chrome直接將HTML轉換為PDF。 下面的範例並排展示了這些方法,以便您評估哪种更適合您的項目。

Aspose.PDF如何建立文件?

using Aspose.Pdf;
using Aspose.Pdf.Text;

// Create new instance of Document class
var document = new Document();
// Add pages to the document object
var page = document.Pages.Add();
// Create new TextFragment with Hello World text
var textFragment = new TextFragment("Hello World!");
textFragment.TextState.FontSize = 24;
// Add text to paragraphs collection
page.Paragraphs.Add(textFragment);
// Save the generated PDF document
document.Save("output.pdf");
using Aspose.Pdf;
using Aspose.Pdf.Text;

// Create new instance of Document class
var document = new Document();
// Add pages to the document object
var page = document.Pages.Add();
// Create new TextFragment with Hello World text
var textFragment = new TextFragment("Hello World!");
textFragment.TextState.FontSize = 24;
// Add text to paragraphs collection
page.Paragraphs.Add(textFragment);
// Save the generated PDF document
document.Save("output.pdf");
Imports Aspose.Pdf
Imports Aspose.Pdf.Text

' Create new instance of Document class
Dim document As New Document()
' Add pages to the document object
Dim page As Page = document.Pages.Add()
' Create new TextFragment with Hello World text
Dim textFragment As New TextFragment("Hello World!")
textFragment.TextState.FontSize = 24
' Add text to paragraphs collection
page.Paragraphs.Add(textFragment)
' Save the generated PDF document
document.Save("output.pdf")
$vbLabelText   $csharpLabel

這段程式碼通過構建文件物件模型來建立PDF文件。 您建立一個新文件,將頁面新增到集合中,然後將內容新增到這些頁面上。 Document類提供了基礎,而段落承載了您的內容。 這種方法需要理解Aspose的物件層次結構並明確管理字體。 Aspose的官方文件涵蓋了其完整的基於DOM的API在aspose.com上

Aspose.PDF的輸出看起來如何?

PDF查看器顯示用Aspose.PDF建立的簡單

IronPDF如何建立文件?

using IronPdf;

// Create new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Convert HTML string to PDF file
var html = "<h1>Hello World!</h1>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Save PDF files using SaveAs method
pdf.SaveAs("output.pdf");
using IronPdf;

// Create new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Convert HTML string to PDF file
var html = "<h1>Hello World!</h1>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Save PDF files using SaveAs method
pdf.SaveAs("output.pdf");
Imports IronPdf

' Create new instance of ChromePdfRenderer
Dim renderer As New ChromePdfRenderer()
' Convert HTML string to PDF file
Dim html As String = "<h1>Hello World!</h1>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
' Save PDF files using SaveAs method
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

IronPDF採用不同的方法 -- 它使用Chrome直接將HTML呈現為PDF格式。 這使您可以使用標準HTML標記,並在最終文件中準確呈現。 您還可以從本地HTML文件或指向在線網頁的URL進行呈現。

IronPDF的輸出看起來如何?

PDF查看器顯示帶有Iron Software水印的簡單

您如何建立現實世界中的發票PDF?

在商業應用中,發票生成是最常見的PDF用例之一。 以下範例展示了帶有行項表格的簡單發票。 比較這兩種方法清楚地顯示了物件模型控制和HTML驅動的簡單性之間的權衡。

您如何使用Aspose.PDF構建發票?

using Aspose.Pdf;
using Aspose.Pdf.Text;

// Create new Document instance
var document = new Document();
var page = document.Pages.Add();
// Add title text
var title = new TextFragment("INVOICE");
title.TextState.FontSize = 28;
title.HorizontalAlignment = HorizontalAlignment.Center;
page.Paragraphs.Add(title);
// Create table object for invoice items
var table = new Table();
table.ColumnWidths = "200 100 100";
// Add header row to table
var headerRow = table.Rows.Add();
headerRow.Cells.Add("Description");
headerRow.Cells.Add("Quantity");
headerRow.Cells.Add("Price");
// Add data rows
var dataRow = table.Rows.Add();
dataRow.Cells.Add("Product A");
dataRow.Cells.Add("2");
dataRow.Cells.Add("$50.00");
// Add table to page paragraphs
page.Paragraphs.Add(table);
document.Save("invoice.pdf");
using Aspose.Pdf;
using Aspose.Pdf.Text;

// Create new Document instance
var document = new Document();
var page = document.Pages.Add();
// Add title text
var title = new TextFragment("INVOICE");
title.TextState.FontSize = 28;
title.HorizontalAlignment = HorizontalAlignment.Center;
page.Paragraphs.Add(title);
// Create table object for invoice items
var table = new Table();
table.ColumnWidths = "200 100 100";
// Add header row to table
var headerRow = table.Rows.Add();
headerRow.Cells.Add("Description");
headerRow.Cells.Add("Quantity");
headerRow.Cells.Add("Price");
// Add data rows
var dataRow = table.Rows.Add();
dataRow.Cells.Add("Product A");
dataRow.Cells.Add("2");
dataRow.Cells.Add("$50.00");
// Add table to page paragraphs
page.Paragraphs.Add(table);
document.Save("invoice.pdf");
Imports Aspose.Pdf
Imports Aspose.Pdf.Text

' Create new Document instance
Dim document As New Document()
Dim page As Page = document.Pages.Add()
' Add title text
Dim title As New TextFragment("INVOICE")
title.TextState.FontSize = 28
title.HorizontalAlignment = HorizontalAlignment.Center
page.Paragraphs.Add(title)
' Create table object for invoice items
Dim table As New Table()
table.ColumnWidths = "200 100 100"
' Add header row to table
Dim headerRow As Row = table.Rows.Add()
headerRow.Cells.Add("Description")
headerRow.Cells.Add("Quantity")
headerRow.Cells.Add("Price")
' Add data rows
Dim dataRow As Row = table.Rows.Add()
dataRow.Cells.Add("Product A")
dataRow.Cells.Add("2")
dataRow.Cells.Add("$50.00")
' Add table to page paragraphs
page.Paragraphs.Add(table)
document.Save("invoice.pdf")
$vbLabelText   $csharpLabel

Aspose.PDF .NET API需要程式化地構建每個元素。 您建立文件物件,新增頁面,然後將內容新增到段落集合中。 當您需要精準控制時,這種方法顯示出明顯的好處,但需要更多程式碼來實現複雜的佈局。 若要新增頁眉和頁腳或設置自定義邊距,必須在保存前初始化其他的配置物件。

您如何使用IronPDF構建發票?

using IronPdf;

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
// HTML string with invoice content
var html = @"
<html>
<head>
    <style>
        table { width: 100%; }
        th, td { padding: 8px; }
    </style>
</head>
<body>
    <h1>INVOICE</h1>
    <table>
        <tr><th>Description</th><th>Quantity</th><th>Price</th></tr>
        <tr><td>Product A</td><td>2</td><td>$50.00</td></tr>
    </table>
</body>
</html>";
// Generate PDF from HTML
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("invoice.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
// HTML string with invoice content
var html = @"
<html>
<head>
    <style>
        table { width: 100%; }
        th, td { padding: 8px; }
    </style>
</head>
<body>
    <h1>INVOICE</h1>
    <table>
        <tr><th>Description</th><th>Quantity</th><th>Price</th></tr>
        <tr><td>Product A</td><td>2</td><td>$50.00</td></tr>
    </table>
</body>
</html>";
// Generate PDF from HTML
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("invoice.pdf");
Imports IronPdf

Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.MarginTop = 25
renderer.RenderingOptions.MarginBottom = 25
' HTML string with invoice content
Dim html As String = "
<html>
<head>
    <style>
        table { width: 100%; }
        th, td { padding: 8px; }
    </style>
</head>
<body>
    <h1>INVOICE</h1>
    <table>
        <tr><th>Description</th><th>Quantity</th><th>Price</th></tr>
        <tr><td>Product A</td><td>2</td><td>$50.00</td></tr>
    </table>
</body>
</html>"
' Generate PDF from HTML
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("invoice.pdf")
$vbLabelText   $csharpLabel

使用IronPDF,您編寫標準HTML,呈現引擎處理其餘內容。您的應用程式可以立即參考外部樣式表、新增圖像並包含連結。 呈現選項提供了對輸出的細緻控制,包括自定義紙張大小和頁面方向。

發票的輸出看起來如何?

程式化生成的基本PDF發票範例,顯示以表格格式的產品資訊,並有可見水印覆蓋

有哪些進階功能可用?

這兩個函式庫提供了遠超基本PDF建立功能的能力。 了解這些進階功能有助於您確定哪個函式庫最符合您的需求。

您如何將網頁轉換為PDF?

using IronPdf;

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("website.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("website.pdf");
Imports IronPdf

Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://example.com")
pdf.SaveAs("website.pdf")
$vbLabelText   $csharpLabel

IronPDF在URL到PDF轉換方面表現出色,能處理包含JavaScript、cookies和身份驗證的複雜網站。 Chrome引擎確保精準呈現現代web應用,包括用Angular或React構建的單頁應用。

您如何向PDF新增安全性?

using IronPdf;

var renderer = new ChromePdfRenderer();
var html = "<h1>Confidential Report</h1>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Apply password protection and print rights
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
pdf.SaveAs("secured.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();
var html = "<h1>Confidential Report</h1>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Apply password protection and print rights
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
pdf.SaveAs("secured.pdf");
Imports IronPdf

Dim renderer As New ChromePdfRenderer()
Dim html As String = "<h1>Confidential Report</h1>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
' Apply password protection and print rights
pdf.SecuritySettings.UserPassword = "user123"
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights
pdf.SaveAs("secured.pdf")
$vbLabelText   $csharpLabel

IronPDF提供直觀的方法來為您的PDF文件新增安全性和數位簽章。 您還可以合併或分割文件以及提取文字和圖像以進行下游處理。

受保護的PDF看起來如何?

受保護的PDF文件的密碼保護對話框,顯示密碼輸入框和提交按鈕

IronPDF還支持哪些其他進階功能?

IronPDF支援許多其他場景,包括:

IronPDF和Aspose.PDF如何並排比較?

下表總結了IronPDF和Aspose.PDF之間的主要差異,以幫助您快速決策:

IronPDF vs. Aspose.PDF -- 主要功能比較
功能 IronPDF Aspose.PDF
HTML到PDF呈現 是 -- 基於Chrome的,像素級精準 有限
URL到PDF轉換 是 -- 處理JavaScript和cookies
文件物件模型API 部分 是 -- 完全按元素控制
跨平台支援 Windows,Linux,macOS,Docker Windows,Linux,macOS
數位簽章
PDF/A合規
授權起始價格 $liteLicense(包含支援) $1,199(支援另收額外費用)
免費試用 是 -- 所有功能,帶水印 是 -- 評估版本

比較顯示,當您的工作流程以HTML內容、網頁或現代前端技術為中心時,IronPDF是更強的選擇。 Aspose.PDF在僅使用程式化文件構建且需要細緻元素控制時仍具競爭力。

您應該選擇哪個函式庫?

什麼時候應該使用IronPDF?

當您需要:

  • 使用熟悉的web技術將HTML轉換為PDF
  • 呈現完整的網頁,包括由JavaScript驅動的內容
  • 部署到Docker或雲環境(如Azure或AWS)
  • 以更簡單的API交付更快的開發週期
  • 產生與瀏覽器呈現完全一致的輸出

功能比較圖展示了四個PDF功能類別:建立PDF、轉換PDF、編輯PDF以及簽署和保護PDF,每類別下都有詳細的功能清單

什麼時候應該使用Aspose.PDF?

Aspose.PDF for .NET在您:

  • 通過程式化文件物件模型從頭開始建立PDF時效果良好
  • 需要對佈局進行細緻的逐元素控制
  • 構建不從HTML源開始的文件中心應用程式
  • 需要與任何呈現引擎無關的特定PDF格式合規性

Aspose.PDF功能的詳細細分可在Microsoft的NuGet註冊表條目中找到Aspose.PDF

性能方面有哪些考量?

IronPDF的Chrome引擎為HTML呈現提供了出色的性能,而Aspose.PDF在完全程式化生成無需HTML源時可能表現得更好。 對於異步操作,這兩個函式庫都提供基於任務的API。在計劃大規模批處理部署時考慮記憶體使用和呈現超時,並在擴展時查看IronPDF的性能故障排除指南

這些授權如何比較?

IronPDF提供明確的授權起價$999,支援和更新包括在內。 免費試用包含所有功能,開發和測試帶有水印。 授權部署簡單,具有多種配置選項。

IronPDF授權頁展示四個價格層次(Lite、Plus、Professional和Unlimited),並提供每個計劃的折扣價格和功能

Aspose.PDF起價$1,199,支援和更新單獨計費。 這兩個函式庫都提供評估版本以供在購買前測試。 IronPDF提供了更多的靈活選項,包括基於項目和SaaS的授權模型,讓為成長中的團隊制定預算變得更加容易。

接下來您應該做什麼?

這兩個函式庫都能夠有效地使用C#建立PDF文件。 Aspose.PDF通過其文件物件模型提供細緻控制,而IronPDF利用熟悉的Web技術在HTML到PDF轉換方面表現出色。

對於大多數現代.NET 10應用,IronPDF提供更高的價值,因為其直觀的API、卓越的HTML呈現和包含的支援。 無論您是需要載入網頁、新增圖像還是構建複雜佈局,IronPDF基於Chrome的方法大大簡化了過程。 該程式庫的功能集涵蓋從基本PDF建立到高級簽署和安全性的所有內容。

IronPDF支援各種部署場景,包括Azure Functions、AWS Lambda和Blazor應用程式。 如有問題,工程支援團隊API參考資料都可用作資源。

從IronPDF的免費試用開始,以評估其如何滿足您的PDF生成需求。 您可以在不需要預先承諾的情況下,存取所有功能來建立、轉換和操作.NET解決方案中的PDF文件。

要比較IronPDF與其他熱門替代方案,請參閱IronPDF與iTextPDFSharp的指南。

常見問題

使用Aspose C#和IronPDF進行PDF建立的主要目的是什麼?

使用Aspose C#和IronPDF的主要目的是為現代.NET應用程式以程式方式建立PDF文件,例如生成發票、報告或將網頁內容轉換成PDF格式。

IronPDF如何改進.NET應用程式中的PDF建立?

IronPDF透過提供完整的工具和程式碼範例簡化了過程,提高了.NET應用程式中的PDF建立,使開發者更容易生成與操作PDF。

我可以使用IronPDF將網頁內容轉換成PDF格式嗎?

是的,IronPDF允許您無縫地將網頁內容轉換成PDF格式,這對於從HTML來源建立精確且專業的文件非常理想。

使用IronPDF生成PDF的一些典型用例是什麼?

使用IronPDF生成PDF的典型用例包括建立發票、生成報告,以及將網頁或HTML內容轉為PDF文件。

IronPDF是否提供完整的程式碼範例給開發者?

是的,IronPDF提供完整的程式碼範例,協助開發者有效利用該程式庫建立及管理PDF文件。

是否可以將IronPDF整合到其他.NET程式庫中以增強功能?

是的,IronPDF可以與其他.NET程式庫整合,例如Aspose C#,以增強PDF建立和操作的功能與能力。

使用IronPDF相比其他PDF程式庫的優勢有哪些?

使用IronPDF的優勢包括其易用性、完整的文件化說明,以及完整的程式碼範例,使其成為開發者的多功能選擇。

IronPDF能否處理如新增水印或標題等複雜的PDF生成任務?

是的,IronPDF可以處理複雜的PDF生成任務,包括為文件新增水印、標題和頁腳,提供文件自訂的彈性。

IronPDF適合.NET開發的初學者嗎?

IronPDF適合初學者和有經驗的開發者,因為它提供了使用者友好的功能和詳細的文件以指導使用者完成PDF建立過程。

IronPDF如何確保生成PDF文件的質量?

IronPDF透過提供精確佈局控制的強大工具以及對多種PDF功能和標準的支援來確保高品質的PDF文件。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話