Bookmark Not Defined error when saving DOCX to PDF after mailmerge

Reproduced using the latest version of Aspose (14.2.0). This issue does not occur in Aspose.Words 9.4.0, however we need to upgrade to something newer to address some other bugs in Aspose.Words that have since been fixed.

I am attaching the sample document and code used to reproduce.

We have .docx that is going through a mailmerge. The result is then saved to PDF. However, once saved to PDF, we have fields showing up as “Error! Bookmark Not Defined”.

If I inspect the source document, and hit Alt-F9 to show hidden fields, I can see there are some additional funky fields that surround the “normal” merge field. I’m not sure if these are artifacts from a copy/paste from an older version of Word, but in either case I believe they are the source of the error. Without Alt-F9, the document looks like this:

½SAL_DEP_OPTIONS╗

However, if I hit Alt-F9, I see the following:

{ MERGEFIELD "SAL_DEP_OPTIONS" \# "#,##0"}{{ MERGEFIELD COM_ID }+{MERGEFIELD COM_ID}+{MERGEFIELD COM_ID}}

What is odd is the merge field COM_ID does not show unless I hit Alt-F9 (it is not a field we actually want in the document).

The data for SAL_DEP_OPTIONS is populating fine. If I make sure I include merge data for COM_ID, something weird happens:

  1. I still get the “Error! Bookmark not defined!” message in the PDF
  2. If I save the merged document to .docx, and then hit Alt-F9, I can see that the hidden merge fields for COM_ID now look like this:

FOOBAR {""+""+""}
Where FOOBAR is the value of SAL_DEP_OPTIONS and “” is the value assigned to COM_ID. FOOBAR is visible normally, while the extraneous is only visible if I ALT-F9.

If I remove the merge field SAL_DEP_OPTIONS and recreate it, the issue goes away. However, this problem has been replicated across about 2500 documents, and in more than one place. It was not an issue when we were on Aspose 9.4 (hence it went for so long unnoticed), but now we are stuck and cannot upgrade.

My questions are:

  1. Is there a way to suppress Bookmark Not Defined errors when saving to PDF?
  2. Alternatively, is there a way to programatically locate the Bookmark Not Defined problems and remove them from the document, prior to saving to PDF?
  3. Would you consider this a bug, since it did not have this problem in 9.4, but now does in 14.2?

Documents and screenshots (showing before and afters, including w/ Alt-F9 mode) attached. Code used:

var doc = new Document("test.docx");
doc.MailMerge.Execute(new []
{
    "SAL_DEP_OPTIONS",
    "COM_ID"
}, new []
{
    "FOOBAR",
    ""
});
doc.Save(@"out.docx");
doc.Save(@"out.pdf", SaveFormat.Pdf);

Hi Orion,

Thanks for your inquiry. I am working over your query and will get back to you soon.

Hi Orion,

Thanks for your patience. I have worked with your document and have found that this is not a bug. Please look at the following mail merge fields of your document.

{ { MERGEFIELD COM_ID } + { MERGEFIELD COM_ID } + { MERGEFIELD COM_ID } }

In your code you are replacing COM_ID with empty string. The result will be { “”+""+"" }. Which is incorrect field.

Please use the following code example to remove such fields. Hope this helps you. Please let us know if you have any more queries.

var doc = new Document(MyDir + "test.docx");
doc.MailMerge.Execute(new []
{
    "SAL_DEP_OPTIONS",
    "COM_ID"
}, new []
{
    "FOOBAR",
    ""
});
foreach(Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldNone)
    {
        if (field.Result.ToLower().Trim().StartsWith("error"))
        {
            field.Remove();
        }
    }
}
doc.Save(MyDir + "out.docx", SaveFormat.Docx);
doc.Save(MyDir + "out.pdf", SaveFormat.Pdf);

Aspose.Words 9.4 throws the exception ‘The document appears to be corrupted and cannot be loaded.’ while loading the document.