我有一个
.docx
邮件合并文档。该应用程序构建了一个包含 13 个元素(列)的数据源。我使用以下代码构建页面:
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);
}
所有这些代码的工作方式完全符合我的预期,并且生成的文档包含表中的所有数据源数据。
邮件合并文档利用
{MAILMERGE TableStart:Protest}
和 {MAILMERGE TableEnd:Protest}
来方便生成表格。
现在我需要传递 4 条静态信息,这些信息将出现在生成的所有页面的页眉和页脚中。
有什么想法吗?
感谢您的帮助。
您可以将合并字段放在区域之外,并使用 Document.Execute 方法执行简单的邮件合并,以用数据填充它们。例如: 并通过调用
ExecuteWithRegions
和 Execute
方法来填充这样的模板:
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");
要在生成文档的每一页上重复信息,只需将字段放入模板的页眉和页脚中即可。