Page number restart in region mail merge

Hi Aspose Team,
I need to perform a region mail merge (with two nested regions as master/child) and force to restart page number for each new record.
The structure is like this:

[master_region:Start]

<<master_merge_field_1>>,
<<master_merge_filed_2>>,

<<master_merge_field_n>>

[child_region:Start]
<<child_merge_field>>
[child_region:End]

[master_region:End]

In few words I would like that when master record starts the page number could be restarted.
But since regions must be placed on the same section, how can I get a restart?

Thank you.
Kind Regards
Andrea

Hi Andrea,

Thanks for your inquiry.

I think you can use the code like below to achieve what you are looking for. In your template document you will need to add a bookmark called “RegionMarker” to the starting paragraph of your master region.

doc.MailMerge.ExecuteWithRegions(data);
// The bookmark will be duplicated as the start of each region.
// Insert a new section break at each of these bookmarks.
foreach(Bookmark bookmark in doc.Range.Bookmarks)
{
    if (bookmark.Name == "RegionMarker")
    {
        Paragraph para = (Paragraph) bookmark.BookmarkStart.ParentNode;
        builder.MoveTo(para);
        builder.InsertBreak(BreakType.SectionBreakNewPage);
    }
}
// It's easier to save and reopen the document so the new sections are loaded as Section nodes.
MemoryStream tempStream = new MemoryStream();
doc.Save(tempStream, SaveFormat.Docx);
doc = new Document(tempStream);
Section section = (Section) doc.Range.Bookmarks["RegionMarker"].BookmarkStart.GetAncestor(NodeType.Section);
// Reset page numbering for each of the region sections.
while (section != null)
{
    section.PageSetup.RestartPageNumbering = true;
    section.PageSetup.PageStartingNumber = 1;
    section = (Section) section.NextSibling;
}
// Remove the marker bookmark.
doc.Range.Bookmarks["RegionMarker"].Remove();

If we can help with anything else, please feel free to ask.

Thanks,

Hi Adam,

Thank you for your quick response.

Your suggestion is clear, simple: it works perfectly. I have just adapted the snippet to used in Java code and added a check in order to skip page restart, just for the first record.

Is that possible, just to complete the footer information, add the pagcount of the single section too, like this: page x of N
where N is the page count into that current section (not the overall count)?

Thanks

Kind Regards
Andrea Caracciolo

Hi Andrea,

It’s great that the code is working as expected. In regards to your additional request, I think you can use the code like below to achieve what you are looking for.

Section section = (Section) doc.Range.Bookmarks["RegionMarker"].BookmarkStart.GetAncestor(NodeType.Section);
// Reset page numbering for each of the region sections.
while (section != null)
{
    section.PageSetup.RestartPageNumbering = true;
    section.PageSetup.PageStartingNumber = 1;
    builder.MoveToSection(doc.Sections.IndexOf(section));
    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    builder.InsertField("PAGE");
    builder.Writeln(" of ");
    builder.InsertField("SECTIONPAGES");
    section = (Section) section.NextSibling;
}

If we can help with anything else, please feel free to ask.

Thanks,

Hi Adam,
That is what I was searching for.

Thank you again.

Kind Regards.
Andrea Caracciolo