WPF FlowDocument确定一个块将在哪个页面上

如果我有一个var flow = new FlowDocument(),并且向其BlockCollection添加了几个块:

if (m_Blocks == null || m_Blocks.Count <= 0)
  return flow;

flow.Blocks.AddRange(m_Blocks.ToList());
var paginatorSource = flow as IDocumentPaginatorSource;
paginatorSource.DocumentPaginator.ComputePageCount();
var pageCount = paginatorSource.DocumentPaginator.PageCount;

我能够确定此文档包含多少页。

问题

如果页数大于1,我该如何找出文档的哪些块无法放入首页?我在Block类上找不到任何可以帮助我确定这一点的东西。但是由于分页者可以ComputePageCount(),所以我认为这是可能的。

xbl_lkz 回答:WPF FlowDocument确定一个块将在哪个页面上

对于要完成的工作,我采取了蛮力的方法。基本上,我需要在每页顶部重复一个Paragraph。我想这不是最高效的方法,但是它完成了工作:

protected FlowDocument RenderBody()
{
  var flow = new FlowDocument
  {
    PageWidth = PaperSizeDPI.Width,PageHeight = PaperSizeDPI.Height,ColumnGap = 0,ColumnWidth = PaperSizeDPI.Width,PagePadding = new Thickness(MarginDPI.Left,MarginDPI.Top + HeaderHeightDPI,MarginDPI.Right,MarginDPI.Bottom + FooterHeightDPI)
  };

  if (m_Blocks == null || m_Blocks.Count <= 0)
    return flow;

  // Get page count before deciding to deal with repeat blocks.
  // Adding blocks to the FlowDocument is required before invoking
  // `DocumentPaginator.ComputePageCount()`.
  flow.Blocks.AddRange(m_Blocks.ToList());

  int GetPageCount()
  {
    var paginatorSource = (IDocumentPaginatorSource) flow;
    paginatorSource.DocumentPaginator.ComputePageCount();
    return paginatorSource.DocumentPaginator.PageCount;
  }

  // If there is only one page,no roll-over of headers necessary
  if (GetPageCount() <= 1) return flow;

  // If we don't have a header to repeat,we assume the report is correct already
  if (PrintTemplateConverter.BuildRepeatableHeader == null) return flow;

  // Build the FlowDocument,one block at a time,checking if the page count
  // changes so that we can inject a repeatable header
  flow.Blocks.Clear();
  var pageCount = 1;

  var blocks = m_Blocks.ToList();

  for (var blockIndex = 0; blockIndex < blocks.Count; blockIndex++)
  {
    flow.Blocks.Add(blocks[blockIndex]);
    if (GetPageCount() <= pageCount) continue;

    // If this block moved us to a new page,inject repeatable header in its spot.
    // `flow.Blocks.AddRange(...)` ignores duplicate blocks (by reference).
    // This is why I go through the "pain" of making a whole new Header block
    // instance.
    var blocksSoFar = flow.Blocks.ToList();
    flow.Blocks.Clear();
    blocksSoFar.Insert(blockIndex,PrintTemplateConverter.BuildRepeatableHeader());

    flow.Blocks.AddRange(blocksSoFar);
    pageCount = GetPageCount();
  }

  return flow;
}

我希望这对以后的人有帮助!

本文链接:https://www.f2er.com/3033468.html

大家都在问