为 WPF 的 FlowDocumentReader 添加书签

发布于 2024-08-20 02:28:13 字数 623 浏览 7 评论 0原文

我正在尝试保存和恢复 FlowDocumentReader 中文档的位置以创建书签功能。似乎没有任何内置的可公开访问的查找或搜索功能,给我留下了以下选项:

  1. 改用 FlowDocumentPageViewer, 每次打开窗口时保存页面 已调整大小并尽快恢复 当应用程序重新加载时。

  2. 循环遍历数组中的所有元素 文档属性 FlowDocumentReader,正在寻找 第一个通过屏幕的 命中测试,然后使用反射 使用内部搜索功能 将该文本重新带回到视图中 稍后。

  3. 序列化整个控件。

  4. 编写我自己的文档查看器控件。

第一个很烦人,因为我必须放弃 FlowDocumentReader 的两页和滚动查看选项。它还意味着在用户有机会调整窗口大小之前先查找已保存的页面。这是脆弱的,如果用户说在会话之间切换分辨率,则可能会崩溃。

第二个是一个花哨的黑客,可能会起作用,但速度很慢,如果内部发生变化,就会完全崩溃。

第三个看起来是我最好的选择,但它只能让我保存/恢复当前位置,不能设置任意书签。

第四是工作量太大了。这些控件非常太棒了,我只需要这个功能...

还有其他方法可以解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风轻花落早 2024-08-27 02:28:13

这似乎适用于页面视图,但不适用于滚动视图,但这没关系。

reader 的类型为 FlowDocumentReader,而 document 是其中的 FlowDocument

设置书签:

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

恢复书签:

bookmark.BringIntoView();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文