C # print a Word document

This article describes how to print a Word document via C # code program. By calling the Print dialog box (PrintDialog) related to the print settings, you can also print a Word document directly by the silent print mode.
Tools: Word library (Spire.Doc pack for the .NET)
Dll download and references : by downloading the official website pack package. Once downloaded, the program BIN folder under the specified path to the installation, the installation is complete open the Sample Center and see an example of a document API, etc., at the same time, under the installation path Add Spire.Doc.dll shall file in the Bin folder of references to VS program. Can also Nuget download installation.

[Example 1] Print dialog box

//初始化Document实例
Document doc = new Document();

//加载一个Word文档
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();
}

[Example 2] Printing muting

//初始化Document实例
Document doc = new Document();

//加载一个Word文档
doc.LoadFromFile("sample.docx");

//获取PrintDocument对象
PrintDocument printDoc = doc.PrintDocument;

//设置PrintController属性为StandardPrintController,用于隐藏打印进程
printDoc.PrintController = new StandardPrintController();

//打印文档
printDoc.Print();

(This article End)

Guess you like

Origin blog.51cto.com/eiceblue/2450282