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

       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;

        }

猜你喜欢

转载自googlelee.iteye.com/blog/2340972