WPF flowdocument with different first page size

ぐ巨炮叔叔 提交于 2020-01-06 03:48:05

问题


I have a flowdocument with repeating header/footer which is generated with custom DocumentPaginator. The problem is I need different header size for the first page or in the other words I need different PagePadding for the first page. A DocumentPaginator with repeating page header/footer was described many times (e.g. here or here) but never (?) for different size headers (very similar question without answer). My code snippet:

public class ReportPaginator : DocumentPaginator
{
    private DocumentPaginator _flowDocumentpaginator;
    private readonly FlowDocument _flowDocument;
    public ReportPaginator(FlowDocument document)
    {
        _flowDocumentpaginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
        _flowDocument = document;
    }

    public override DocumentPage GetPage(int pageNumber)
    {
        DocumentPage page = _flowDocumentpaginator.GetPage(pageNumber);
        ContainerVisual newVisual = new ContainerVisual();
        DrawingVisual header = new DrawingVisual();
        if (pageNumber == 0)
        {
            //smaller header
            [...]
        }
        else
        {
            //normal header 
            [...]
        }
        newVisual.Children.Add(header);

        if (pageNumber == 0)
        {
            //set different margins for the first page
            _flowDocument.PagePadding = new Thickness(0.7 * 96, 93.2, 0.7 * 96, 74);
            _flowDocumentpaginator = ((IDocumentPaginatorSource)_flowDocument).DocumentPaginator;
            page = _flowDocumentpaginator.GetPage(pageNumber);
        }
        else if (pageNumber == 1)
        {
            //set "normal" margins for other pages
            _flowDocument.PagePadding = new Thickness(0.7 * 96, 180, 0.7 * 96, 74);
            _flowDocumentpaginator = ((IDocumentPaginatorSource)_flowDocument).DocumentPaginator;
            page = _flowDocumentpaginator.GetPage(pageNumber);
        }

        newVisual.Children.Add(page.Visual);

        DrawingVisual footer = new DrawingVisual();
        [...]
        newVisual.Children.Add(footer);

        return new DocumentPage(newVisual, page.Size, page.BleedBox, page.ContentBox);
    }
}

This code produces pages of correct size (the first one is bigger than others) but pagination is broken: a few lines of the bottom of the first page are repeated at the top of the second page, because pagination is recalculated after changing PagePadding.

来源:https://stackoverflow.com/questions/58797651/wpf-flowdocument-with-different-first-page-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!