
如何在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
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);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)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应用程序。
需要哪些先决条件?
开始之前,请确保您已准备好:
- 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 :
-
以相同的方式安装IronWord和IronPDF 。 对于控制台,请使用:
Install-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")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")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")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)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

如需更精细地控制文本、多页、灰度及 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!");
}' Asynchronous printing example
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' Asynchronous printing example
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 FunctionPRINT选项如何提升功能?
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、移动、桌面还是控制台应用程序,这些工具都能简化您的文档工作流程。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


