以下示例将介绍通过C#编程来打印Word文档的方法。示例将分2中情况来介绍,即:1. 通过对话框打印2. 静默打印
工具/原料
Spire.Doc for .NET
Visual Studio
dll引用
1、下载安装后,注意在编辑程序时,添加引用Spire.Doc.dll,dll文件在安装路径下的Bin文件夹中获取。
C#代码示例
1、通过对话框打印://初始化Document实例Document doc = new Document();//加载一个Wor颊俄岿髭d文档doc.LoadFromFile("sample.docx");//初始化PrintDialog实例PrintDialog dialog = new PrintDialog();//设置打印对话框属性dialog.AllowPrintToFile = true;dialog.AllowCurrentPage = true;dialog.AllowSomePages = true;//设置文档打印对话框doc.PrintDialog = dialog;//显示打印对话框并点击确定执行打印PrintDocument printDoc = doc.PrintDocument;if (dialog.ShowDialog() == DialogResult.OK){ printDoc.Print();}
2、静默打印://初始化Document实例Document doc = new Document();//加载一个Word文档doc.Loa颊俄岿髭dFromFile("sample.docx");//获取PrintDocument对象PrintDocument printDoc = doc.PrintDocument;//设置PrintController属性为StandardPrintController,用于隐藏打印进程printDoc.PrintController = new StandardPrintController();//打印文档printDoc.Print();