gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:02:53 26 4
gpt4 key购买 nike

.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com