Beispiel #1
0
        public int getpage(object file)
        {
            try
            {
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                object nullobj = System.Reflection.Missing.Value;

                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
                    ref file, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj);
                doc.ActiveWindow.Selection.WholeStory();
                doc.ActiveWindow.Selection.Copy();
                IDataObject data = Clipboard.GetDataObject();

                // get number of pages
                Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                int pages = doc.ComputeStatistics(stat, Type.Missing);

                //string text = data.GetData(DataFormats.Text).ToString();  //获取具体内容,此项目不需要

                doc.Close(ref nullobj, ref nullobj, ref nullobj);
                app.Quit(ref nullobj, ref nullobj, ref nullobj);


                return(pages);
            }
            catch (Exception ex)
            {
                ex.ToString();
                return(0);
            }
        }
Beispiel #2
0
        public List <string> readDocFile(string path)
        {
            List <string> data = new List <string>();

            try
            {
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document    doc  = new Microsoft.Office.Interop.Word.Document();

                object fileName = path;
                object missing  = System.Type.Missing;
                object What     = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                object Which    = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
                object Start;
                object End;
                object CurrentPageNumber;
                object NextPageNumber;
                object Miss = System.Reflection.Missing.Value;


                doc = word.Documents.Open(ref fileName,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing);

                Microsoft.Office.Interop.Word.WdStatistic PagesCountStat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                int PagesCount = doc.ComputeStatistics(PagesCountStat, ref missing);

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

                    //Lay vi tri bat dau cua trang
                    Start = word.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start;
                    //Lay vi tri cuoi cua trang
                    End = word.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End;
                    //Lay text ra
                    if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString()))
                    {
                        data.Add(doc.Range(ref Start, ref End).Text);
                    }
                    else
                    {
                        data.Add(doc.Range(ref Start).Text);
                    }
                }


                doc.Close();
                word.Quit();
            }
            catch (Exception)
            {
                data = null;
            }

            return(data);
        }
Beispiel #3
0
        public void Merge() // Shall be called on a separate thread and shall make use of the member variables
        {
            object missing    = System.Type.Missing;
            object pageBreak  = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
            object outputFile = outputFilePath;

            // Create a new Word application
            Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();

            bSuccessfulOperation = true;
            try
            {
                // Create a new file
                Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Add(
                    ref missing
                    , ref missing
                    , ref missing
                    , ref missing);

                // Make a Word selection object.
                Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

                //Count the number of documents to insert;
                int documentCount = fileList.Length;

                //A counter that signals that we shoudn't insert a page break at the end of document.
                int breakStop = 0;

                int iCounter   = 0;
                int iFileCount = fileList.Length;

                // Iterate through the entire list of files
                foreach (string file in fileList)
                {
                    Console.WriteLine("Merging file: " + file);
                    breakStop++;
                    // Insert the files to our template
                    selection.InsertFile(
                        file
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing);

                    //Do we want page breaks added after each documents?
                    if (insertPageBreaks && breakStop != documentCount)
                    {
                        selection.InsertBreak(ref pageBreak);
                    }
                    iCounter++;
                    UpdateProgressBar(((float)iCounter / iFileCount) * 100);
                }

                // Save the document to it's output file.
                wordDocument.SaveAs(
                    ref outputFile
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing
                    , ref missing);

                // Get some stats about the merged file
                lineCount = wordDocument.Sentences.Count;
                wordCount = wordDocument.Words.Count;

                Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                pageCount = wordDocument.ComputeStatistics(stat, ref missing);
                wordDocument.Save();

                // Clean up!
                wordDocument = null;
            }
            catch (Exception ex)
            {
                //I didn't include a default error handler so i'm just throwing the error
                Console.WriteLine(ex);
                bSuccessfulOperation = false;
            }
            finally
            {
                // Finally, Close our Word application
                wordApplication.Quit(ref missing, ref missing, ref missing);
            }


            DisplayResult(); // Done, show the results
        }
Beispiel #4
0
        public void replaceContent(List <Segment> listSegs, string path)
        {
            try
            {
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document    doc  = new Microsoft.Office.Interop.Word.Document();

                object fileName = path;
                object missing  = System.Type.Missing;
                object What     = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                object Which    = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
                object Start;
                object End;
                object CurrentPageNumber;
                object NextPageNumber;
                object Miss = System.Reflection.Missing.Value;

                doc = word.Documents.Open(ref fileName,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing);

                Microsoft.Office.Interop.Word.WdStatistic PagesCountStat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                int PagesCount = doc.ComputeStatistics(PagesCountStat, ref missing);

                for (int Index = 1; Index <= PagesCount; Index++)
                {
                    CurrentPageNumber = (Convert.ToInt32(Index.ToString()));
                    NextPageNumber    = (Convert.ToInt32((Index + 1).ToString()));
                    List <Segment> listSegInPage = new List <Segment>();
                    listSegInPage = getListSegsInPage(listSegs, Index);

                    //Lay vi tri bat dau cua trang
                    Start = word.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start;
                    //Lay vi tri cuoi cua trang
                    End = word.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End;
                    if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString()))
                    {
                        foreach (Segment seg in listSegInPage)
                        {
                            tm tmp = seg.getTM();
                            if (tmp.Target != null)
                            {
                                if (tmp.Target.Trim() != "")
                                {
                                    findAndReplace(doc.Range(ref Start, ref End), tmp.Source, tmp.Target);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (Segment seg in listSegInPage)
                        {
                            tm tmp = seg.getTM();
                            if (tmp.Target != null)
                            {
                                if (tmp.Target.Trim() != "")
                                {
                                    findAndReplace(doc.Range(ref Start), tmp.Source, tmp.Target);
                                }
                            }
                        }
                    }

                    //Lay text ra
                    //if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString()))
                    //{
                    //    data.Add(doc.Range(ref Start, ref End).Text);
                    //}
                    //else
                    //{
                    //    data.Add(doc.Range(ref Start).Text);
                    //}
                }


                doc.Close();
                word.Quit();
            }
            catch (Exception)
            {
            }
        }