2014-09-12 6 views
1

DocumentPaginator를 사용하여 FlowDocument의 텍스트를 페이지로 나눕니다. ComputePageCount() 이후 페이지 번호별로 각 페이지의 내용을 가져올 수 있습니까? 그렇지 않다면 다른 방법으로 어떻게 할 수 있습니까?WPF에서 페이지 번호별로 각 DocumentPage의 내용을 가져올 수 있습니까?

코드 :

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

답변

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