如何在 .NET Core 中列印 PDF 文件
.NET Core是由Microsoft開發的開源跨平台框架,以其靈活性、性能和支持雲端應用程式而受歡迎。 然而,當涉及處理PDF檔案,特別是執行例如列印PDF文件的任務時,開發者需要一個強大且功能豐富的PDF程式庫。 這就是IronPDF幫助開發者的地方。
IronPDF是一個專為.NET框架設計的綜合程式庫,包含.NET Core和ASP.NET Core,這簡化了處理PDF文件的過程。 它不僅允許建立和操作PDF檔案,還提供了一種無縫的方式來列印這些文件,無論是直接列印到列印機或將其轉換為適合列印的格式。
在本教程中,我們將深入研究在.NET Core環境中的IronPDF的功能。 從設置您的專案、建立您的第一個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 App"。

安裝IronPDF:前往"NuGet套件管理器"並搜尋"IronPDF"以將其安裝到您的專案中。 確保IronPDF程式庫已正確安裝並在您的專案檔案中被引用。 您需要在程式碼中包含適當的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");
}
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace YourProjectName.Controllers
Public Class PdfController
Inherits Controller
' Action method for creating a PDF document
Public Function CreatePdf() As IActionResult
' Create a new PDF document from HTML content
Dim renderer = New ChromePdfRenderer()
Dim 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
Dim content = pdf.Stream.ToArray()
' Return the PDF to the browser as a downloadable file
Return File(content, "application/pdf", "MyFirstPdf.pdf")
End Function
End Class
End Namespace
運行您的應用程式
啟動您的應用程式並導航到PdfController中。 例如,假設您的應用程式運行在http://localhost:<Your-Port>/Pdf/CreatePdf。
下載PDF
存取該URL後,將通過您的網頁瀏覽器生成並下載PDF文件。 要查看生成的PDF,您需要在計算機上安裝PDF查看器。
使用IronPDF在.NET Core中列印PDF文件
一旦您掌握了在您的ASP.NET Core Web App中建立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.");
}
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace YourProjectName.Controllers
Public Class PdfController
Inherits Controller
' Action method for printing a PDF document
Public Function PrintPdf() As IActionResult
' HTML string to be converted to PDF
Dim htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>"
' Render the HTML content to a PDF in memory
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
' Get the print document from the PDF
Dim 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.")
End Function
End Class
End Namespace
請記得將"您的列印機名稱"替換為您環境中實際的列印機名稱。 列印機應該對運行ASP.NET Core應用程式的伺服器是可存取的。 當您運行程式並轉至URL "**https://localhost:<Your-Port>/Pdf/PrintPdf**" 時,您將看到輸出消息,指出PDF已發送至列印機。

結論
在本教程中,我們探討了在ASP.NET Core應用程式中,IronPDF的功能和能力。 從設置您的專案與IronPDF,到建立和操作PDF文件,再到更複雜的列印過程,IronPDF證明是一個處理.NET Core中PDF的強大且多功能的工具。
對於那些有興趣使用IronPDF的使用者,需注意該程式庫提供免費試用,允許您在承諾之前評估其功能。 如果您覺得它適合您的需求,IronPDF授權從$999開始,為小型和大型項目提供可擴展的解決方案。 下方您可以看到IronXL授權的定價,並且您可以點擊這裡查看更多。

常見問題
如何設置.NET Core專案以列印PDF?
要設置.NET Core專案以列印PDF,請在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,以及在列印前驗證文件格式來解決。


