我的目标:一个 DocumentPaginator,它接受一个带有表格的 FlowDocument,它拆分表格以适应页面大小并在每个页面上重复页眉/页脚(特殊标记的 TableRowGroups)。
为了拆分表格,我必须知道其行的高度。
通过代码构建 FlowDocument 表时,TableRows 的高度/宽度为 0(当然)。如果我将此文档分配给 FlowDocumentScrollViewer(设置了 PageSize),则会计算高度等。如果不使用 UI 绑定对象,这可能吗?实例化未绑定到窗口的 FlowDocumentScrollViewer 不会强制分页/计算高度。
这就是我确定 TableRow 高度的方式(它非常适用于 FlowDocumentScrollViewer 显示的文档):
FlowDocument doc = BuildNewDocument();
// what is the scrollviewer doing with the FlowDocument?
FlowDocumentScrollViewer dv = new FlowDocumentScrollViewer();
dv.Document = doc;
dv.Arrange(new Rect(0, 0, 0, 0));
TableRowGroup dataRows = null;
foreach (Block b in doc.Blocks)
{
if (b is Table)
{
Table t = b as Table;
foreach (TableRowGroup g in t.RowGroups)
{
if ((g.Tag is String) && ((String)g.Tag == "dataRows"))
{
dataRows = g;
break;
}
}
}
if (dataRows != null)
break;
}
if (dataRows != null)
{
foreach (TableRow r in dataRows.Rows)
{
double maxCellHeight = 0.0;
foreach (TableCell c in r.Cells)
{
Rect start = c.ElementStart.GetCharacterRect(LogicalDirection.Forward);
Rect end = c.ElementEnd.GetNextInsertionPosition(LogicalDirection.Backward).GetCharacterRect(LogicalDirection.Forward);
double cellHeight = end.Bottom - start.Top;
if (cellHeight > maxCellHeight)
maxCellHeight = cellHeight;
}
System.Diagnostics.Trace.WriteLine("row " + dataRows.Rows.IndexOf(r) + " = " + maxCellHeight);
}
}
编辑:我将 FlowDocumentScrollViewer 添加到我的示例中。“排列”的调用强制 FlowDocument 计算其高度等。我想知道 FlowDocumentScrollViewer 对 FlowDocument 做了什么,所以我可以在没有 UIElement 的情况下做到这一点。可能吗?