WPF DocumentViewer 查找功能和固定页面文档

发布于 2024-07-06 16:50:48 字数 863 浏览 22 评论 0原文

.Net 包含一个名为 DocumentViewer。 它还提供了一个子控件,用于在加载的文档中查找文本(这至少是它应该做的)。

插入 FixedPage 的对象作为 DocumentViewer 的文档源,查找功能只是找不到任何内容。 连一个字母都没有。 我还没有尝试过 FlowDocument 还没有, 由于 DocumentViewer 的文档不太有用,而且网上的资源实际上并不存在,我现在想问 stackoverflow 社区:

需要什么才能获得 WPF 的 Find-Function < code>DocumentViewer 是否可以使用 FixedPage 文档?

[顺便说一句,我不为 DocumentViewer 使用自定义 ControlTemplates]

.Net contains a nice control called DocumentViewer. it also offers a subcontrol for finding text in the loaded document (that's at least what it is supposed to do).

When inserting FixedPage's objects as document source for the DocumentViewer, the find-functionality just does not find anything. Not even single letters. I haven't tried FlowDocument's yet,
as the documentation for DocumentViewer is not that useful and the resources on the net are not actually existing, I now want to ask the stackoverflow community:

What does it need to get the Find-Function of the WPF DocumentViewer working with FixedPage documents?

[btw, I don't use custom ControlTemplates for DocumentViewer]

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

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

发布评论

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

评论(2

懒猫 2024-07-13 16:50:48

我在使用固定文档时也遇到了同样的问题。 如果您将固定文档转换为 XPS 文档,则它可以正常工作。

从固定文档在内存中创建 XPS 文档然后在文档查看器中显示的示例。

// Add to xaml: <DocumentViewer x:Name="documentViewer" />
// Add project references to "ReachFramework" and "System.Printing"
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.IO;
using System.IO.Packaging;
using System.Windows.Xps.Packaging;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Set up demo FixedDocument containing text to be searched
            var fixedDocument = new FixedDocument();
            var pageContent = new PageContent();
            var fixedPage = new FixedPage();
            fixedPage.Children.Add(new TextBlock() { Text = "Demo document text." });
            pageContent.Child = fixedPage;
            fixedDocument.Pages.Add(pageContent);

            // Set up fresh XpsDocument
            var stream = new MemoryStream();
            var uri = new Uri("pack://document.xps");
            var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
            PackageStore.AddPackage(uri, package);
            var xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri);

            // Write FixedDocument to the XpsDocument
            var docWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
            docWriter.Write(fixedDocument);

            // Display XpsDocument in DocumentViewer
            documentViewer.Document = xpsDoc.GetFixedDocumentSequence();
        }
    }
}

输入图片此处描述

I had this same problem with FixedDocuments. If you convert your FixedDocument to an XPS document then it works fine.

Example of creating an XPS Document in memory from a FixedDocument then displaying in a DocumentViewer.

// Add to xaml: <DocumentViewer x:Name="documentViewer" />
// Add project references to "ReachFramework" and "System.Printing"
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.IO;
using System.IO.Packaging;
using System.Windows.Xps.Packaging;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Set up demo FixedDocument containing text to be searched
            var fixedDocument = new FixedDocument();
            var pageContent = new PageContent();
            var fixedPage = new FixedPage();
            fixedPage.Children.Add(new TextBlock() { Text = "Demo document text." });
            pageContent.Child = fixedPage;
            fixedDocument.Pages.Add(pageContent);

            // Set up fresh XpsDocument
            var stream = new MemoryStream();
            var uri = new Uri("pack://document.xps");
            var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
            PackageStore.AddPackage(uri, package);
            var xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri);

            // Write FixedDocument to the XpsDocument
            var docWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
            docWriter.Write(fixedDocument);

            // Display XpsDocument in DocumentViewer
            documentViewer.Document = xpsDoc.GetFixedDocumentSequence();
        }
    }
}

enter image description here

魂归处 2024-07-13 16:50:48

我在 RichTextBox 中搜索文本时遇到问题,速度太慢。 我所做的就是每次想要搜索时都对 xaml 进行处理。 我提高了几个数量级。

这是一个很大的解决方法,基于 Chris Anderson 的的一部分。

干杯

I had trouble with searching text in richtextbox, it was too slow. What I did was crunch the xaml every time I wanted to search. I improved several orders of magnitude.

It's a big workaround based in a part of the Chris Anderson's book.

Cheers

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