wpfxamlxpsdocumentviewerfixedpage

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]


Solution

  • 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