示例#1
0
        public Dictionary <string, int> GetTotalTreeStatistics(string treeName)
        {
            // Gets names of all the leaves we will look through.
            var folderLogic  = new FolderLogicHandler(new FileSystem());
            var treePath     = folderLogic.GetFullTreePath(treeName);
            var leavesInTree = folderLogic.GetAllLeafNamesWithNoExtension(treePath);

            // Open an instance of word to open the documents in.
            var wordInstance = WordSingleton.Instance.CreateNewWordInstance();

            var statistics = TreeStatsContainerGetter.GetStatisticsContainer();

            foreach (string leaf in leavesInTree)
            {
                // Open current leaf.
                string   fullLeafPath = folderLogic.GetFullLeafPath(treeName, leaf);
                Document document     = wordInstance.Documents.Open(fullLeafPath);

                // Prepare to get statistics.
                object      includeFootnotesAndEndnotes = true;
                WdStatistic wordStats = WdStatistic.wdStatisticWords;
                WdStatistic charStats = WdStatistic.wdStatisticCharacters;

                // Update statistics.
                statistics[StatsNamingConstants.WordCount]      += document.ComputeStatistics(wordStats, ref includeFootnotesAndEndnotes);
                statistics[StatsNamingConstants.CharacterCount] += document.ComputeStatistics(charStats, ref includeFootnotesAndEndnotes);
                statistics[StatsNamingConstants.LeafCount]++;

                document.Close();
            }
            return(statistics);
        }
示例#2
0
 public int ComputeStatistics(WdStatistic Statistic, ref object IncludeFootnotesAndEndnotes)
 {
     throw new System.NotImplementedException();
 }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            string imagePath  = @"C:\Temp\test.png";
            string docPath    = @"c:\temp\test.docx";
            string newDocPath = @"c:\temp\test-new.docx";

            Application wordApp = new Application();

            if (!File.Exists(docPath))
            {
                Console.WriteLine("Source Word document doesn't exist, please put a document in the path: " + docPath);
                return;
            }

            if (!File.Exists(imagePath))
            {
                Console.WriteLine("Image file doesn't exist, please put a document in the path: " + imagePath);
                return;
            }

            Document wordDoc = wordApp.Documents.Open(docPath);

            object saveWithDocument = true;
            object missing          = Type.Missing;

            // Get pages count
            WdStatistic PagesCountStat = WdStatistic.wdStatisticPages;
            int         PagesCount     = wordDoc.ComputeStatistics(PagesCountStat, ref missing);

            //Get pages
            object What  = WdGoToItem.wdGoToPage;
            object Which = WdGoToDirection.wdGoToAbsolute;
            object Start;
            object End;
            object CurrentPageNumber;
            object NextPageNumber;

            for (int Index = 1; Index < PagesCount + 1; Index++)
            {
                CurrentPageNumber = (Convert.ToInt32(Index.ToString()));
                NextPageNumber    = (Convert.ToInt32((Index + 1).ToString()));

                // Get start position of current page
                Start = wordApp.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref missing).Start;

                // Get end position of current page
                End = wordApp.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref missing).End;

                // Get text
                object oRange;
                if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString()))
                {
                    //Pages.Add(wordDoc.Range(ref Start, ref End).Text);

                    oRange = wordDoc.Range(ref Start, ref End);
                }
                else
                {
                    //Pages.Add(wordDoc.Range(ref Start).Text);

                    oRange = wordDoc.Range(ref Start);
                }

                InlineShape pic = wordDoc.InlineShapes.AddPicture(imagePath, ref missing, ref saveWithDocument, ref oRange);
                pic.Width  = 595;
                pic.Height = 842;

                Shape shapePic = pic.ConvertToShape();
                shapePic.Left            = -50;
                shapePic.Top             = -30;
                shapePic.WrapFormat.Type = WdWrapType.wdWrapFront;
            }

            wordDoc.SaveAs2(newDocPath);
            wordApp.Quit();

            Console.WriteLine("Please check the new file: " + newDocPath);
        }
 public int ComputeStatistics(WdStatistic Statistic, ref object IncludeFootnotesAndEndnotes)
 {
     throw new System.NotImplementedException();
 }
示例#5
0
        public static int GetWordPages(string docFile)
        {
            int    pages_t = -1;
            string msg     = "";

            Microsoft.Office.Interop.Word.Document    doc     = null;
            Microsoft.Office.Interop.Word.Application appWord = null;
            try
            {
                if (docFile == null || !(docFile.ToLower().EndsWith("doc") || docFile.ToLower().EndsWith("docx")))
                {
                    msg = "非word文件: " + docFile;
                    return(-999);
                }


                object wordFile = docFile;

                object oMissing = Missing.Value;

                //自定义object类型的布尔值
                object oTrue  = true;
                object oFalse = false;

                object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;

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

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



                //打开要打印的文件
                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);


                // 计算Word文档页数 
                WdStatistic stat = WdStatistic.wdStatisticPages;
                pages_t = doc.ComputeStatistics(stat, ref oMissing);//.ComputeStatistics(stat, ref Nothing); 
                //打印完关闭WORD文件
                doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);

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

                doc     = null;
                appWord = null;

                return(pages_t);
            }
            catch (System.Exception ex)
            {
                msg = ex.Message;
                LogHelper.WriteLogError(typeof(FilePages), ex.Message);
                pages_t = -997;
            }
            finally {
            }

            //this.textBox1.Text = msg;
            return(pages_t);
        }
示例#6
0
        private void buttonShowPages_Click(object sender, EventArgs e)
        {
            string msg = "";

            try
            {
                DialogResult ret = this.openFileDialogWord.ShowDialog(this);

                if (ret != DialogResult.OK)
                {
                    return;
                }

                string docFile = this.openFileDialogWord.FileName;

                if (docFile == null || !(docFile.ToLower().EndsWith("doc") || docFile.ToLower().EndsWith("docx")))
                {
                    msg = "非word文件: " + docFile;
                    throw new Exception(msg);
                }

                this.textBox1.Text = msg;

                object wordFile = docFile;

                object oMissing = Missing.Value;

                //自定义object类型的布尔值
                object oTrue  = true;
                object oFalse = false;

                object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;

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

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

                //打开要打印的文件
                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);


                // 计算Word文档页数 
                WdStatistic stat = WdStatistic.wdStatisticPages;
                int         num  = doc.ComputeStatistics(stat, ref oMissing);//.ComputeStatistics(stat, ref Nothing); 
                //打印完关闭WORD文件
                doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);

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

                doc     = null;
                appWord = null;

                msg = docFile + " : All Pages:" + num;
            }
            catch (System.Exception ex)
            {
                msg = ex.Message;
            }

            this.textBox1.Text = msg;
        }
        public void countpage(string printPCLFile)
        {
            string docFile = printPCLFile;
            string msg     = "";

            try
            {
                if (docFile.ToLower().EndsWith("pdf"))
                {
                    iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(docFile);
                    int num = reader.NumberOfPages;

                    msg  = num + "页";
                    mnum = num;
                }
                if (docFile.ToLower().EndsWith("jpg"))
                {
                    mnum = 1;
                }
                if (docFile.ToLower().EndsWith("xlsx") || docFile.ToLower().EndsWith("xlsm"))
                {
                    string path  = System.IO.Directory.GetCurrentDirectory();
                    string sPath = path + "\\temp\\xlsx";

                    // MessageBox.Show(docFile+"\n"+ sPath, "sPath");
                    if (XLSConvertToPDF(docFile, sPath))
                    {
                        string pdffile = sPath + ".pdf";
                        // MessageBox.Show(pdffile,"pdffile");
                        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdffile);
                        int num = reader.NumberOfPages;
                        msg  = num + "页";
                        mnum = num;
                    }
                }
                if (docFile.ToLower().EndsWith("doc") || docFile.ToLower().EndsWith("docx"))
                {
                    //this.textFilename.Text = msg;
                    object wordFile = docFile;
                    object oMissing = Missing.Value;
                    //自定义object类型的布尔值
                    object oTrue  = true;
                    object oFalse = false;

                    object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
                    //定义WORD Application相关
                    Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                    //WORD程序不可见
                    appWord.Visible = false;
                    //不弹出警告框
                    appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                    //打开要打印的文件
                    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);
                    // 计算Word文档页数 
                    WdStatistic stat = WdStatistic.wdStatisticPages;
                    num = doc.ComputeStatistics(stat, ref oMissing);//.ComputeStatistics(stat, ref Nothing); 
                    //打印完关闭WORD文件
                    doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
                    //退出WORD程序
                    appWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                    doc     = null;
                    appWord = null;
                    msg     = num + "页";
                    mnum    = num;
                }
            }
            catch (System.Exception ex)
            {
                msg = ex.Message;
            }
        }