【问题标题】:WPF DocumentViewer Find-function and FixedPage documentsWPF DocumentViewer Find-function 和 FixedPage 文档
【发布时间】:2010-09-16 01:02:58
【问题描述】:

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

当插入FixedPage 的对象作为DocumentViewer 的文档源时,查找功能找不到任何东西。甚至没有一个字母。我还没试过FlowDocument, 由于DocumentViewer 的文档没有那么有用,而且网络上的资源实际上并不存在,我现在想问问stackoverflow 社区:

要让 WPF DocumentViewer 的 Find-Function 与 FixedPage 文档一起使用需要什么?

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

【问题讨论】:

    标签: wpf xaml xps documentviewer fixedpage


    【解决方案1】:

    FixedDocuments 也有同样的问题。如果您将 FixedDocument 转换为 XPS 文档,那么它可以正常工作。

    从 FixedDocument 在内存中创建 XPS 文档然后在 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();
            }
        }
    }
    

    【讨论】:

    • 这行得通,但是在 DocumentViewer 到达最后一个元素之后搜索会引发异常。我发现只需在磁盘上创建一个临时 XPS 文件然后读取它就可以了。在 .NET Core 3.1 上测试。
    【解决方案2】:

    我在富文本框中搜索文本时遇到了问题,它太慢了。我所做的是每次我想搜索时都会压缩xaml。我提高了几个数量级。

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

    干杯

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 2018-02-13
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多