使用 IRONPRINT

如何在 .NET Core 中列印 PDF 檔案

發佈 2024年1月14日
分享:

介紹

.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網頁應用程序中使用IronPDF創建PDF文件,您將首先需要在其中一個控制器中添加一些代碼。這是一個簡單的示例,幫助您開始:

設置新控制器

在您的專案中創建一個新的控制器,該控制器將負責處理PDF創建請求。您可以將其命名為 PdfController

編寫操作方法

在您的新控制器中,編寫一個名為 CreatePdf 的操作方法,該方法返回一個 PDF 文件作為結果。

using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        public IActionResult CreatePdf()
        {
            // Create a new PDF document
            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 PDF to the server's memory
            var content = pdf.Stream.ToArray();
            // Return the PDF to the browser as a file download
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        public IActionResult CreatePdf()
        {
            // Create a new PDF document
            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 PDF to the server's memory
            var content = pdf.Stream.ToArray();
            // Return the PDF to the browser as a file download
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		Public Function CreatePdf() As IActionResult
			' Create a new PDF document
			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 PDF to the server's memory
			Dim content = pdf.Stream.ToArray()
			' Return the PDF to the browser as a file download
			Return File(content, "application/pdf", "MyFirstPdf.pdf")
		End Function
	End Class
End Namespace
VB   C#

運行您的應用程式

啟動您的應用程式,並導航到 PdfController 中的 CreatePdf 操作。例如,如果您的應用程式在端口 5000localhost 上運行,請在您的網絡瀏覽器中訪問 http://localhost:<Your-Port>/Pdf/CreatePdf

下載 PDF

當訪問 URL 時,PDF 文件會通過您的網頁瀏覽器生成並下載。如果您想查看生成的 PDF,必須使用 PDF 瀏覽器在您的電腦上閱讀它。

在 .NET Core 中使用 IronPDF 列印 PDF 文件

在你掌握了在 ASP.NET Core 網頁應用程式中生成 PDF 文件之後,下一步就是實現列印功能。IronPDF 提供了一種簡單的方法,讓你的專案能夠將 PDF 文檔列印到應用程式運行的伺服器可存取的印表機上。

配置默認打印機和打印機名稱

要打印 PDF 文件,您需要在應用程序中配置打印機設置。IronPDF 允許您根據打印機的名稱指定打印機,該打印機可以是本地安裝的打印機或網絡打印機。此外,您還可以定義其他設置,例如紙張來源或方向。

以下是一個在您的 PdfController 類程式中配置打印機設置並啟動打印作業的方法範例:

using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult PrintPdf()
    {
        // Assuming 'htmlContent' is the HTML string you want to print
        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 PrintDocument from the PDF
        var printDoc = pdf.GetPrintDocument();
        // Set the printer name
        printDoc.PrinterSettings.PrinterName = "Your Printer Name"; // Replace with your printer's name
        // Optional: Set other printer settings
        // printDoc.PrinterSettings.Copies = 2;
        // printDoc.DefaultPageSettings.Landscape = true;
        // Print the document
        printDoc.Print();
        // Return a response to the client, e.g., a confirmation message
        return Content("The document has been sent to the printer.");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult PrintPdf()
    {
        // Assuming 'htmlContent' is the HTML string you want to print
        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 PrintDocument from the PDF
        var printDoc = pdf.GetPrintDocument();
        // Set the printer name
        printDoc.PrinterSettings.PrinterName = "Your Printer Name"; // Replace with your printer's name
        // Optional: Set other printer settings
        // printDoc.PrinterSettings.Copies = 2;
        // printDoc.DefaultPageSettings.Landscape = true;
        // Print the document
        printDoc.Print();
        // Return a response to the client, e.g., a confirmation message
        return Content("The document has been sent to the printer.");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Public Class PdfController
	Inherits Controller

	Public Function PrintPdf() As IActionResult
		' Assuming 'htmlContent' is the HTML string you want to print
		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 PrintDocument from the PDF
		Dim printDoc = pdf.GetPrintDocument()
		' Set the printer name
		printDoc.PrinterSettings.PrinterName = "Your Printer Name" ' Replace with your printer's name
		' Optional: Set other printer settings
		' printDoc.PrinterSettings.Copies = 2;
		' printDoc.DefaultPageSettings.Landscape = true;
		' Print the document
		printDoc.Print()
		' Return a response to the client, e.g., a confirmation message
		Return Content("The document has been sent to the printer.")
	End Function
End Class
VB   C#

記得將「Your Printer Name」替換為您環境中打印機的實際名稱。打印機應能夠被運行ASP.NET Core應用程序的服務器訪問。當您運行程序並前往以下URL "**https://localhost:<Your-Port>/Pdf/PrintPdf**"時,您將看到以下消息:

在 .NET Core 中列印 PDF 文件:圖 3 - 來自上一段程式碼的輸出訊息

這表示 PDF 被送到打印機。

結論

在本教程中,我們探討了在ASP.NET Core應用程式中IronPDF的功能和能力。從使用IronPDF設定專案、創建和操作PDF文件,到涉及打印這些文件的更複雜過程,IronPDF已證明是處理.NET Core中PDF的強大且多功能的工具。

對於有意使用IronPDF的人員,值得注意的是該庫提供了一個 免費試用讓你在承諾之前評估其功能。如果你覺得適合您的需求,IronPDF 授權費用從 $749 開始,提供適用於小型和大型專案的可擴展解決方案。下方可以看到 IronXL 授權的價格,您可以點選 這裡 查看更多。

如何在 .NET Core 中列印 PDF 檔案:圖 4 - IronPDF 授權頁面

< 上一頁
如何使用IronPDF從網路打印機列印PDF
下一個 >
使用 IronPDF 列印 PDF 檔案的 C#代碼

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 9,531 查看許可證 >