如何在C语言中打印Word文档
要在 C# 中打印 Word 文档,可以使用IronWord创建文档,使用IronPDF将其转换为 PDF 格式,然后使用IronPrint处理打印过程,并可在多个平台上进行自定义设置。
在构建 C# 应用程序时,您经常需要以编程方式生成和打印 Word 文档。 无论您是创建报告、处理文档还是制作Professional成果,拥有可靠的工具都至关重要。 这时,Iron Software 的IronWord 、 IronPDF和IronPrint就派上了用场——这些库协同工作,简化了 C# 应用程序中的文档创建、转换和打印。
本文将指导您如何使用IronPrint进行打印,如何使用IronWord创建 Word 文档,以及如何如何使用IronPDF将其转换为 PDF。 无论您是构建Enterprise报告系统还是实现文档工作流程自动化,这些工具都能提供文档处理所需的一切。
How to Print a Word Document in C#?
- 创建一个Visual Studio项目
- 安装IronWord、IronPDF和IronPrint库
- 使用 IronWord 类创建 Word 文档
WordDocument - 使用
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)
打印机类是如何工作的?
Printer 类是 IronPrint 的核心。 它提供了打印各种文件类型(包括图像和 PDF)的方法。 您可以将其集成到任何打印场景中,它甚至支持实时应用程序的打印对话框。 ShowPrintDialog 方法可在用户需要时提供熟悉的打印配置选项。
IronPrint支持哪些平台?
IronPrint可在 Windows、macOS、Android 和 iOS 系统上运行,确保无论部署在哪里,都能提供一致的打印功能。 这种跨平台支持扩展到了WPF、Windows Forms 和ASP.NET应用程序。
需要哪些先决条件?
开始之前,请确保您已准备好:
如何创建、转换和打印 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 IronPdfInstall-Package IronWord Install-Package IronPdfSHELL
步骤3:使用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 - 创建一个
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")
输出 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")
过程很简单:
- 创建一个
DocxToPdfRenderer将 Word 文档渲染为 PDF - 保存结果
步骤5:使用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)
该代码:
- 获取带有
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
PRINT选项如何提升功能?
Printer 类可处理多种文件类型,包括 PDF、PNG、JPG、TIFF 和 BMP。这种多功能性意味着您可以打印不同的内容类型而无需改变您的方法。
我可以部署到哪些平台?
IronPrint 支持 Windows、Android、iOS 和 macOS 系统。 您的打印代码在所有平台上都能保持一致运行,使部署变得简单直接。
哪些打印设置可以自定义?
通过 PrintSettings 类,您可以控制:
- 纸张尺寸和方向
- DPI 和打印质量
- 副本与校对
- 页边距与版式
- 双面打印
- 自定义页面范围
IronPrint 如何与其他库集成?
IronPrint 可与 Iron Software 产品(如 IronBarcode 和 IronPDF)无缝协作。 一致的 API 设计使您能够在一个工作流中轻松创建、转换和 PRINT 文档。
为什么该 API 被认为易于使用?
IronPrint 直观的方法名称和完整的 IntelliSense 支持使其易于所有开发人员使用。 您无需经历陡峭的学习曲线,即可快速添加打印功能。
有哪些可用支持资源?
Iron Software 提供完整的文档、示例、API 参考和最佳实践。 他们的支持团队将协助您高效地实现PRINT功能。
IronPrint 如何提升对打印的控制力?
IronPrint 让您能够精确控制打印过程的每个环节。 请设置精确的纸张尺寸、页边距和参数,以确保输出结果符合特定要求。 监控打印机状态并处理错误,以实现可靠的打印任务管理。
下一步是什么?
现在,您已具备在 C# 应用程序中创建 WORD 文档、将其转换为 PDF 并进行 PRINT 所需的一切条件。 IronWord、IronPDF 和 IronPrint 协同工作,提供一套完整的文档处理解决方案。 无论您正在开发 Web、移动、桌面还是控制台应用程序,这些工具都能简化您的文档工作流程。
常见问题解答
如何在 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 进行最终打印过程。此组合确保高质量输出并易于使用。



