7

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

David Schmitt
  • 58,259
  • 26
  • 121
  • 165
Joachim Kerschbaumer
  • 9,695
  • 7
  • 49
  • 84

2 Answers2

9

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

Vimes
  • 10,577
  • 17
  • 66
  • 86
  • This works, but searching after DocumentViewer has reached the last element will throw an exception. I found that by only creating a temporary XPS file on disk then reading it works. Tested on .NET Core 3.1. – Shahin Dohan Dec 06 '19 at 21:16
1

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

Artur Carvalho
  • 6,901
  • 10
  • 76
  • 105