Возможно ли в WPF получить содержимое каждой страницы DocumentPage по номеру страницы?

Я использую DocumentPaginator для разделения текста FlowDocument на страницы. Можно ли получить содержимое каждой страницы по номеру страницы после ComputePageCount()? Если нет, то как я могу сделать это по-другому?

Код:

var flowDocument = this.Document;
flowDocument.MaxPageHeight = this.MaxContentHeight;
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
paginator.ComputePageCount();

person Jeksonic    schedule 12.09.2014    source источник


Ответы (2)


Да, это возможно.

Вот пример:

MainWindow.xaml

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <TextBlock x:Name="outputTextBlock" DockPanel.Dock="Bottom"/>
        <FlowDocumentPageViewer>
            <FlowDocument x:Name="flowDocument" Loaded="OnFlowDocumentLoaded">
                <Paragraph>First page</Paragraph>
                <Paragraph BreakPageBefore="True">Second page</Paragraph>
                <Paragraph BreakPageBefore="True">Third page</Paragraph>
            </FlowDocument>
        </FlowDocumentPageViewer>
    </DockPanel>
</Window>

MainWindow.xaml.cs

using System.Text;
using System.Windows;
using System.Windows.Documents;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnFlowDocumentLoaded(object sender, RoutedEventArgs e)
        {
            var paginator = (DynamicDocumentPaginator)((IDocumentPaginatorSource)this.flowDocument).DocumentPaginator;

            var text = new StringBuilder();

            for (int pageNumber = 0; ; ++pageNumber)
                using(var page = paginator.GetPage(pageNumber))
                using (var nextPage = paginator.GetPage(pageNumber + 1))
                {
                    if (page == DocumentPage.Missing)
                        break;

                    var startPosition = (TextPointer)paginator.GetPagePosition(page);
                    var endPosition = nextPage == DocumentPage.Missing ? this.flowDocument.ContentEnd : (TextPointer)paginator.GetPagePosition(nextPage);

                    var range = new TextRange(startPosition, endPosition);

                    text.AppendFormat("Page {0}:", pageNumber + 1).AppendLine();
                    text.AppendLine(range.Text);
                }

            this.outputTextBlock.Text = text.ToString();
        }
    }
}
person Stipo    schedule 03.11.2014

Вы можете использовать функцию GetPage объекта DocumentPaginator. В качестве параметра принимает номер страницы.

См. http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpaginator.getpage(v=vs.110).aspx

person kenjara    schedule 12.09.2014
comment
Да, но как мне получить строковый текст этой страницы? - person Jeksonic; 12.09.2014