简体   繁体   English

在WPF中是否可以通过页码获取每个DocumentPage的内容?

[英]Is it possible in WPF to get content of each DocumentPage by page number?

I use DocumentPaginator to divide FlowDocument's text into pages. 我使用DocumentPaginator将FlowDocument的文本分为几页。 Is it possible to get content of each pages by page number after ComputePageCount()? ComputePageCount()之后是否可以通过页码获取每个页面的内容? If not, how can I do it in other way? 如果没有,我该怎么办?

Code: 码:

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

Yes, it is possible. 对的,这是可能的。

Here is an example: 这是一个例子:

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

You can use the GetPage function of the DocumentPaginator object. 您可以使用DocumentPaginator对象的GetPage函数。 Its takes a page number as a parameter. 它以页码为参数。

See http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpaginator.getpage(v=vs.110).aspx 请参阅http://msdn.microsoft.com/zh-cn/library/system.windows.documents.documentpaginator.getpage(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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