跳過到頁腳內容
使用 IRONPRINT

如何在 .NET Core 中打印 PDF 文件

.NET Core是由微軟開發的開源跨平台框架,因其靈活性、高效能和對雲端應用程式的支援而越來越受歡迎。 然而,在處理 PDF 文件時,特別是對於列印 PDF 文件等任務,開發人員需要一個強大且功能豐富的 PDF 庫。 這正是IronPDF能夠幫助開發者的地方。

IronPDF是一個專為.NET Framework(包括.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");
        }
    }
}
$vbLabelText   $csharpLabel

運行您的應用程式

啟動您的應用程序,並導航至您的 CreatePdf 操作。 例如,如果您的應用程式在 localhost 上執行,連接埠為 5000,請在您的 Web 瀏覽器中前往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.");
        }
    }
}
$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?

要設置一個 .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 並在打印之前驗證文檔格式來解決。

Curtis Chau
技術作家

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

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我