R – How to goto particular page number in flowdocument reader through code in WPF

flowdocumentflowdocumentreaderwpf

There is a pagenumber property in flowdocument reader.But that property is readonly. Is there any way to goto particular page number in flowdocument reader.Please help.

Thanks.

Best Answer

If you keep track of the Blocks on the FlowDocument contained in the FlowDocumentReader, than you can simply use:

// Getting a block by index
YourReader.Document.Blocks.ElementAt(index).BringIntoView();

// Showing Last Block
YourReader.Document.Blocks.LastBlock.BringIntoView();

// Showing the last Inline
(YourReader.Document.Blocks.LastBlock as Paragraph).Inlines.LastInline.BringIntoView();

This works only on the page ViewingModes of the FlowDocumentReader.

if you whould like to do so on the scroll mode, you must go down the visual tree and search for the ScrollViewer, somthing like this:

        public static ScrollViewer FindScroll(Visual visual)
        {
            if (visual is ScrollViewer)
                return visual as ScrollViewer;

            ScrollViewer searchChiled = null;
            DependencyObject chiled;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
            {
                chiled = VisualTreeHelper.GetChild(visual, i);
                if (chiled is Visual)
                    searchChiled = FindScroll(chiled as Visual);
                if (searchChiled != null)
                    return searchChiled;
            }

            return null;
        }

ScrollViewer scroller = FindScroll(YourReader as Visual);
if (scroller != null) 
   (scroller as ScrollViewer).ScrollToBottom();