Jonathan Small
Jonathan Small

Reputation: 1079

C# & ASP.NET MVC : using Aspose mail merge with table data and separate header information

I have a .docx mail merge document. The application builds a datasource with 13 elements (columns). I build the page with the following code:

System.Data.DataTable CondoDataTable = _repoForm.GetProtestCasesTempateCondo(record, guid);
if (CondoDataTable.Rows.Count > 0)
{
    Aspose.Words.Document templateAttachment = new Aspose.Words.Document(PathToTemplateAttachment);
    CondoDataTable.TableName = "Protest";
    templateAttachment.FieldOptions.BarcodeGenerator = new CustomBarcodeGenerator();
    templateAttachment.MailMerge.ExecuteWithRegions(CondoDataTable);
    MemoryStream protestAttachmentMemory = new MemoryStream();
    templateAttachment.Save(protestAttachmentMemory, Aspose.Words.SaveFormat.Pdf);
    docs.Add(protestAttachmentMemory);
}

All this code works exactly as I expected and the resulting document contains all the datasource data in a table.

The mail merge document makes use of {MAILMERGE TableStart:Protest} and {MAILMERGE TableEnd:Protest} to facilitate generation of the table.

Now I need to pass 4 pieces of static information which will appear in the header and footer of all pages generated.

Any ideas?

Thank you for any assistance.

Upvotes: 0

Views: 299

Answers (1)

Alexey Noskov
Alexey Noskov

Reputation: 1960

You can put merge field outside the region and execute simple mail merge using Document.Execute method to fill them with data. For example: enter image description here And fill such template by calling ExecuteWithRegions and Execute methods:

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.ExecuteWithRegions(myTableData);
doc.MailMerge.Execute(new string[] { "MyFieldOutsideRegion", "AnotherFieldOutsideRegion" },
    new string[] { "MyFieldOutsideRegion Data", "AnotherFieldOutsideRegion Data" });
doc.Save(@"C:\Temp\out.docx");

To repeat the information on each page of the generated document, just put the fields into the template's header and footer.

Upvotes: 1

Related Questions