WPF: Use documentpaginator to manually paging flowdocument

Source: Internet
Author: User

The sample program can display a text file by PAGE based on the user-defined size.

 

This is due to the documentpaginator in flowdocument ). Flowdocument, fixeddocument, and fixeddocumentsequence in WPF both have their own page splitters. They all inherit from the idocumentpaginatorsource interface. This excuse is to return the documentpaginator object.

 

Fixeddocument and fixeddocumentsequence are well understood using the page splitter, because they must be displayed on a fixed page size. While flowdocument is flow, some controls, flowdocumentreader or flowdocumentpageviewer, can display them by page. In this case, they are used as documentpaginator ). Since the WPF internal controls can be used, we can also use them by ourselves.

 

The procedure is as follows:

  1. Use the pagewidth and pageheight attributes to set the flowdocument size.
  2. Get documentpaginator through idocumentpaginatorsource
  3. Use documentpaginator. computepagecount to calculate the total number of pages
  4. Obtain the documentpage object of all pages
  5. Use documentpage. Visual and visualbrush to reflect the page content to the interface

 

Of course, in this example, you must first convert the TXT text to flowdocument (add run to paragraph, and then add paragraph to the blocks attribute in flowdocument ).

 

Main logic code:

Uielement [] pageflowdoc (flowdocument fdoc, double width, double height)

{

// Set the flowdocument size

Fdoc. pagewidth = width;

Fdoc. pageheight = height;

// Get the page sharer

VaR paginator = (idocumentpaginatorsource) fdoc). documentpaginator;

// Calculate the total number of pages

Paginator. computepagecount ();

 

VaR elements = new uielement [paginator. pagecount];

 

// Obtain the page

For (INT I = 0; I <elements. length; I ++)

{

VaR grid = new grid ();

Grid. width = width;

Grid. Height = height;

 

VaR visual = paginator. getpage (I). Visual;

VaR visualbrush = new visualbrush (visual );

Grid. Background = visualbrush;

 

Elements [I] = grid;

}

 

Return elements;

}

 

Source code download

Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.

Source code environment: Visual Studio 2010

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.