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:
- Use the pagewidth and pageheight attributes to set the flowdocument size.
- Get documentpaginator through idocumentpaginatorsource
- Use documentpaginator. computepagecount to calculate the total number of pages
- Obtain the documentpage object of all pages
- 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