检测FixedPage上的内容是否超过底部边界

时间:2018-07-30 17:17:30

标签: c# wpf fixeddocument fixedpage

我正在创建一个文档,稍后再打印。该文档应包含一个网格,该网格列出诸如表的项目,包括标题行。项目的数量各不相同,因此网格可能会超出单个页面的底部边界。发生这种情况时,我想继续在第二页上,“ table”标题将再次位于该页上。我正在以编程方式在for循环中添加行。

您知道一种如何检测是否超出底部边界的方法吗?也许有不同的方法。

1 个答案:

答案 0 :(得分:0)

解决方案是将StackPanel作为每个FixedPage的“根子”。然后,我可以添加内容并对其进行度量。

public StackPanel AddNewPage()
{
    PageContent pC = new PageContent();
    FixedPage fP = new FixedPage { Width = PageWidth, Height = PageHeight, Margin = Margin };

    StackPanel sP = new StackPanel { Width = PageWidth - Margin.Left - Margin.Right };
    fP.Children.Add(sP);

    pC.Child = fP;

    //FixedDocument
    Document.Pages.Add(pC);

    //used later to add content to the page 
    return sP;
}

public bool IsPageOverfilled(int pageIndex)
{
    StackPanel sP = (Document.Pages[pageIndex].Child as FixedPage).Children[0] as StackPanel;
    //necessary to recognize new added elements
    sP.UpdateLayout();
    sP.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

    if (sP.DesiredSize.Height > MaxPageContentHeight)
        return true;
    else return false;
}

MaxPageContentHeight定义如下:

double MaxPageContentHeight = PageHeight - Margin.Top - Margin.Bottom;
相关问题