views:

271

answers:

1

I'm trying to save and restore the position of the document within a FlowDocumentReader to create a bookmark feature. There doesn't appear to be any seeking or search feature build in that is publicly accessible, leaving me with the following options:

  1. Use FlowDocumentPageViewer instead, saving the page each time the window is resized and restoring it as soon as the app is reloaded.

  2. Loop through all the elements in the Document property of FlowDocumentReader, looking for the first one that passes an on-screen hit test, then using reflection to use the internal search features to bring that text back into view at a later time.

  3. Serialize the entire control.

  4. Write my own document viewer control.

No. 1 is annoying because I'd have to forfeit the two-page and scroll viewing options of FlowDocumentReader. It also means seeking to the saved page before the user has a chance to resize the window. This is fragile and would probably break if the user say, switched resolutions between sessions.

No. 2 is a garish hack that would probably work, but be slow and break completely if the internals ever change.

No. 3 is looking like my best bet, but it only lets me save/restore the current position, not set arbitrary bookmarks.

No. 4 is just too much work. These controls are utterly fantastic, I just need this one feature...

Is there any other way to go about this?

A: 

This seems to work well for page views, but not for scroll view, which is okay.

reader is of type FlowDocumentReader, and document is the FlowDocument within it.

Set the bookmark:

var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator as DynamicDocumentPaginator;
var position = paginator.GetPagePosition(paginator.GetPage(reader.PageNumber - 1)) as TextPointer;
bookmark = position.Paragraph;

Restore the bookmark:

bookmark.BringIntoView();
Matt Olenik