MailMerge and Title field

Hello!
We are using Aspose.Words for Java (version 21.2.0) to convert doc/docx files to PDF and also to using MailMerge function to fill some predefined data in our documents. Recently our customers reached us with the problem regarding change of content during this operation. I created a sample document to reproduce this issue and have a question if this behaviour is configurable or can be disabled?
Steps to reproduce the issue are as follows:

  1. Create field TITLE in document (note: this is not a MERGEFIELD and shows as { TITLE "Title Placeholder" \* MERGEFORMAT }
  2. In normal editing mode manually modify the text of a placeholder to Actual Title (in mergefields view the field still shows as “Placeholder”, but in normal view it shows as our modified text)
  3. Add a MERGEFIELD in document (in our case it is { MERGEFIELD Name \* MERGEFORMAT }
  4. Execute some code with MailMerge functional to fill Name mergefield, ie:
Document document = new Document(SRC_PATH);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setSaveFormat(SaveFormat.PDF);
document.getMailMerge().execute(new String[]{"Name"}, new Object[]{"John Doe"});
document.save(DST_PATH, saveOptions);

It may be done with or without converting to PDF format (doesn’t matter if I select different SaveFormat here).
Every time that MailMerge is used - our manually edited text (Actual Title) disappears in the output result - output text resets to TITLE placeholder - “Title Placeholder”.
So my question is - is there any way or option to avoid this TITLE editing (or resetting)? To just update mergefields and to not modify any other document fields?

Also, this behaviour reproduces even if document doesn’t have any mergefields (only TITLE field), or when executed “empty” MailMerge, i.e. document.getMailMerge().execute(new String[]{}, new Object[]{});

Sample document (.docx) and produced result (.pdf) by this code are attached here
sample.docx (11.3 KB)
sample_mergeFields.pdf (12.3 KB)

@Dmitry.Ignatov This occurs because Aspose.Words update fields in the document upon executing mail merge. You can disable updating or unlinking the fields of certain types to prevent this. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_TITLE)
    {
        // Lock title field to prevent it's updating.
        f.isLocked(true);
        // You can also unlink (replace field with it's value)
        // f.unlink();
    }
}

doc.getMailMerge().execute(new String[] { "Name" }, new Object[] { "John Doe" });

doc.save("C:\\Temp\\out.pdf");

Hello, @alexey.noskov thank you for your reply! I checked this code and it worked.
However, is there any way to lock all “autofilled fields” For example, if user adds author, subject or another fields? I see there are 97 types of fields, but how to determine which types i should lock to prevent them from autofilling/updating? Are there some types of fields that should not be locked? (For example lock all types of fields except some numbers)? Or should i check all types by names against types of fields in Microsoft Word and lock all except mergefield type?

@Dmitry.Ignatov Actually editing field value directly is incorrect using of fields. Once you update field either in MS Word (F9) or in Aspose.Words, field value will be updated. So, I am afraid, there is no way to determine whether a particular field should be updated or not.
Normally fields should not be locked in the document unless you are sure it is required.

@alexey.noskov okay, thank you again for pointing me to the right direction! We will try our way to lock all fields (or as much as possible) and see the results, if it fits our requirements.

1 Like