Get pages of word document

孤者浪人 提交于 2019-11-28 10:26:37

Eventually, I finished up with this, and it works (it's lame, it's ugly, but it does what it should):

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

A bit of a simpler solution.

Pseudo code:

  • Grab the page count.
  • For each page:
    • Find the characters between this pages last character index and the last pages last character index.

Implementation:

    /// <summary>
    /// Reads each page of the word document into a string and returns the list of the page strings.
    /// </summary>
    public static IEnumerable<string> ReadPages(string filePath)
    {
        ICollection<string> pageStrings = new List<string>();
        Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
        Document doc = app.Documents.Open(filePath);

        long pageCount = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages);
        int lastPageEnd = 0; // The document starts at 0.
        for ( long i = 0; i < pageCount; i++)
        {
            // The "range" of the page break. This actually is a range of 0 elements, both start and end are the 
            // location of the page break.
            Range pageBreakRange = app.Selection.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
            string currentPageText = doc.Range(lastPageEnd, pageBreakRange.End).Text;
            lastPageEnd = pageBreakRange.End;
            pageStrings.Add(currentPageText);
        }
        return pageStrings;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!