跳過到頁腳內容
使用 IRONPRINT

如何在 .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 文件

  1. 在 Visual Studio 中建立一個 ASP.NET Core Web 項目
  2. 使用 NuGet 套件管理器安裝 PDF 庫
  3. 在控制器中建立或載入 PDF 文檔
  4. 使用 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;

如何在 .NET Core 中列印 PDF 檔案:圖 2 - 使用 NuGet 瀏覽器尋找 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
$vbLabelText   $csharpLabel

運行您的應用程式

啟動應用程序,並導航至PdfController中的CreatePdf操作。 例如,如果您的應用程式在localhost5000連接埠上執行,請造訪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.");
        }
    }
}
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
$vbLabelText   $csharpLabel

請記得將"您的印表機名稱"替換為您所在環境中印表機的實際名稱。 印表機應該能夠被執行 ASP.NET Core 應用程式的伺服器存取。 當你執行程式並造訪 URL" **https://localhost:<Your-Port> /Pdf/PrintPdf** ",您將看到輸出訊息,表示 PDF 已傳送到印表機。

如何在 .NET Core 中列印 PDF 檔案:圖 3 - 前一段程式碼的輸出訊息

結論

在本教學中,我們探討了 IronPDF 在 ASP.NET Core 應用程式環境中的功能和特性。 從使用 IronPDF 設定項目、建立和操作 PDF 文檔,到列印這些文檔所涉及的更複雜的流程,IronPDF 已被證明是 .NET Core 中處理 PDF 的強大而多功能的工具。

對於有興趣使用 IronPDF 的人來說,值得注意的是,該庫提供免費試用版,讓您可以在購買前評估其功能。 如果您覺得符合您的需求,IronPDF 授權起價為$799 ,可為小型和大型專案提供可擴展的解決方案。 下面您可以查看 IronXL 許可的價格,點擊這裡可以查看更多資訊。

如何在 .NET Core 列印 PDF 檔案:圖 4 - IronPDF 許可頁面

常見問題解答

如何設定 .NET Core 專案以列印 PDF?

要為列印 PDF 設定 .NET Core 專案,請在 Visual Studio 中建立新的 ASP.NET Core Web 專案,並透過 NuGet Package Manager 安裝 IronPDF。此設定可讓您利用 IronPDF 的功能來建立和列印 PDF。

在 .NET Core 中列印 PDF 文件涉及哪些步驟?

在 .NET Core 中打印 PDF 涉及使用 IronPDF 創建或載入 PDF 文檔、配置印表機設定,以及從應用程式執行列印指令以將文檔傳送至印表機。

如何在 ASP.NET Core 中將 HTML 內容轉換為 PDF?

您可以在 ASP.NET Core 中使用 IronPDF 的 ChromePdfRenderer 類將 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 已正確安裝,以及在列印前驗證文件格式來解決。

Curtis Chau
技術作家

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

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