WPF FlowDocument Page Break/Positioning
Here's the situation.
I'm tasked with auto-generating a rather large document consisting of a number of distinct sections of varying height and constant (full-page) width.
Each section consists of two distinct sub-sections. Think of them as "header" and "data."
The problem, then, is: When only one secti开发者_如何学编程on fits on a page (because it has a lot of data) the header should display at the very top and the data should display at the very bottom, leaving any unused space in the center.
When more than one section fits on a page everything should "squish to the top", leaving no space between the sub-sections, or even the sections. All of the extra whitespace should be at the bottom.
What sorts of constructs can I use to accomplish this?
Perhaps this is a task better suited to a FixedDocument? I have a rather large book on WPF, but it only barely mentions FixedDocuments.
Not sure if you are still battling with this issue, but you could try using FlowDocument.
if you write a wrapper around DocumentPaginator you'll be able to insert a header into the flowdoc. Also, you can set the flowdoc.PagePadding to a custom value, taking into account the printablePageHeight and the height of your content-size.
Here's an example of a wrapper around the DocumentPaginator which I got from the book : Pro WPF in C# 2008 - Mathew MacDonald
Hope it helps. (PS. I just copied and pasted the default so didn't add any custom calcs etc.)
using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace NPS.ClinicalEAudit.Controls
{
public class FlowDocPaginator : DocumentPaginator
{
private DocumentPaginator _paginator;
public FlowDocPaginator(FlowDocument flowDoc)
{
_paginator = ((IDocumentPaginatorSource) flowDoc).DocumentPaginator;
}
public override bool IsPageCountValid
{
get { return _paginator.IsPageCountValid; }
}
public override int PageCount
{
get { return _paginator.PageCount; }
}
public override Size PageSize
{
get { return _paginator.PageSize; }
set { _paginator.PageSize = value; }
}
public override IDocumentPaginatorSource Source
{
get { return _paginator.Source; }
}
public override DocumentPage GetPage(int pageNumber)
{
// Get the requested page.
DocumentPage page = _paginator.GetPage(pageNumber);
// Wrap the page in a Visual object. You can then apply transformations
// and add other elements.
ContainerVisual newVisual = new ContainerVisual();
newVisual.Children.Add(page.Visual);
// Create a header.
DrawingVisual header = new DrawingVisual();
using (DrawingContext dc = header.RenderOpen())
{
Typeface typeface = new Typeface("Times New Roman");
FormattedText text = new FormattedText("Page " +
(pageNumber + 1).ToString(), CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, typeface, 14, Brushes.Black);
// Leave a quarter inch of space between the page edge and this text.
dc.DrawText(text, new Point(96 * 0.25, 96 * 0.25));
}
// Add the title to the visual.
newVisual.Children.Add(header);
// Wrap the visual in a new page.
DocumentPage newPage = new DocumentPage(newVisual);
return newPage;
}
}
}
精彩评论