如何在 C# 中打印 Word 文档
在不断发展的软件开发领域中,能够以编程方式生成和打印Word文档是一项基本要求。 C#开发者经常会遇到需要生成和打印Word文档的场景,这对于报告生成、文档处理或创建专业效果的输出等任务来说是必不可少的。 为了解决这一需求,Iron Software推出了IronWord、IronPDF和IronPrint,这些强大的库旨在简化在C#应用程序中创建、操作和打印Word和PDF文档的流程。
本文将探讨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由Iron Software开发,是一个强大而灵活的.NET打印库,提供了一系列用于处理C#打印任务的工具。 它以专用的类和方法为特色,专门用于打印相关功能,为开发人员提供对打印过程和打印机设置的细粒度控制。
IronPrint的关键特性
1. 全面的打印设置
IronPrint使开发人员能够定制打印过程的各个方面。 这包括:
- 纸张尺寸
- 方向(纵向或横向)
- DPI(每英寸的点数)
- 复印数量
- 打印机名称
- 边距(上、下、左、右)
- 灰度打印
2. 使用Printer类的多功能打印
IronPrint的一大亮点功能是引入了Printer类。 该类提供了一整套用于打印各种文件类型的方法,包括图像和PDF文档。 Printer类的多功能性允许在不同的打印场景中无缝集成。 它还允许在实时应用程序中打印时显示打印对话框,从而为Word文档的打印提供更细粒度的控制。
3. 跨平台支持
IronPrint具有跨平台兼容性,使其适合在多种环境中部署。 无论您的应用程序运行在Windows、macOS、Android还是iOS上,IronPrint都能保证一致和可靠的打印功能。
前提条件
在进行实施之前,请确保您具备以下先决条件:
创建、转换和打印Word文档的步骤
请按照这些步骤设置C#控制台应用程序,创建Word文档对象,将其转换为PDF,最后分别使用IronWord、IronPDF和IronPrint进行打印。
步骤1:在Visual Studio中创建C#控制台应用程序
- 打开Visual Studio并创建一个新的C#控制台应用程序。
- 按照以下步骤配置项目,然后单击"下一步"。
- 从附加信息中选择.NET Framework,并单击"创建"。
步骤2:通过NuGet包管理器安装必要的库
- 在Visual Studio项目中使用工具菜单或解决方案资源管理器打开NuGet包管理器控制台或NuGet包管理器。
- 在NuGet的浏览选项卡中搜索库并点击安装。
安装IronPrint打印库:
使用NuGet包管理器控制台,使用以下命令:
Install-Package IronPrint
使用解决方案的NuGet包管理器同样安装IronWord和IronPDF库。 要使用NuGet包管理器控制台安装IronWord和IronPDF,请使用以下命令:
Install-Package IronWord Install-Package IronPdfInstall-Package IronWord Install-Package IronPdfSHELL
步骤3:使用IronWord创建Word文档
让我们开始创建一个简单的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")在此代码中:
- 我们创建一个包含示例文本的
TextRun。 - 创建
Paragraph并将TextRun添加到其中。 - 最后,使用段落创建
WordDocument并将新文档保存为"document.docx"。
输出 Word 文档

步骤4:使用IronPDF将Word文档转换为PDF
一旦我们有了Word文档,可能需要将其转换为PDF格式。 IronPDF简化了这一过程,实现无缝转换。 以下是代码片段:
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")在此代码中:
- 我们使用DocxToPdfRenderer()方法将Word文档渲染为PDF。
- 得到的PDF保存为"word.pdf"。
步骤5:使用IronPrint打印PDF
可以使用IronPrint打印PDF,提供打印设置的灵活性和控制。 如果没有设置打印设置,则使用默认设置进行打印。 以下代码演示了如何打印生成的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)在此代码中:
- 我们使用
Printer.GetPrinterNames()获取可用的打印机名称。 - 设置特定的打印机名称(在本例中为"Microsoft Print to PDF")。 如果物理打印机不可用,则默认情况下IronPrint将其视为默认打印机。
- 配置打印设置,指定纸张尺寸为A4并设置边距。
- 最后使用
Printer.Print()方法打印PDF。

如果您希望控制NumberOfCopies、打印多页、Grayscale和DPI,请访问此代码示例页面。 您还可以启用打印机对话框并防止文档的静默打印。
IronPrint在C#中打印的优势
以下是一些使用IronPrint在C#控制台或Windows窗体应用程序中进行打印作业的主要优点:
1. 异步打印
IronPrint提供异步函数,允许异步执行打印操作。 这防止了打印操作阻塞线程,从而提高应用程序的性能和响应能力。
2. 多功能的打印选项
IronPrint中的专用Printer类提供了一整套方法,用于打印包括图像和PDF文档在内的各种文件类型。 这种多功能性提供了超越标准打印的灵活性,使开发人员能够轻松处理不同类型的内容。
3. 跨平台支持
IronPrint支持跨多个平台打印,包括Windows、Android、iOS和macOS。 这种跨平台兼容性使其适用于各种应用程序环境,确保您的打印功能可以在不同的操作系统上部署。
4. 可自定义的打印设置
IronPrint允许开发人员对打印设置进行精细控制,提供高度的定制化能力。 开发人员可以通过PrintSettings类指定打印过程的各个方面,如纸张尺寸、方向、DPI、复印数量、打印机名称、边距和灰度打印。
5. 与IronQR和IronPDF的无缝集成
IronPrint与其他Iron Software库(如IronQR和IronPDF)无缝集成。 这种集成允许开发人员在统一而高效的工作流程中创建、转换和打印二维码、PDF和其他文档。
6. 用户友好的API
IronPrint提供用户友好的API,简化C#应用中的打印功能实现。 开发人员可以快速将条形码和打印功能添加到他们的项目中,从而减少开发时间和精力。
7. 全面的文档和支持
IronPrint得到了Iron Software提供的全面文档和支持的支持。 这确保了开发人员在实施打印功能时能获得资源和协助,便于排查问题和优化打印过程。
8. 增强的打印过程控制
使用IronPrint,开发人员可以增强对打印过程的控制。 诸如设置纸张大小、边距和其他打印参数的能力提供了精准的控制,确保打印输出满足特定要求和标准。
结论
通过遵循这些步骤,您可以在C#应用中无缝集成Word文档处理、转换为PDF和打印功能。 IronWord、IronPDF和IronPrint共同为希望加强文档相关任务的开发人员提供了强有力的工具包。 无论您是在从事Web、移动、桌面还是控制台应用程序工作,本指南都是有效利用这些库在个人.NET项目中的全面资源。
有关如何高效打印的更多信息,请访问此文档页面。
常见问题解答
如何在 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 进行最终打印过程。此组合确保高质量输出并易于使用。









