跳過到頁腳內容
產品比較

如何使用 Aspose C# 和 IronPDF 創建 PDF

對於現代的 .NET 應用程式而言,以程式化的方式建立 PDF 檔案是不可或缺的,不論是產生發票、報表或將網頁內容轉換成 PDF 格式。 本教學探討如何使用 Aspose C# 和 IronPDF建立 PDF,並提供完整的程式碼範例,以協助開發人員選擇適合自己的函式庫。

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 1

先決條件

在開始建立 PDF 檔案之前,請確保您已具備下列條件:

  • .NET Framework 4.6.2+ 或 .NET Core 3.1+
  • Visual Studio 2019 或相容的 IDE
  • 使用 C# 程式設計的基本知識

安裝

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 2

透過套件管理員控制台安裝 Aspose PDF

開啟 Visual Studio 中的套件管理員控制台,並執行下列指令:

Install-Package Aspose.PDF

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 3 - Aspose.PDF 安裝

安裝 IronPDF。

使用相同的方法安裝 IronPdf 套件:

Install-Package IronPdf

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 4 - 安裝

注意:IronPdf.Linux 包含內嵌 Chrome 引擎,可提供優異的 HTML 渲染效果,並支援 Windows、Linux、macOS、Docker containers 和雲端平台。

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 5 - 創建 PDF - IronPDF

建立您的第一個 PDF 文件

讓我們使用這兩個函式庫建立 PDF 檔案,以瞭解它們的基本方法。 這些程式碼片段範例展示了 PDF 產生的基本邏輯。

使用 Aspose PDF for .NET API 創建 PDF.

using Aspose.Pdf;
using Aspose.Pdf.Text;
// Create new instance of Document class
var document = new Aspose.Pdf.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 Aspose.Pdf.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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此原始碼透過建立文件物件模型來建立 PDF 文件。 您建立一個新的文件,將頁面加入集合,然後將內容加入這些頁面。 Aspose.Pdf.Document 類別提供基礎,而段落則保留您的內容。 為了測試目的,這個 Hello World 範例展示了基本流程。

輸出

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 6 - PDF 輸出

使用 IronPDF 創建 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");
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPDF 採用不同的方式來建立 PDF - 它使用 Chrome 瀏覽器直接將 HTML 譯成 PDF 格式。 此 API 可讓您輕鬆轉換網頁內容並實現複雜的佈局。 這個函式庫可處理所有複雜的渲染,讓完成專業的結果變得更簡單。

輸出

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 7 - IronPDF 輸出

實例:建立發票

以下是使用這兩個函式庫建立 PDF 檔案的完整程式碼,以提供實用的發票解決方案。

使用新 Aspose PDF 開發發票

// 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;
// Add to paragraphs
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);
// Save the PDF document
document.Save("invoice.pdf");
// 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;
// Add to paragraphs
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);
// Save the PDF document
document.Save("invoice.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Aspose PDF NET API 要求以程式化方式建立每個元素。 您建立文件物件、新增頁面,然後將內容新增至段落集合。 這樣可以精確控制,但需要更多的程式碼來完成複雜的佈局。

使用 IronPDF 開發發票。

var renderer = new ChromePdfRenderer();
// Configure PDF output settings
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");
var renderer = new ChromePdfRenderer();
// Configure PDF output settings
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

使用 IronPDF,您只需編寫標準的 HTML,系統就會處理渲染。 此方法可讓您更輕鬆地建立具有專業版面的 PDF 檔案。 您可以參考外部樣式表、新增圖片並納入連結。

輸出

How to Create a PDF with Aspose C# VS IronPDF:圖 8 - IronPDF 發票輸出

進階功能

使用 IronPDF 將網頁轉換為 PDF 檔案

// Load and convert URL to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.SaveAs("website.pdf");
// Load and convert URL to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.SaveAs("website.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

為生成的 PDF 增加安全性

// Create PDF document
var pdf = renderer.RenderHtmlAsPdf(html);
// Implement security settings
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
// Save secured file
pdf.SaveAs("secured.pdf");
// Create PDF document
var pdf = renderer.RenderHtmlAsPdf(html);
// Implement security settings
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
// Save secured file
pdf.SaveAs("secured.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPDF 提供了直觀的方法來為您的 PDF 文件添加安全性、頁首、頁尾以及其他功能。 您也可以刪除頁面、合併檔案或擷取內容。

輸出

How to Create a PDF with Aspose C# VS IronPDF:圖 9 - IronPDF 安全 PDF 輸出

選擇正確的函式庫

何時使用 IronPDF。

IronPdf 的優勢在於您:

  • 需要將 HTML 轉換為 PDF 格式
  • 想要更簡單的程式碼和更快速的開發
  • 要求執行 JavaScript
  • 需要跨平台支援
  • 部署到 Docker 或雲端環境

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 10 - 功能

何時使用 Aspose PDF

Aspose PDF for .NET 應用程式在下列情況下運作良好:

  • 以程式化方式從零開始建立 PDF
  • 需要對文件物件模型進行粒度控制
  • 處理複雜的 PDF 操作
  • 匯入現有 PDF 檔案進行處理

授權比較

IronPdf 提供直接的 許可,起價為 749 美元,包括支援和更新。 免費試用版包含所有功能,不含水印。

 如何使用 Aspose C# VS IronPDF 創建 PDF:圖 11 - 授權

Aspose PDF 的起價為 1,199 美元,支援費用另計。 這兩個函式庫都提供評估版本以供測試之用。

結論

這兩個函式庫都能有效地在 C# 中建立 PDF 文件。 Aspose PDF 可透過其文件物件模型和 TextFragment 類別提供細部控制,而 IronPDF 則可使用熟悉的網路技術,在 HTML 至 PDF 的轉換上大放異彩。

對於大多數現代 .NET 應用程式而言,IronPDF 以其直覺的 API、優異的 HTML 渲染、內含的支援,以及使用簡單程式碼建立 PDF 檔案的能力,提供了更好的價值。 無論您是需要載入網頁、添加 BMP 等圖片,或是實現複雜的版面設計,IronPDF 基於 Chrome 的功能都能簡化流程。

開發人員注意事項:IronPDF 也包含繪圖功能,可以重現複雜的設計。 如有問題,請聯絡其工程團隊或搜尋其說明文件,以解決任何問題。

從 IronPdf 的 免費試用開始,評估它如何完成您的 PDF 生成需求。 存取所有功能,以便在您的 .NET 解決方案中建立、轉換和處理 PDF 檔案。

請注意Aspose 是其各自擁有者的註冊商標。 本網站與 Aspose 無任何關聯、背書或贊助。 所有產品名稱、標誌和品牌均為其各自擁有者的財產。 比較資料僅供參考,並反映撰寫時的公開資訊。

常見問題解答

IronPDF 創建 PDF 的主要功能是什麼?

IronPDF 提供 HTML 至 PDF 轉換、合併 PDF 以及新增頁首和頁尾等功能。它被設計成可在 .NET 應用程式中輕鬆使用。

在易用性方面,Aspose C# 與 IronPDF 相比如何?

IronPDF 因其直觀的 API 和與 .NET 應用程式的直接整合而經常受到好評,使其成為追求簡單的開發人員的首選。

IronPDF 能否高效處理大規模 PDF 生成?

是的,IronPDF 已針對效能進行最佳化,並能有效率地處理大型 PDF 產生,因此適用於企業級應用程式。

IronPDF 是否支援合併或分割等 PDF 操作?

IronPDF 支援各種 PDF 操作,包括合併、分割和修改現有的 PDF,為開發人員提供多功能的解決方案。

Aspose C# 和 IronPDF 的授權模式是否有差異?

是的,IronPDF 提供靈活的授權選項,包括永久和訂閱模式,這可能與 Aspose 的產品不同。

什麼類型的應用程式可以從使用 IronPDF 中獲益?

IronPDF 非常適合需要動態 PDF 生成的應用程式,例如報表系統、發票和文件管理解決方案。

IronPDF 如何處理 PDF 中的安全功能?

IronPDF 包括密碼保護和加密等安全功能,可確保生成的 PDF 安全且符合資料保護標準。

使用 IronPDF 是否有特定的系統需求?

IronPDF 與 .NET Framework 和 .NET Core 相容,但具體的系統需求取決於所使用的版本和應用環境。

IronPDF 可以將網頁轉換成 PDF 格式嗎?

是的,IronPDF 可以將 HTML 網頁轉換成 PDF 格式,包括支援複雜的版面設計和樣式。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。