如何在 .NET Core 中列印 PDF 文件
.NET Core 是由微軟開發的開源跨平台框架,因其靈活性、高效能和對雲端應用程式的支援而越來越受歡迎。 然而,在處理 PDF 文件時,特別是對於列印 PDF 文件等任務,開發人員需要一個強大且功能豐富的 PDF 庫。 這正是 IronPDF 能夠幫助開發者的地方。
IronPDF是一個專為 .NET 框架(包括 .NET Core 和 ASP.NET Core)設計的綜合資料庫,它簡化了處理 PDF 文件的流程。 它不僅允許創建和操作 PDF 文件,還提供了一種無縫的方式來列印這些文檔,無論是直接列印到印表機,還是將其轉換為適合列印的格式。
在本教學中,我們將深入探討 IronPDF 在 .NET Core 環境中的功能。 從專案設定、建立第一個 PDF 文檔,到配置列印設定、實現進階列印功能,我們將引導您完成每個步驟。本教學課程旨在為您提供在 .NET Core 應用程式中有效處理 PDF 檔案列印所需的知識和工具。
如何在 .NET Core 中列印 PDF 文件
- 在 Visual Studio 中建立一個 ASP.NET Core Web 項目
- 使用 NuGet 套件管理器安裝 PDF 庫
- 在控制器中建立或載入 PDF 文檔
- 使用 PDF 庫列印已載入的 PDF 文件
設定您的 .NET Core 項目
安裝 IronPDF - .NET PDF 庫
要開始在 .NET 應用程式中使用 PDF,第一步是整合 IronPDF 庫。 IronPDF 是一個功能強大且用途廣泛的程式庫,它使 .NET 開發人員能夠輕鬆建立、編輯以及最重要的程式庫——列印 PDF 文件。 讓我們一起來看看安裝過程:
建立 .NET Core 專案:開啟 Visual Studio 並選擇"建立新專案"。在專案範本選擇視窗中,在"所有平台"下篩選"Web",然後選擇"ASP.NET Core Web 應用程式"。
如何在 .NET Core 中列印 PDF 檔案:圖 1 - 選擇 ASP.NET Core Web 應用程式以建立新項目
安裝 IronPDF:前往"NuGet 套件管理器",搜尋"IronPDF"並將其安裝到您的專案中。 請確保 IronPDF 庫已正確安裝並在專案文件中引用。 你需要在程式碼中包含適當的using語句,例如using IronPdf;
在 ASP.NET Core 中建立基本 PDF 文檔
要在 ASP.NET Core Web 應用程式中使用 IronPDF 建立 PDF 文檔,首先需要在其中一個控制器中添加一些程式碼。 這裡有一個簡單的例子供您參考:
設定新控制器
在專案中建立一個新的控制器,該控制器將負責處理 PDF 建立請求。 例如,您可以將其命名為PdfController 。
編寫操作方法
在新控制器中,編寫一個名為CreatePdf操作方法,該方法傳回一個 PDF 檔案作為結果。
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
public class PdfController : Controller
{
// Action method for creating a PDF document
public IActionResult CreatePdf()
{
// Create a new PDF document from HTML content
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>");
// Save the generated PDF to the server's memory
var content = pdf.Stream.ToArray();
// Return the PDF to the browser as a downloadable file
return File(content, "application/pdf", "MyFirstPdf.pdf");
}
}
}using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
public class PdfController : Controller
{
// Action method for creating a PDF document
public IActionResult CreatePdf()
{
// Create a new PDF document from HTML content
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>");
// Save the generated PDF to the server's memory
var content = pdf.Stream.ToArray();
// Return the PDF to the browser as a downloadable file
return File(content, "application/pdf", "MyFirstPdf.pdf");
}
}
}運行您的應用程式
啟動應用程序,並導航至PdfController中的CreatePdf操作。 例如,如果您的應用程式在localhost的5000連接埠上執行,請造訪http://localhost:<Your-Port> /Pdf/CreatePdf在您的網頁瀏覽器中http://localhost:<Your-Port> /Pdf/CreatePdf 。
下載PDF文件
存取該網址後,PDF 文件將產生並透過您的網頁瀏覽器下載。 若要查看產生的 PDF 文件,您的電腦上需要安裝 PDF 檢視器。
使用 IronPDF 在 .NET Core 中列印 PDF 文檔
一旦你掌握了在 ASP.NET Core Web 應用程式中建立 PDF 文件的方法,下一步就是實作列印功能。 IronPDF 提供了一種簡單的方法,可以將專案中的 PDF 文件列印到應用程式運行所在的伺服器可存取的印表機。
配置預設印表機和印表機名稱
要列印 PDF 文檔,您需要在應用程式中設定印表機設定。 IronPDF 讓您可以依名稱指定印表機,可以是本機安裝的印表機,也可以是網路印表機。 此外,您還可以定義其他設置,例如紙張來源或方向。
以下是PdfController類別程式中的範例方法,該方法配置印表機設定並啟動列印作業:
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
public class PdfController : Controller
{
// Action method for printing a PDF document
public IActionResult PrintPdf()
{
// HTML string to be converted to PDF
var htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>";
// Render the HTML content to a PDF in memory
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Get the print document from the PDF
var printDoc = pdf.GetPrintDocument();
// Set the printer name (replace with your printer's actual name)
printDoc.PrinterSettings.PrinterName = "Your Printer Name";
// Optional: Configure additional printer settings
// e.g., printDoc.PrinterSettings.Copies = 2;
// e.g., printDoc.DefaultPageSettings.Landscape = true;
// Send the document to the printer
printDoc.Print();
// Return a confirmation response to the client
return Content("The document has been sent to the printer.");
}
}
}using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
public class PdfController : Controller
{
// Action method for printing a PDF document
public IActionResult PrintPdf()
{
// HTML string to be converted to PDF
var htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>";
// Render the HTML content to a PDF in memory
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Get the print document from the PDF
var printDoc = pdf.GetPrintDocument();
// Set the printer name (replace with your printer's actual name)
printDoc.PrinterSettings.PrinterName = "Your Printer Name";
// Optional: Configure additional printer settings
// e.g., printDoc.PrinterSettings.Copies = 2;
// e.g., printDoc.DefaultPageSettings.Landscape = true;
// Send the document to the printer
printDoc.Print();
// Return a confirmation response to the client
return Content("The document has been sent to the printer.");
}
}
}請記得將"您的印表機名稱"替換為您所在環境中印表機的實際名稱。 印表機應該能夠被執行 ASP.NET Core 應用程式的伺服器存取。 當你執行程式並造訪 URL" **https://localhost:<Your-Port> /Pdf/PrintPdf** ",您將看到輸出訊息,表示 PDF 已傳送到印表機。
結論
在本教學中,我們探討了 IronPDF 在 ASP.NET Core 應用程式環境中的功能和特性。 從使用 IronPDF 設定項目、建立和操作 PDF 文檔,到列印這些文檔所涉及的更複雜的流程,IronPDF 已被證明是 .NET Core 中處理 PDF 的強大而多功能的工具。
對於有興趣使用 IronPDF 的人來說,值得注意的是,該庫提供免費試用版,讓您可以在購買前評估其功能。 如果您覺得符合您的需求,IronPDF 授權起價為$799 ,可為小型和大型專案提供可擴展的解決方案。 下面您可以查看 IronXL 許可的價格,點擊這裡可以查看更多資訊。
常見問題解答
如何設定 .NET Core 項目以列印 PDF 檔案?
若要設定用於列印 PDF 的 .NET Core 項目,請在 Visual Studio 中建立新的 ASP.NET Core Web 項目,並透過 NuGet 套件管理器安裝 IronPDF。此設定可讓您使用 IronPDF 的 PDF 建立和列印功能。
在 .NET Core 列印 PDF 文件需要哪些步驟?
在 .NET Core 中列印 PDF 涉及使用 IronPDF 建立或載入 PDF 文檔,配置印表機設置,然後從應用程式執行列印命令將文件傳送到印表機。
如何在 ASP.NET Core 中將 HTML 內容轉換為 PDF?
您可以使用 IronPDF 的ChromePdfRenderer類別在 ASP.NET Core 中將 HTML 內容轉換為 PDF,該類別可以有效地將 HTML 字串或檔案渲染成 PDF 文件。
IronPDF 能否從 .NET Core 應用程式直接列印到印表機?
是的,IronPDF 可以直接從 .NET Core 應用程式列印到印表機。您需要在程式碼中設定印表機設置,並使用 IronPDF 的方法啟動列印作業。
列印PDF文件時可以配置哪些印表機設定?
使用 IronPDF 列印 PDF 時,您可以直接在應用程式程式碼中配置印表機名稱、份數、頁面方向和其他相關列印選項等設定。
購買前可以試用IronPDF嗎?
是的,您可以免費試用 IronPDF,在決定購買之前,您可以先體驗它的功能和功能。
IronPDF與哪些作業系統相容?
IronPDF 與包括 Windows、macOS、Android 和 iOS 在內的多種作業系統相容,使其成為跨平台開發的多功能解決方案。
如何排查在 .NET Core 中列印 PDF 時遇到的常見問題?
.NET Core 中列印 PDF 時常見的一些問題通常可以透過檢查印表機配置設定、確保 IronPDF 已正確安裝以及在列印前驗證文件格式來解決。








