首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WPF中可以通过页码获取每个DocumentPage的内容吗?

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

Stack Overflow用户
提问于 2014-09-12 19:11:29
回答 2查看 1.8K关注 0票数 2

我使用DocumentPaginator将FlowDocument的文本分成多个页面。是否可以在ComputePageCount()之后按页码获取每个页面的内容?如果不是,我怎么能用其他方式呢?

代码:

代码语言:javascript
复制
var flowDocument = this.Document;
flowDocument.MaxPageHeight = this.MaxContentHeight;
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
paginator.ComputePageCount();
EN

回答 2

Stack Overflow用户

发布于 2014-11-04 02:53:14

是的,这是可能的。

下面是一个示例:

MainWindow.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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();
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2014-09-12 22:27:47

您可以使用DocumentPaginator对象的GetPage函数。它接受一个页码作为参数。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25806961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档