Aspose LINQ Reporting 自动启动新页面

问题描述 投票:0回答:1

我正在使用 Aspose LINQ Reporting 生成 Word 文档。 word模板如下所示 我想要的是:

  1. 当数据量小于表格行数时,会添加空白列,下方描述空白。
  2. 当数据量大于表格行数时,会自动另起一页,包含页眉和页脚

word template

我在网上找了好久。但没有用。请帮忙或尝试给出一些如何实现这一目标的想法。

c# aspose aspose.words
1个回答
0
投票

恐怕没有直接的方法来实现你所需要的。您可以将表头放入文档页眉中,将表页脚放入文档页脚中。

注意:在 MS Word 中,表格不能是故事的最后一个节点(正文、页眉或页脚),并且表格后面总是有空段落。因此,为了避免页脚后出现空白,您可以减小空白段落的字体大小:

这将允许在报告的每一页上重复表页脚和页眉。但由于 MS Word 文档本质上是流动的,因此没有页面的概念,也没有直接的方法来确定页面上是否有空白区域。 Aspose.Words 有自己的布局引擎,并使用 LayoutCollectorLayoutEnumerator 来计算表格后面的空白空间。这将允许在构建报告后向表中添加空行。例如看下面的代码:

// Some dummy data.
string[] dummyData = new string[100];
for (int i = 0; i < dummyData.Length; i++)
{
    dummyData[i] = $"Dummy Data {i}";
}

// Open template.
Document doc = new Document(@"C:\Temp\in.docx");
// Build report.
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dummyData, "items");

// Suppose there is one table in the table.
// Use LayoutCollector and LayoutEnumerator to calculate last table row bounds
// to check whether additional empty rows must be added.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Calculate visible page bounds
PageSetup ps = doc.FirstSection.PageSetup;
RectangleF pageRect = new RectangleF(
    (float)ps.LeftMargin,
    (float)ps.TopMargin,
    (float)(ps.PageWidth - ps.LeftMargin - ps.RightMargin),
    (float)(ps.PageHeight - ps.TopMargin - ps.BottomMargin));

// Get bounds of the last row of the table.
Table table = doc.FirstSection.Body.Tables[0];
enumerator.Current = collector.GetEntity(table.LastRow.FirstCell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Row)
    enumerator.MoveParent();
RectangleF lastRowBounds = enumerator.Rectangle;

// Check therer there is empty space avaialbel on the page.
if (lastRowBounds.Bottom < pageRect.Bottom)
{
    float availableEmptySpace = pageRect.Bottom - lastRowBounds.Bottom;
    Row emptyRow = (Row)table.LastRow.Clone(true);
    emptyRow.GetChildNodes(NodeType.Paragraph, true).Clear();
    int emptyRowsCount = (int)(availableEmptySpace / lastRowBounds.Height) - 1;
    for (int i = 0; i < emptyRowsCount; i++)
        table.AppendChild(emptyRow.Clone(true));
}

doc.Save(@"C:\Temp\out.docx");

结果如下:

© www.soinside.com 2019 - 2024. All rights reserved.