使用IRONPRINT

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

发布 2024年一月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 App"。

如何在 .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
    {
        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:<您的端口>/Pdf/CreatePdf

下载 PDF

访问 URL 后,将通过网络浏览器生成并下载 PDF 文档。如果要查看生成的 PDF 文件,则必须使用 PDF 查看器在计算机上阅读。

使用 IronPDF 在 .NET Core 中打印 PDF 文档

一旦您掌握了在 ASP.NET Core Web 应用程序中创建 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#

切记将 "您的打印机名称 "替换为您环境中打印机的实际名称。运行 ASP.NET Core 应用程序的服务器应该可以访问打印机。运行程序并访问以下 URL"***https://localhost:<Your-Port>/Pdf/PrintPdf**"时,您将看到以下信息:

如何在 .NET Core 中打印 PDF 文件:图 3 - 先前代码的输出信息

这意味着 PDF 已发送到打印机。

结论

通过本教程,我们探索了 IronPDF 在 ASP.NET Core 应用程序中的功能和能力。从使用 IronPDF 设置项目、创建和操作 PDF 文档,到打印这些文档所涉及的更复杂的过程,IronPDF 已被证明是 .NET Core 中处理 PDF 的强大而通用的工具。

对于有兴趣使用 IronPDF 的用户,值得注意的是该库提供了一个 免费试用,让您在使用前对其功能进行评估。如果您发现它符合您的需求,IronPDF 许可证的起价为 $749,可为小型和大型项目提供可扩展的解决方案。下面是 IronXL 许可证的定价,您可以点击 这里 查看更多信息。

如何在 .NET Core 中打印 PDF 文件:图 4 - IronPDF 许可页面

< 前一页
如何使用IronPDF从网络打印机打印PDF
下一步 >
使用 IronPDF 打印 PDF 文件

准备开始了吗? 版本: 2024.8 刚刚发布

免费NuGet下载 总下载量: 7,289 查看许可证 >