
如何在 .NET Core 中列印 PDF 文件
IronPrint 是 Iron Software 全新的 .NET 列印程式庫,提供跨多種平台的相容性,包括 Windows、macOS、Android 和 iOS。開始使用 IronPrint 現在!
.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");
}
}
}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.");
}
}
}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授權的定價,並且您可以點擊這裡查看更多。

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

