Let’s say for example, if in the source XML, I have the no data for the merge field ItemInformation (<<ItemInformation>>. Is there a way to remove the whole row about Item Information (the merge field and the label text as well?
[Test]
public void Test001()
{
// Create a dummy datasource.
DataSet ds = new DataSet();
ds.ReadXml(@"Test001\data.xml");
// Open template and execute mail merge.
Document doc = new Document(@"Test001\in.doc");
// Specify field merging callback that will remove paragraphs with empty mergefields.
doc.MailMerge.FieldMergingCallback = new FieldMergingCallback();
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(@"Test001\out.doc");
}
private class FieldMergingCallback: IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
if (string.IsNullOrEmpty((string) args.FieldValue))
{
// Get paragraph where the empty value should be inserted and remove it.
Paragraph paragraph = (Paragraph) args.Field.Start.GetAncestor(NodeType.Paragraph);
if (paragraph != null)
paragraph.Remove();
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// do nothing
}
}
Works wonderful with paragraph but Alexey, I should mention all of my fields are in table. So how can I get the row with the empty field and delete it? Thank you.
Regards
Tld
Hi Alexey,
Don’t worry, I got it working with my table now. I just change your code to
public void FieldMerging(FieldMergingArgs args) <? XML : NAMESPACE PREFIX = O />
{
if (string.IsNullOrEmpty((string) args.FieldValue))
{
// Get row where the empty value should be inserted and remove it.
Row row = (Row) args.Field.Start.GetAncestor(NodeType.Row);
if (row != null)
row.Remove();
}
}
Hi
It is perfect that you managed to implement what you need. Please feel free to ask in case of any issues, we will be glad to help you.
Best regards,