简体   繁体   中英

WPF DocumentViewer Find-function and FixedPage documents

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

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();
        }
    }
}

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM