Word文档是日常办公和学习中不可或缺的一部分。比如在商务往来中,经常需要打印 Word 文档用于撰写和传递正式的商务信函、合作协议、项目提案等。打印出来的文档便于双方签字盖章,具有法律效力和正式性。本文将提供以下4种通过C# 打印Word文档的方法,以适应不同的场景需求。
目录
C# 通过物理打印机打印Word文档
C# 静默打印 Word 文档
C# 通过虚拟打印机将 Word 转为 PDF
C# 在一张纸上打印多个页面
免费 .NET Word库 - Free Spire.Doc for .NET。要实现通过C#打印Word文档,我们需要安装该免费库(有页数限制)。可以直接在Visual Studio中通过NuGet搜索 “FreeSpire.Doc”,然后点击 “安装” 将其引用到程序中。或者通过该链接下载产品包,解压后再手动将dll文件添加引用至程序。
Downloads - Free Spire.XLSDownload free .NET/Wpf Excel library to read, create, manipulate, convert & print Microsoft Excel documents.https://www.e-iceblue.com/Download/download-excel-for-net-free.html
C# 通过物理打印机打印Word文档
通过免费.NET库提供的 PrintDocument 类,我们可以在指定的打印机上打印 Word 文档,还可以指定设置打印选项,如要打印的页面范围、份数、和纸张大小等。
C# 代码:
using Spire.Doc;
using System.Drawing.Printing;namespace PrintWordDocument
{class Program{static void Main(string[] args){// 加载Word文档Document doc = new Document();doc.LoadFromFile("示例.docx");// 获取 PrintDocument 对象PrintDocument printDoc = doc.PrintDocument;// 指定打印机名称printDoc.PrinterSettings.PrinterName = "打印机名";// 指定要打印的页面范围printDoc.PrinterSettings.FromPage = 1;printDoc.PrinterSettings.ToPage = 10;// 设置打印份数printDoc.PrinterSettings.Copies = 1;// 指定纸张大小printDoc.DefaultPageSettings.PaperSize = new PaperSize("custom", 500, 800);// 打印文档printDoc.Print();}}
}
C# 静默打印 Word 文档
静默打印是指在打印过程中不弹出打印对话框,直接通过程序或系统设置自动完成打印操作。使用Free Spire.Doc,我们可以将 PrintController 属性设置为 StandardPrintController,用于隐藏打印进程,从而实现静默打印。
C# 代码:
using Spire.Doc;
using System.Drawing.Printing;namespace SilentlyPrintWord
{class Program{static void Main(string[] args){// 加载Word文档Document doc = new Document();doc.LoadFromFile("示例.docx");// 获取 PrintDocument 对象PrintDocument printDoc = doc.PrintDocument;// 指定打印机名称printDoc.PrinterSettings.PrinterName = "打印机名";// 将PrintController属性设置为StandardPrintController,用于隐藏打印进程printDoc.PrintController = new StandardPrintController();// 打印文档printDoc.Print();}}
}
注意:静默打印时无法实时确认打印效果和参数设置,如果打印机出现故障或打印参数设置错误,可能会导致打印失败或结果不符合预期。所以在正式使用静默打印前,建议先进行测试,确保打印设置和打印机状态正常。
C# 通过虚拟打印机将 Word 转为 PDF
虚拟打印机是一种软件,能模拟实现打印机的功能,但并不涉及实际的纸张和墨水消耗,而是将电子文档转换为特定格式的电子文件保存在电脑上。除了物理打印机外,Free Spire.Doc也支持使用虚拟打印机。
C# 代码:
using Spire.Doc;
using System.Drawing.Printing;namespace PrintWordToPdf
{class Program{static void Main(string[] args){// 加载Word文档Document doc = new Document();doc.LoadFromFile("示例.docx");// 获取 PrintDocument 对象PrintDocument printDoc = doc.PrintDocument;// 打印输出到文件printDoc.PrinterSettings.PrintToFile = true;// 指定虚拟打印机名称printDoc.PrinterSettings.PrinterName = "Microsoft Print to PDF";// 指定输出文件路径和名称printDoc.PrinterSettings.PrintFileName = @"C:\Users\Administrator\Desktop\ToPDF.pdf";// 打印文档printDoc.Print();}}
}
C# 在一张纸上打印多个页面
将多个相关的 Word 文档页面打印在一张纸上,以便于对比查看和整理资料,提高工作效率,减少纸张浪费。通过使用 PrintMultipageToOneSheet() 方法可以实现该操作。
C# 代码:
using Spire.Doc;
using Spire.Doc.Printing;
using System.Drawing.Printing;namespace PrintMultiplePagesOnOneSheet
{internal class Program{static void Main(string[] args){// 加载Word文档Document doc = new Document();doc.LoadFromFile("示例.docx");// 获取 PrintDocument 对象PrintDocument printDoc = doc.PrintDocument;// 启用单面打印printDoc.PrinterSettings.Duplex = Duplex.Simplex;// 将指定的页数打印到一页上doc.PrintMultipageToOneSheet(PagesPreSheet.TwoPages, false);}}
}