Linq reporting - How to insert page breaks?

When using the Linq reporting syntax, is there a way to force a page break?
The use case is something similar to an invoice, where each record (invoice) must start on a new page.

@ebrito,

You can build logic on the following code to be able to insert a page beak during LINQ Reporting process:

DocumentBuilder builder = new DocumentBuilder();
builder.Write("<<foreach [in items]>>");
builder.Write("Page #<<[NumberOf()]>>");
builder.Write("<<[ControlChar.PageBreak]>>");
builder.Write("<</foreach>>");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(ControlChar));

ArrayList items = new ArrayList();
for (int i = 0; i < 3; i++)
    items.Add(new object());

engine.BuildReport(builder.Document, items, "items");
builder.Document.Save("E:\\Temp\\output.docx");

Thanks. I’m not using DocumentBuilder.
I just load a docx template using

new Document(fileName)

I tried adding <<[ControlChar.PageBreak]>> directly to the docx template file but id didn’t work.

To be exact, the code is more like this:

Document document;
DataSet dataSet = this.GetDataSet();
string base64File = this.GetFileAsBase64String();
ReportingEngine engine = new ReportingEngine
{
    Options = ReportBuildOptions.AllowMissingMembers | ReportBuildOptions.RemoveEmptyParagraphs
};

using (var stream = new MemoryStream(Convert.FromBase64String(base64File)))
{
    document = new Document(stream);
}

engine.KnownTypes.Add(typeof(ControlChar));
engine.BuildReport(document, dataSet, "data");

And inside the template file (docx):

<<foreach [in items]>>
    Page <<[NumberOf()]>>
    <<[ControlChar.PageBreak]>>
<</foreach>>

It completelly ignores ControlChar.PageBreak.
I’m using Aspose.Words 19.10.0

@ebrito,

Please see these input/output Word documents (Docs.zip (17.6 KB)) and try running the following code:

Document doc = new Document("E:\\Temp\\in.docx");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(ControlChar));

ArrayList items = new ArrayList();
for (int i = 0; i < 3; i++)
    items.Add(new object());

engine.BuildReport(doc, items, "items");

doc.Save("E:\\Temp\\19.10.docx");

Looks like the issue is with the remove empty paragraph option. Appears to be a bug to me.
When you set this this option it ignores the ControlChar.PageBreak.

var engine = new ReportingEngine
{
    Options = ReportBuildOptions.RemoveEmptyParagraphs
};

Unrelated to the above, this will always result in a blank page at the end of the document.

<<foreach [in items]>>
    Page <<[NumberOf()]>>
    <<[ControlChar.PageBreak]>>
 <</foreach>>

I can’t hard code something to just remove the last page since not all templates will be the same.
Is there a way ignore the ControlChar.PageBreak when processing the last record? Something in the template syntax would be even better.

@ebrito,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-19505. We will further look into the details of this requirement and will keep you updated on the status of the linked issue. We apologize for any inconvenience.

I’m assuming the ticket is for the “bug”. Thank you.
I can work without the “ReportBuildOptions.RemoveEmptyParagraphs” for now, no problem.
Meanwhile, could you please let me know the proper way to handle page breaks when working with link reporting? For example if I’m creating invoices, how can I avoid the empty page at the end?

@ebrito,

We have logged your concerns/questions in our issue tracking system and will keep you posted on any further updates.

The issues you have found earlier (filed as WORDSNET-19505) have been fixed in this Aspose.Words for .NET 19.12 update and this Aspose.Words for Java 19.12 update.

A post was split to a new topic: Linq reporting - How to insert page breaks