使用 IRONPRINT 如何在 .NET Core 中打印 PDF 文件 Curtis Chau 已更新:七月 28, 2025 下载 IronPrint NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 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语句,例如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"); } } } 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动作。 例如,如果您的应用程序在localhost上运行,端口为5000,请在您的网页浏览器中访问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."); } } } 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 请记得将"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许可从$799开始,为小型和大型项目提供可扩展的解决方案。 下面您可以看到IronXL许可的价格,您可以点击这里查看更多。 常见问题解答 如何设置 .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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十二月 18, 2025 使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多 已发布十月 19, 2025 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多 已更新八月 3, 2025 C# 以编程方式打印 PDF(代码示例教程) 在应用程序中,有多种用例需要打印到 PDF 文件的功能。 阅读更多 如何使用 IronPDF 从网络打印机打印 PDFC# 使用 IronPDF 打印 PDF 文件
已发布十二月 18, 2025 使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多
已发布十月 19, 2025 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多