Hello,
We have a requirement to generate a pdf from a word template that is created with variables using mergeField option in word. Once we create the template, we use aspose mailmerge to populate the word with all the data and then create a pdf. We have been using this feature for a lot of static templates and works well. However, we now have a requirement to generate a template that may or may not have data for all the fields and when there is no data we should not display the fields. Is there any option in aspose for dynamic templates. Attached a sample template. When there is not data for all of the fields under a section, we want the complete section to be skipped from displaying in the pdf. For example Plaintiff section should be skipped when there is no data. One way of doing that is my making the labels as variables but our actual templated are very huge and that option will not be feasible. Any inputs on how to achieve this using apsose?
SampleTemplate.docx (48.5 KB)
Thanks,
B
@judiciary
Aspose.PDF does not offer feature to modify and update Word files. It is served by Aspose.Words along with the feature of generating PDF from DOCX. Therefore, the topic is moved to the respective category where you will be assisted shortly.
@judiciary You can use MailMergeCleanupOptions to remove unused fields and regions from the template upon executing mail merge.
In your template however, the cleanup options will not help to remove the title of the section:
The content marked by green can be removed using
MailMergeCleanupOptions.RemoveUnusedRegions
, however the content marked with red is not part of the region. You can remove it in
IFieldMergingCallback. For example see the following code:
Document doc = new Document(@"C:\Temp\in.doc");
doc.MailMerge.FieldMergingCallback = new RemoveTitleCallback();
doc.MailMerge.Execute(new string[] { "noOfPlaintiffs" }, new object[] { "" });
doc.Save(@"C:\Temp\out.docx");
private class RemoveTitleCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
if (args.FieldName == "noOfPlaintiffs" && string.IsNullOrEmpty(args.FieldValue.ToString()))
{
args.Field.Start.GetAncestor(NodeType.Table).Remove();
}
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// do nothing
}
}