
如何在 .NET Core 中打印 PDF 文件
IronPrint 是 Iron Software 的全新 .NET 打印库,提供跨多种平台的兼容性,包括 Windows、macOS、Android 和 iOS。现在就开始使用 IronPrint!
.NET Core是一个由微软开发的开源跨平台框架,以其灵活性、性能和对基于云应用程序的支持而受到越来越多的欢迎。 然而,在处理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 应用程序"。

**安装 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运行您的应用程序
启动您的应用程序并导航到您的CreatePdf动作。 例如,如果您的应用程序运行在端口为http://localhost:<Your-Port>/Pdf/CreatePdf。
下载PDF
访问该URL后,PDF文档将通过您的Web浏览器生成并下载。 要查看生成的PDF,您需要在计算机上安装PDF查看器。
在.NET Core中使用IronPDF打印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.");
}
}
}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请记得将"Your Printer Name"替换为您环境中实际的打印机名称。 打印机应该可以被运行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 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


