Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

c# - Get pages of word document

I'm trying to get all pages of MSWord document via Microsoft.Office.Interop.Word (I'm using C# in VS2012). What I would like to get is List< String > Pages, where index is the number of page. I understand (at least I think so) that there is no direct way to do that. So I came up with something like that:

        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("
{0}", InnerText);
            else
            {
                Pages.Add(InnerText);
                NumberOfPreviousPage = NumberOfPage;
            }
        }

But, when algorithm gets to paragraph, which starts on one page and ends on another, it decides that paragraph should be on next page. I want to split this paragraph between pages, but I don't know how to detect where I have to do the split.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...