C# 打印 word pdf PrintOut方法

//要打印的文件路径
            object wordFile = lstDoc.SelectedValue.ToString();//@"c:\test.doc";
            object oMissing = Missing.Value;

            //自定义object类型的布尔值
            object oTrue = true;
            object oFalse = false;
            object Copies = nudCopies.Value; //打印份数
            object wdPrintFrom = txtPage1.Text;//打印的起始页码
            object wdPrintTo = txtPage2.Text;//打印的结束页码

            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

            //定义WORD Application相关
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();

            //WORD程序不可见
            appWord.Visible = false;
            //不弹出警告框
            appWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

            //先保存默认的打印机
            string defaultPrinter = appWord.ActivePrinter;

            //打开要打印的文件
            Microsoft.Office.Interop.Word.Document doc = appWord.Documents.Open(
                ref wordFile,
                ref oMissing,
                ref oTrue,
                ref oFalse,
                ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing,ref oMissing,ref oMissing, ref oMissing);

            //设置指定的打印机
            appWord.ActivePrinter = cboInstalledPrinters.SelectedText; //"\\\\10.10.96.236\\HP Deskjet 6500 Series";

            
//打印
            doc.PrintOut(
                ref oTrue, //Background 此处为true,表示后台打印
                ref oFalse,
                ref oMissing, //Range 页面范围
                ref oMissing,
                ref wdPrintFrom, //当 Range 设置为 wdPrintFromTo 时的起始页码
                ref wdPrintTo,//当 Range 设置为 wdPrintFromTo 时的结束页码
                ref oMissing,
                ref Copies,  //要打印的份数
                ref oMissing,ref oMissing, ref oMissing , ref oMissing, ref oMissing,ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing);

            lblMessage.Text = "正在打印,请稍候!";

            //打印完关闭WORD文件
            doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);

            //还原原来的默认打印机
            appWord.ActivePrinter = defaultPrinter;

            //退出WORD程序
            appWord.Quit(ref oMissing, ref oMissing, ref oMissing);

            doc = null;
            appWord = null;

            lblMessage.Text = "打印成功!";

参数

Background

如果为 true,则可以让自定义项代码在 Microsoft Office Word 打印文档时继续工作。

Append

如果为 true,则会将文档追加到 OutputFileName 参数指定的文件;如果为 false,则会改写 OutputFileName 的内容。

Range

页面范围。可以是任何 WdPrintOutRange 值。

OutputFileName

如果 PrintToFile 为 true,则此参数指定输出文件的路径和文件名。

From

当 Range 设置为 wdPrintFromTo 时的起始页码。

To

当 Range 设置为 wdPrintFromTo 时的结束页码。

Item

要打印的项。可以是任何 WdPrintOutItem 值。

Copies

要打印的份数。

Pages

要打印的页码和页码范围,由逗号分隔。例如,“2, 6-10”意为打印第 2 页和第 6、7、8、9、10 页。

PageType

要打印的页面的类型。可以是任何 WdPrintOutPages 值。

PrintToFile

如果为 true,则将打印机指令发送到文件。请确保使用 OutputFileName 指定一个文件名。

Collate

在打印多份文档时,如果为 true,则先打印该文档的所有页,然后再打印下一份。

ActivePrinterMacGX

此参数仅在 Microsoft Office Macintosh Edition 中可用。有关此参数的其他信息,请查询 Microsoft Office Macintosh Edition 附带的语言参考帮助。

ManualDuplexPrint

如果为 true,则在没有双面打印装置的打印机上打印双面文档。如果此参数为 true,则忽略 PrintBackgroundPrintReverse 属性。使用 PrintOddPagesInAscendingOrderPrintEvenPagesInAscendingOrder 属性在手动进行双面打印时控制输出。您可能无法使用此参数,具体取决于您选择或安装的语言支持(例如,美国英语)。

PrintZoomColumn

希望 Word 在一页上水平布置的页数。可以为 1、2、3 或 4。与 PrintZoomRow 参数一起使用时可在单张纸上打印多页。

PrintZoomRow

希望 Word 在一页上垂直布置的页数。可以为 1、2 或 4。与 PrintZoomColumn 参数一起使用时可在单张纸上打印多页。

PrintZoomPaperWidth

希望 Word 将打印页缩放到的宽度(以缇表示,20 缇 = 1 磅,72 磅 = 1 英寸)。

PrintZoomPaperHeight

希望 Word 将打印页缩放到的高度(以缇表示,20 缇 = 1 磅,72 磅 = 1 英寸)。

备注

原文地址:https://www.cnblogs.com/Snowfun/p/2880160.html