C# gets the number of pages in the word document and prints the word document (written as three functions)

       private void getWordPageNumber()

        {

            wordApp = new MSWord.ApplicationClass();

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

            // open 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);

 

            // Calculate the number of pages in the Word document

            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();

            //It is not realistic to call the program window, but it is invalid for some applications

            p.StartInfo.CreateNoWindow = true;

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

 

            //Use the mode automatically recognized by the operating system

            p.StartInfo.UseShellExecute = true;

 

            //The path of the file to be printed, which can be WORD, EXCEL, PDF, TXT, etc.

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

 

            //Specify the action to be executed, which is print, that is, print, and open is open

            p.StartInfo.Verb = "print";

 

            //Start

            p.Start();

        }

 

        private void anotherPrint()

        {

            // file path to print

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

 

            object oMissing = Missing.Value;

 

            // Boolean value of custom object type

            object oTrue = true;

            object oFalse = false;

 

            object doNotSaveChanges = MSWord.WdSaveOptions.wdDoNotSaveChanges;

 

            //Define WORD Application related

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

 

            //WORD program is not visible

            appWord.Visible = false;

            // don't pop up a warning box

            appWord.DisplayAlerts = MSWord.WdAlertLevel.wdAlertsNone;

 

            //Save the default printer first

            string defaultPrinter = appWord.ActivePrinter;

 

            //Open the file to be printed

            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);

 

            //Set the specified printer

           // appWord.ActivePrinter = "Specify the name of the printer";

 

            //Print

            doc.PrintOut(

                ref oTrue, // here is true, which means background printing

                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

                );

 

            //Close the WORD file after printing

            doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);

 

            //Restore the original default printer

            appWord.ActivePrinter = defaultPrinter;

 

            //Exit the WORD program

            appWord.Quit(ref oMissing, ref oMissing, ref oMissing);

 

            doc = null;

            appWord = null;

        }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394963&siteId=291194637