Hello
I use Aspose.Words to mail merge account statements etc from a share registry system. I would like to use a merge template with a header/footer with ‘Different First Page’. I have set this up and it sort of works. The problem is that the document it produces does indeed get a different first page, however, I want the different first page to apply for each account in the merged document.
That is, the template has a child table that can grow to multiple pages, and when it does I want a different footer in the extra pages. But then when page one in the template comes up for the next account I want the header/footer for the first page again, since that is the fist page for that account.
Is that possible to achieve?
Regards,
!Rob
Hi Robert,
Thanks for your inquiry. Well, I can offer you the following solution.
- Implement IFieldMergingCallback interface, track for each occurrence of the first merge field inside your ‘base’ region and insert a temporary Bookmark at the beginning of Word Table containing that merge field. Please see the following draft code:
private class HandleMergeField: IFieldMergingCallback
{
int i = 0;
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
if (args.FieldName == "ProductAddress")
{
Table table = (Table) args.Field.Start.GetAncestor(NodeType.Table);
if (table != null && table.PreviousSibling != null)
{
DocumentBuilder builder = new DocumentBuilder(args.Document);
builder.MoveTo(table.PreviousSibling);
builder.StartBookmark("bm_" + i);
builder.EndBookmark("bm_" + i);
i++;
}
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
// Do nothing
}
}
- After calling the ExecuteWithRegions method, insert a Section Break at the places where you inserted temporary Bookmarks in step 1 and after that remove those temporary Bookmarks from document: Please see the following code snippet:
…
…
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
doc.MailMerge.ExecuteWithRegions(ds);
DocumentBuilder builder = new DocumentBuilder(doc);
foreach(Bookmark bm in doc.Range.Bookmarks)
{
if (bm.Name.StartsWith("bm_"))
{
builder.MoveToBookmark(bm.Name);
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
}
doc.Range.Bookmarks.Clear();
- Finally, copy the content of all Headers/Footers of first Section into the newly created Sections as follows:
for (int i = 1; i <doc.Sections.Count; i++)
{
Section section = doc.Sections[i];
CopyHeadersFootersFromPreviousSection(section);
}
doc.FirstSection.Remove();
private static void CopyHeadersFootersFromPreviousSection(Section section)
{
Section previousSection = (Section) section.PreviousSibling;
if (previousSection == null)
return;
section.HeadersFooters.Clear();
foreach(HeaderFooter headerFooter in previousSection.HeadersFooters)
section.HeadersFooters.Add(headerFooter.Clone(true));
}
I hope, this helps.
PS: I have also simplified your template document by removing the extra empty Paragraphs from the end after the TableEnd:Base merge field. Please find the attachment.
Best regards,