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

标签 wpf xaml xps documentviewer fixedpage

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

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

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

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

最佳答案

我在使用固定文档时也遇到了同样的问题。如果您将固定文档转换为 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();
        }
    }
}

enter image description here

关于WPF DocumentViewer 查找功能和固定页面文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/183249/

相关文章:

wpf - 在WPF中用鼠标拖动选定的项目

WPF 和 MVVM。绑定(bind)事件

silverlight - 需要 XAML 教程

c# - PrintDialog/XPS Document Writer 中忽略的纸张大小

C++ LogFont 嵌入到 XPS 中

c# - mvvm 中的 ComboBox 静态值绑定(bind)

C# WPF 在 Stackpanel 中以编程方式创建的 DataTemplate Dockpanel 无效

c# - 是否可以将 Xaml 设计器或智能感知与 Xamarin.Forms 一起使用?

WPF DockPanel IsMouseOver 仅在将鼠标悬停在子控件上时触发

c# - 如何在将 XPS 转换为 PDF 时获取 GhostPDL 进度通知?