如何在 C# 中匯出 PDF | IronPDF

C# Export to PDF Code Example Tutorial

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 是一個 C# PDF 庫,允許您使用 C# 將 HTML 儲存為 PDF。 它還允許 C# / VB 開發者以程式化方式編輯 PDF 文件。

快速入門:在 C# 中使用 IronPDF 將 HTML 匯出為 PDF

使用 IronPDF 在 C# 中輕鬆地將您的 HTML 內容匯出為 PDF。 本快速指南展示了如何將 HTML 轉換為 PDF 文件並透過簡單幾行程式碼保存它。 IronPDF 簡化了 PDF 生成,使開發者可以輕鬆地將 PDF 匯出功能整合到應用程式中,而無需麻煩。 深入了解並看看如何輕鬆入門!

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>HelloPDF</h1>").SaveAs("myExportedFile.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步驟)

  1. 從 NuGet 下載並安裝 C# PDF Export Library
  2. 探索 PdfDocument 文檔以發現數位簽名匯出 PDF 的方法
  3. 使用 System.IO.MemoryStream 將 PDF 保存到記憶體
  4. 將 PDF 作為二進制資料而非 HTML 提供給網頁
  5. 將 PDF 匯出為文件


儲存 PDF 的選項

如何將 PDF 保存到磁碟

使用 PdfDocument.SaveAs 方法將您的 PDF 保存到磁碟。

您會發現此方法支援新增密碼保護。 查看以下文章以瞭解更多關於數位簽名匯出 PDF 的資訊:'給 PDF 文件數位簽名.'

如何在 C# 中將 PDF 文件保存到 MemoryStream (System.IO.MemoryStream)

IronPdf.PdfDocument.Stream 屬性使用 System.IO.MemoryStream 將 PDF 保存到記憶體。

如何保存為二進制資料

IronPdf.PdfDocument.BinaryData 屬性將 PDF 文件匯出為記憶體中的二進制資料。

這會將 PDF 輸出為 ByteArray,在 C# 中表示為 byte []

如何從 Web 伺服器提供給瀏覽器

要將 PDF 提供給網頁,我們需要以二進制資料而非 HTML 發送它。

MVC PDF 匯出

// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
' Sends 'stream' to the client as a file download with the specified name.
Return New FileStreamResult(stream, "application/pdf") With {.FileDownloadName = "file.pdf"}
$vbLabelText   $csharpLabel

ASP.NET PDF 匯出

// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;

// Clears the existing response content
Response.Clear();

// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";

// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

// Flushes the response to send the data to the client
Response.Flush();
// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;

// Clears the existing response content
Response.Clear();

// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";

// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

// Flushes the response to send the data to the client
Response.Flush();
' Retrieves the PDF binary data
Dim Binary() As Byte = MyPdfDocument.BinaryData

' Clears the existing response content
Response.Clear()

' Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream"

' Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length)

' Flushes the response to send the data to the client
Response.Flush()
$vbLabelText   $csharpLabel

常見問題解答

如何在C#匯出PDF?

要在 C# 中匯出 PDF,可以使用 IronPDF 庫。首先,從 NuGet 下載並安裝 C# PDF 匯出程式庫。然後,探索 `PdfDocument` 方法,將 PDF 儲存到磁碟或內存,並將其提供給 Web 瀏覽器。

使用 IronPDF 儲存 PDF 檔案有哪些選項?

IronPDF 允許您使用多種方法儲存 PDF:使用PdfDocument.SaveAs儲存到磁碟,使用System.IO.MemoryStream儲存到內存,或使用PdfDocument.BinaryData匯出為二進位資料。

如何在C#中將PDF檔案儲存到磁碟?

在 IronPDF 中,使用PdfDocument.SaveAs方法可以將 PDF 檔案儲存到磁碟。此方法還支援添加密碼保護等功能。

如何在 C# 中將 PDF 檔案儲存到 MemoryStream 中?

在 IronPDF 中,您可以使用IronPdf.PdfDocument.Stream屬性將 PDF 儲存到記憶體中,該屬性採用System.IO.MemoryStream來高效處理 PDF 資料。

如何在 C# 中將 PDF 檔案匯出為二進位資料?

IronPDF 中的PdfDocument.BinaryData屬性可讓您將 PDF 文件匯出為記憶體中的二進位數據,表示為byte[]陣列。

如何使用 IronPDF 將 Web 伺服器上的 PDF 檔案提供給瀏覽器?

若要使用 IronPDF 將 Web 伺服器上的 PDF 檔案提供給瀏覽器,您應該將其作為二進位資料傳送。在 MVC 中,您可以使用FileStreamResult ;在 ASP.NET 中,您需要將二進位資料直接寫入回應流。

在IronPDF處理PDF時,System.IO.MemoryStream扮演什麼角色?

IronPDF 使用System.IO.MemoryStream將 PDF 檔案儲存到記憶體中,因此無需立即儲存到磁碟即可高效管理 PDF 資料。

如何在 MVC 中使用 IronPDF 將 PDF 檔案作為下載檔案發送?

在 MVC 應用程式中,您可以使用FileStreamResult類別將 PDF 檔案作為下載檔案傳送。這會將 PDF 資料串流傳輸到客戶端,以便使用指定的檔案名稱進行下載。

.NET 10 相容性:IronPDF 是否可以在 .NET 10 專案中完全使用?

是的。 IronPDF 完全支援 .NET 10,無需特殊配置或變通方法,即可在跨平台(Windows、Linux、macOS)的 .NET 10 專案中開箱即用。

Curtis Chau
技術作家

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

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

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

">

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once