C#获取word文档页数,打印word 文档(写成了三个函数 )

本文介绍了如何使用C#进行Word文档操作,包括通过MSWord库获取Word文档的页数并展示在消息框中,以及提供两种不同的方法实现无界面打印Word文档。示例代码详细展示了如何打开文档、计算页数、设置打印选项和关闭文档等步骤。
摘要由CSDN通过智能技术生成

       private void getWordPageNumber()

        {

            wordApp = new MSWord.ApplicationClass();

            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            // 打开Word

            object FileName = "C:\\Users\\syl\\Downloads\\test.doc";

            object readOnly = true;

            object isVisible = false;

            wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly,

            ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,

            ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing,

            ref Nothing, ref Nothing, ref Nothing);

 

            // 计算Word文档页数

            MSWord.WdStatistic stat = MSWord.WdStatistic.wdStatisticPages;

            int num = wordDoc.ComputeStatistics(stat, ref  Nothing);

            MessageBox.Show(num.ToString());

        }

 

        private void onePrint()

        {

            System.Diagnostics.Process p = new System.Diagnostics.Process();

            //不现实调用程序窗口,但是对于某些应用无效

            p.StartInfo.CreateNoWindow = true;

            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

 

            //采用操作系统自动识别的模式

            p.StartInfo.UseShellExecute = true;

 

            //要打印的文件路径,可以是WORD,EXCEL,PDF,TXT等等

            p.StartInfo.FileName = @"H:\test\test_print.docx";

 

            //指定执行的动作,是打印,即print,打开是 open

            p.StartInfo.Verb = "print";

 

            //开始

            p.Start();

        }

 

        private void anotherPrint()

        {

            //要打印的文件路径

            object wordFile = @"H:\test\test_print_3.doc";

 

            object oMissing = Missing.Value;

 

            //自定义object类型的布尔值

            object oTrue = true;

            object oFalse = false;

 

            object doNotSaveChanges = MSWord.WdSaveOptions.wdDoNotSaveChanges;

 

            //定义WORD Application相关

            MSWord.Application appWord = new MSWord.Application();

 

            //WORD程序不可见

            appWord.Visible = false;

            //不弹出警告框

            appWord.DisplayAlerts = MSWord.WdAlertLevel.wdAlertsNone;

 

            //先保存默认的打印机

            string defaultPrinter = appWord.ActivePrinter;

 

            //打开要打印的文件

            MSWord.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 = "指定打印机的名字";

 

            //打印

            doc.PrintOut(

                ref oTrue, //此处为true,表示后台打印

                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,

                ref oMissing,

                ref oMissing,

                ref oMissing,

                ref oMissing

                );

 

            //打印完关闭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;

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值