使用 IRONPRINT 如何用 C# 打印 Word 文档 Curtis Chau 已更新:2025年10月16日 下载 IronPrint NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 要在 C# 中打印 Word 文档,可以使用 IronWord 创建文档,使用 IronPDF 将其转换为 PDF 格式,然后使用 IronPrint 处理打印过程,并可在多个平台上进行自定义设置。 在构建 C# 应用程序时,您经常需要以编程方式生成和打印 Word 文档。 无论您是创建报告、处理文档还是制作专业成果,拥有可靠的工具都至关重要。 这时,Iron Software 的IronWord 、 IronPDF和IronPrint就派上了用场——这些库协同工作,简化了 C# 应用程序中的文档创建、转换和打印。 本文将指导您如何使用 IronPrint 进行打印,如何使用 IronWord 创建 Word 文档,以及如何如何使用 IronPDF 将其转换为 PDF。 无论您是构建企业报告系统还是实现文档工作流程自动化,这些工具都能提供文档处理所需的一切。 如何在 C# 中打印 Word 文档? 创建一个Visual Studio项目 安装IronWord、IronPDF和IronPrint库 使用IronWord WordDocument类创建Word文档 使用SaveAs方法保存Word文档 使用IronPDF的DocxToPdfRenderer方法创建PDF文档 使用IronPrint调整PrinterSettings 使用IronPrint的Printer.Print方法打印 什么是IronPrint? IronPrint是一个高效的 .NET 打印库,它使你能够完全控制 C# 中的打印。 该软件由 Iron Software 开发,提供专门为打印任务设计的专用类和方法,让您可以微调打印过程的每个方面。 该库与.NET Framework 和 .NET Core都能流畅运行,因此您可以在任何类型的应用程序中使用它。 IronPrint的主要特点是什么? IronPrint 如何处理打印设置? IronPrint 让您可以自定义打印作业的各个方面: 纸张尺寸(Letter、Legal、A4、A3、自定义) 方向(竖屏或横屏) DPI 用于质量控制 带整理的副本数量 打印机选择和验证 精确测量的边距 灰度打印可节省成本 // Example: Advanced print settings configuration using IronPrint; // Create complete print settings PrintSettings advancedSettings = new PrintSettings() { PrinterName = "HP LaserJet Pro", PaperSize = PaperSize.A4, PrintOrientation = PrintOrientation.Portrait, Dpi = 600, // High quality print NumberOfCopies = 3, Grayscale = true, PaperMargins = new Margins(50, 50, 40, 40) // Left, Right, Top, Bottom }; // Apply settings to print job Printer.Print("document.pdf", advancedSettings); // Example: Advanced print settings configuration using IronPrint; // Create complete print settings PrintSettings advancedSettings = new PrintSettings() { PrinterName = "HP LaserJet Pro", PaperSize = PaperSize.A4, PrintOrientation = PrintOrientation.Portrait, Dpi = 600, // High quality print NumberOfCopies = 3, Grayscale = true, PaperMargins = new Margins(50, 50, 40, 40) // Left, Right, Top, Bottom }; // Apply settings to print job Printer.Print("document.pdf", advancedSettings); Imports IronPrint ' Example: Advanced print settings configuration ' Create complete print settings Dim advancedSettings As New PrintSettings() With { .PrinterName = "HP LaserJet Pro", .PaperSize = PaperSize.A4, .PrintOrientation = PrintOrientation.Portrait, .Dpi = 600, ' High quality print .NumberOfCopies = 3, .Grayscale = True, .PaperMargins = New Margins(50, 50, 40, 40) ' Left, Right, Top, Bottom } ' Apply settings to print job Printer.Print("document.pdf", advancedSettings) $vbLabelText $csharpLabel 打印机类是如何工作的? Printer类是 IronPrint 的核心。 它提供了打印各种文件类型(包括图像和 PDF)的方法。 您可以将其集成到任何打印场景中,它甚至支持实时应用程序的打印对话框。 ShowPrintDialog 方法可在用户需要时提供熟悉的打印配置选项。 IronPrint 支持哪些平台? IronPrint 可在 Windows、macOS、Android 和 iOS 系统上运行,确保无论部署在哪里,都能提供一致的打印功能。 这种跨平台支持扩展到了WPF、Windows Forms 和 ASP.NET 应用程序。 需要哪些先决条件? 开始之前,请确保您已准备好: Visual Studio:从官方网站下载并安装。 IronWord 库:用于创建和操作 Word 文件。 通过 NuGet 或IronWord安装。 IronPDF 库:用于 Word 转 PDF。 从IronPDF获取。 IronPrint 库:用于打印功能。 可在IronPrint购买。 如何创建、转换和打印 Word 文档? 让我们构建一个 C# 控制台应用程序,该应用程序创建一个 Word 文档,将其转换为 PDF,并使用这三个库将其打印出来。 步骤1:在Visual Studio中创建C#控制台应用程序 打开Visual Studio并创建一个新的C#控制台应用程序。 配置项目,然后单击"下一步"。 从"附加信息"中选择您的 .NET Framework,然后单击"创建"。 步骤2:通过NuGet包管理器安装必要的库 从"工具"菜单打开 NuGet 包管理器控制台。 在浏览选项卡中,搜索每个库并单击安装。 使用以下命令安装 IronPrint : Install-Package IronPrint 以相同的方式安装 IronWord 和 IronPDF。 对于主机,请使用: Install-Package IronWord Install-Package IronPdf Install-Package IronWord Install-Package IronPdf SHELL 步骤3:使用IronWord创建Word文档 我们先用IronWord创建一个简单的 Word 文档: using IronWord; using IronWord.Models; // Code to Create Word File // Create a TextRun object with sample text TextRun textRun = new TextRun("Sample text"); // Create a paragraph and add the TextRun to it Paragraph paragraph = new Paragraph(); paragraph.AddTextRun(textRun); // Create a Word document object with the paragraph and save it as a .docx file WordDocument doc = new WordDocument(paragraph); doc.SaveAs("assets/document.docx"); using IronWord; using IronWord.Models; // Code to Create Word File // Create a TextRun object with sample text TextRun textRun = new TextRun("Sample text"); // Create a paragraph and add the TextRun to it Paragraph paragraph = new Paragraph(); paragraph.AddTextRun(textRun); // Create a Word document object with the paragraph and save it as a .docx file WordDocument doc = new WordDocument(paragraph); doc.SaveAs("assets/document.docx"); Imports IronWord Imports IronWord.Models ' Code to Create Word File ' Create a TextRun object with sample text Private textRun As New TextRun("Sample text") ' Create a paragraph and add the TextRun to it Private paragraph As New Paragraph() paragraph.AddTextRun(textRun) ' Create a Word document object with the paragraph and save it as a .docx file Dim doc As New WordDocument(paragraph) doc.SaveAs("assets/document.docx") $vbLabelText $csharpLabel 事情经过是这样的: 我们使用文本创建一个TextRun 将其添加到Paragraph 创建并保存WordDocument 对于更复杂的文档,请添加格式、多个段落和表格: using IronWord; using IronWord.Models; // Create a more complex Word document WordDocument complexDoc = new WordDocument(); // Add a title paragraph with formatting TextRun titleRun = new TextRun("Quarterly Sales Report") { FontSize = 24, Bold = true, FontFamily = "Arial" }; Paragraph titleParagraph = new Paragraph(); titleParagraph.AddTextRun(titleRun); // Add body content TextRun bodyRun = new TextRun("This report contains sales data for Q4 2023."); Paragraph bodyParagraph = new Paragraph(); bodyParagraph.AddTextRun(bodyRun); // Add paragraphs to document complexDoc.AddParagraph(titleParagraph); complexDoc.AddParagraph(bodyParagraph); // Save the document complexDoc.SaveAs("assets/sales_report.docx"); using IronWord; using IronWord.Models; // Create a more complex Word document WordDocument complexDoc = new WordDocument(); // Add a title paragraph with formatting TextRun titleRun = new TextRun("Quarterly Sales Report") { FontSize = 24, Bold = true, FontFamily = "Arial" }; Paragraph titleParagraph = new Paragraph(); titleParagraph.AddTextRun(titleRun); // Add body content TextRun bodyRun = new TextRun("This report contains sales data for Q4 2023."); Paragraph bodyParagraph = new Paragraph(); bodyParagraph.AddTextRun(bodyRun); // Add paragraphs to document complexDoc.AddParagraph(titleParagraph); complexDoc.AddParagraph(bodyParagraph); // Save the document complexDoc.SaveAs("assets/sales_report.docx"); Imports IronWord Imports IronWord.Models ' Create a more complex Word document Dim complexDoc As New WordDocument() ' Add a title paragraph with formatting Dim titleRun As New TextRun("Quarterly Sales Report") With { .FontSize = 24, .Bold = True, .FontFamily = "Arial" } Dim titleParagraph As New Paragraph() titleParagraph.AddTextRun(titleRun) ' Add body content Dim bodyRun As New TextRun("This report contains sales data for Q4 2023.") Dim bodyParagraph As New Paragraph() bodyParagraph.AddTextRun(bodyRun) ' Add paragraphs to document complexDoc.AddParagraph(titleParagraph) complexDoc.AddParagraph(bodyParagraph) ' Save the document complexDoc.SaveAs("assets/sales_report.docx") $vbLabelText $csharpLabel 输出 Word 文档 步骤4:使用IronPDF将Word文档转换为PDF 现在让我们用IronPDF将 Word 文档转换为 PDF : using IronPdf; // Code to convert DOCX file to PDF using IronPDF // Create a DocxToPdfRenderer instance var renderer = new DocxToPdfRenderer(); // Render the DOCX document as a PDF var pdf = renderer.RenderDocxAsPdf("assets/document.docx"); // Save the resulting PDF pdf.SaveAs("assets/word.pdf"); using IronPdf; // Code to convert DOCX file to PDF using IronPDF // Create a DocxToPdfRenderer instance var renderer = new DocxToPdfRenderer(); // Render the DOCX document as a PDF var pdf = renderer.RenderDocxAsPdf("assets/document.docx"); // Save the resulting PDF pdf.SaveAs("assets/word.pdf"); Imports IronPdf ' Code to convert DOCX file to PDF using IronPDF ' Create a DocxToPdfRenderer instance Private renderer = New DocxToPdfRenderer() ' Render the DOCX document as a PDF Private pdf = renderer.RenderDocxAsPdf("assets/document.docx") ' Save the resulting PDF pdf.SaveAs("assets/word.pdf") $vbLabelText $csharpLabel 过程很简单: 创建DocxToPdfRenderer 将 Word 文档渲染为 PDF 保存结果 步骤5:使用IronPrint打印PDF 最后,让我们用IronPrint打印PDF 文件: using IronPrint; using System.Collections.Generic; // Code for Printing using IronPrint // Fetch printer names available in the system List<string> printerNames = Printer.GetPrinterNames(); // Configure print settings PrintSettings printerSettings = new PrintSettings(); foreach(string printerName in printerNames) { if(printerName.Equals("Microsoft Print to PDF")) { printerSettings.PrinterName = printerName; } } // Set paper size to A4 and configure margins printerSettings.PaperSize = PaperSize.A4; Margins margins = new Margins(30, 10); printerSettings.PaperMargins = margins; // Print the PDF with the specified settings Printer.Print("assets/word.pdf", printerSettings); using IronPrint; using System.Collections.Generic; // Code for Printing using IronPrint // Fetch printer names available in the system List<string> printerNames = Printer.GetPrinterNames(); // Configure print settings PrintSettings printerSettings = new PrintSettings(); foreach(string printerName in printerNames) { if(printerName.Equals("Microsoft Print to PDF")) { printerSettings.PrinterName = printerName; } } // Set paper size to A4 and configure margins printerSettings.PaperSize = PaperSize.A4; Margins margins = new Margins(30, 10); printerSettings.PaperMargins = margins; // Print the PDF with the specified settings Printer.Print("assets/word.pdf", printerSettings); Imports IronPrint Imports System.Collections.Generic ' Code for Printing using IronPrint ' Fetch printer names available in the system Private printerNames As List(Of String) = Printer.GetPrinterNames() ' Configure print settings Private printerSettings As New PrintSettings() For Each printerName As String In printerNames If printerName.Equals("Microsoft Print to PDF") Then printerSettings.PrinterName = printerName End If Next printerName ' Set paper size to A4 and configure margins printerSettings.PaperSize = PaperSize.A4 Dim margins As New Margins(30, 10) printerSettings.PaperMargins = margins ' Print the PDF with the specified settings Printer.Print("assets/word.pdf", printerSettings) $vbLabelText $csharpLabel 该代码: 使用Printer.GetPrinterNames()获取可用打印机 选择特定打印机 设置纸张尺寸和页边距。 打印PDF文件 打印预览显示转换后的 PDF 文档已准备好打印 - IronPrint 输出显示格式和边距正确的 word.pdf 文件 如需更好地控制副本、多页、灰度和 DPI,请查看这些代码示例。 您还可以启用打印机对话框以进行用户交互。 使用 IronPrint 进行印刷有哪些优势? 以下是IronPrint在C#打印任务中表现出色的原因: 为什么异步打印如此重要? IronPrint 提供异步功能,可防止打印操作阻塞您的应用程序。 在长时间打印作业期间,您的用户界面仍保持响应: // Asynchronous printing example using IronPrint; using System.Threading.Tasks; public async Task PrintDocumentAsync(string filePath) { PrintSettings settings = new PrintSettings { PrinterName = "Default Printer", NumberOfCopies = 2 }; // Non-blocking print operation await Printer.PrintAsync(filePath, settings); Console.WriteLine("Print job completed!"); } // Asynchronous printing example using IronPrint; using System.Threading.Tasks; public async Task PrintDocumentAsync(string filePath) { PrintSettings settings = new PrintSettings { PrinterName = "Default Printer", NumberOfCopies = 2 }; // Non-blocking print operation await Printer.PrintAsync(filePath, settings); Console.WriteLine("Print job completed!"); } Imports IronPrint Imports System.Threading.Tasks Public Async Function PrintDocumentAsync(filePath As String) As Task Dim settings As New PrintSettings With { .PrinterName = "Default Printer", .NumberOfCopies = 2 } ' Non-blocking print operation Await Printer.PrintAsync(filePath, settings) Console.WriteLine("Print job completed!") End Function $vbLabelText $csharpLabel 打印选项如何提升功能性? Printer类可以处理多种文件类型,包括 PDF、PNG、JPG、TIFF 和 BMP。这种多功能性意味着您可以打印不同类型的内容而无需改变您的方法。 我可以部署到哪些平台? IronPrint 可在 Windows、Android、iOS 和 macOS 上运行。 您的打印代码在所有平台上都能稳定运行,部署起来非常简单。 哪些打印设置可以自定义? 通过PrintSettings类,您可以控制: 纸张尺寸和方向 DPI 和打印质量 复印和整理 页边距和布局 双面打印 自定义页面范围 IronPrint 如何与其他库集成? IronPrint 可以与IronBarcode和 IronPDF 等其他 Iron Software 产品无缝协作。 一致的 API 设计使得在一个工作流程中轻松创建、转换和打印文档。 为什么说该 API 易于使用? IronPrint 直观的方法名称和完整的IntelliSense支持使其易于所有开发人员使用。 您可以快速添加打印功能,无需经历陡峭的学习曲线。 有哪些支持资源可用? Iron Software 提供完整的文档、示例、API 参考和最佳实践。 他们的支持团队可以帮助您有效地实现打印功能。 IronPrint 如何提高对打印的控制? IronPrint 让您能够精确控制打印的各个方面。 设置精确的纸张尺寸、边距和参数,以确保输出符合特定要求。 监控打印机状态并处理错误,以实现可靠的打印作业管理。 下一步是什么? 现在,您已拥有在 C# 应用程序中创建 Word 文档、将其转换为 PDF 并打印所需的一切。 IronWord 、 IronPDF和IronPrint协同工作,提供完整的文档处理解决方案。 无论您是构建 Web 应用程序、移动应用程序、桌面应用程序还是控制台应用程序,这些工具都能简化您的文档工作流程。 有关更多打印技巧,请访问文档页面。 探索批量打印和自定义打印处理器等功能,以提升应用程序的性能。 IronPrint许可证起价为$799 。 立即下载该库,为您的 C# 应用程序添加专业打印功能。 常见问题解答 如何在 C# 中打印不丢失格式的 Word 文档? 在 C# 中打印文档时保持格式,使用 IronWord 创建文档,使用 IronPDF 转换为 PDF,然后使用 IronPrint 打印。这确保了文档在整个过程中的格式得到保留。 在 C# 中使用 IronPrint 打印文档的好处是什么? IronPrint 提供异步打印、可自定义设置如纸张大小和方向、跨平台兼容性,并与 Iron Software 的其他库无缝集成,为 C# 环境中的打印任务提供了强大的解决方案。 如何将 IronWord、IronPDF 和 IronPrint 集成到 C# 项目中? 要将这些库集成到 C# 项目中,请在 Visual Studio 的 NuGet 包管理器控制台中安装它们。使用 Install-Package IronWord、Install-Package IronPDF 和 Install-Package IronPrint 添加 Word 创建、PDF 转换和打印的必要功能。 在使用 IronPrint 时可以自定义打印设置吗? 是的,IronPrint 允许您自定义各种打印设置,包括纸张大小、方向、DPI、副本数量、打印机名称、边距和灰度打印,让您完全掌控打印过程。 IronPrint 适合跨平台打印任务吗? IronPrint 专为跨平台支持而设计,可在 Windows、macOS、Android 和 iOS 上部署,使其对于各种开发环境都具有多样性。 在 C# 中创建和打印 Word 文档涉及哪些步骤? 首先,使用 IronWord 创建 Word 文档。接下来,使用 IronPDF 的 DocxToPdfRenderer 将其转换为 PDF。最后,使用 IronPrint 打印 PDF,确保文档的格式在整个过程中得到保持。 IronPrint 如何增强 C# 应用程序中的文档处理? IronPrint 通过提供全面的打印设置、异步打印以及与其他 Iron Software 库的无缝集成,提高了文档处理和打印的效率。 推荐的用于在 C# 中生成和打印文档的工具是什么? Iron Software 推荐使用 IronWord 进行文档创建,IronPDF 进行 PDF 转换,IronPrint 进行最终打印过程。此组合确保高质量输出并易于使用。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新2025年12月19日 使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多 已更新2025年10月19日 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多 已更新2025年10月29日 C# 以编程方式打印 PDF(代码示例教程) 在应用程序中,有多种用例需要打印到 PDF 文件的功能。 阅读更多 如何在 C# 中打印 QR 代码.NET PDF 打印机(开发人员...
已更新2025年12月19日 使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多
已更新2025年10月19日 如何使用 IronPrint 在 VB.NET 中打印 PDF VB.NET 打印 PDF 教程:在您的 .NET 应用程序中实现 PDF 打印。静默打印,对话框选项,自定义设置。 阅读更多