Delete mail merge field using C#

Hi -
I have an interesting problem with the deletion of MergeFields from an Aspose document.
The MergeFields in the Aspose document are first populated with data using MailMerge.
Then, any remaining MergeFields that haven't been populated are removed using MailMerge.DeleteFields on that document.

I save the document to a Word disk-file, and also print out the Aspose document in memory
using Aspose.Print with a PrinterSettings object. The disk-file looks fine. All of the remaining
MergeFields have been deleted. However, the remaining (unpopulated) MergeFields that I thought
were deleted still display when printed at the printer.

Have I missed something obvious?
When MergeFields are deleted, the Aspose document doesn't have to be saved to a disk-file
before it can be printed, does it?
Thanks for your help -
bob kells

Hi -

One more bit of information.

We're using version 10.5 of Aspose.Words.dll

Hi Bob,

Thanks for your query. I have tested the scenario and have not found any issue at my end. Please use the latest version of Aspose.Words for .NET 11.1.0.

Hi Tahir -

Could you check out these forms?

The Empty document is the original form.

The Merged document is the form after the MailMerge operation, and has been saved to a disk-file through Document.Save. This shows that the unpopulated Merge fields have been deleted.

The pdf-file is a scan of what actually comes out from the printer from the Document.Print command. This shows the unpopulated Merge fields still on the form.

I have tried deleting the unpopulated MergeFields using both builder.MoveToMergeField and Document.MailMerge.DeleteFields().

Let me know if you see anything out of place.

Thanks - bob

Hi Bob,


Thanks for your inquiry.

Sometimes during mail merge page layout is updated. For example if a PAGE field requires update then the document must be built in memory to update this field. This layout is stored for later use. Next when you remove the fields from the document they will be removed from the DOM, however the page layout stored in memory is not updated. Then when you save to PDF, image or print etc the cached rendered version of the document is used that still contains the fields is used. This accounts for the fields still appearing during print.

A simple way to make sure that this is avoided is to call doc.UpdatePageLayout before printing or saving to a rendered format.

If we can help with anything else, please feel free to ask.

Thanks,

Adam -

Thank you so much.

I won't be able to test this until Monday but will let you know what happens.

I really appreciate the help -

bob