获取word文档的页面

use*_*379 6 .net c# interop ms-word

我试图通过Microsoft.Office.Interop.Word获取MSWord文档的所有页面(我在VS2012中使用C#).我想得到的是List <String> Pages,其中index是页面数.我理解(至少我认为是这样),没有直接的方法可以做到这一点.所以我想出了类似的东西:

        List<String> Pages = new List<String>();
        int NumberOfPreviousPage = -1;
        int NumberOfPage = -1;
        string InnerText = "";
        for (int i = 0; i < Doc.Paragraphs.Count; i++)
        {
            Paragraph CurrentParagraph = Doc.Paragraphs[i + 1];
            InnerText = CurrentParagraph.Range.Text;
            NumberOfPage = CurrentParagraph.Range.get_Information(WdInformation.wdActiveEndPageNumber);
            if (NumberOfPage == NumberOfPreviousPage)
                Pages[Pages.Count - 1] += String.Format("\r\n{0}", InnerText);
            else
            {
                Pages.Add(InnerText);
                NumberOfPreviousPage = NumberOfPage;
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是,当算法到达段落时,它从一个页面开始到另一个页面结束,它决定段落应该在下一页.我想在页面之间拆分这段,但我不知道如何检测我必须在哪里进行拆分.

use*_*379 7

最终,我完成了这个,并且它可以工作(它很蹩脚,它很难看,但它做了应有的事情):

public string[] GetPagesDoc(object Path)
    {
        List<string> Pages = new List<string>();

        // Get application object
        Microsoft.Office.Interop.Word.Application WordApplication = new Microsoft.Office.Interop.Word.Application();

        // Get document object
        object Miss = System.Reflection.Missing.Value;
        object ReadOnly = false;
        object Visible = false;
        Document Doc = WordApplication.Documents.Open(ref Path, ref Miss, ref ReadOnly, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Visible, ref Miss, ref Miss, ref Miss, ref Miss);

        // Get pages count
        Microsoft.Office.Interop.Word.WdStatistic PagesCountStat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
        int PagesCount = Doc.ComputeStatistics(PagesCountStat, ref Miss);

        //Get pages
        object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
        object Which = Microsoft.Office.Interop.Word.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 = WordApplication.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start;

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

            // Get text
            if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString()))
                Pages.Add(Doc.Range(ref Start, ref End).Text);
            else
                Pages.Add(Doc.Range(ref Start).Text);
        }
            return Pages.ToArray<string>();
    }
Run Code Online (Sandbox Code Playgroud)