When using 'IF' condition in a word template, characters starting with a double quote do not appear in the PDF

When using ‘IF’ condition in a word template, characters starting with a double quote do not appear in the PDF

We are using aspose.word 22.12 jar.

Following is string value
Input value : this is a “single quote” and this is a “double quote”

Output value in pdf : this is a “single quote” and this is a

Above scenario happen only when using following mergfield condition in word document

{IF{ MERGEFIELD Print_Notes * MERGEFORMAT} = “TRUE” "Notes: " “” *MERGEFORMAT} {IF { MERGEFIELD Print_Notes * MERGEFORMAT } =“TRUE” { MERGEFIELD Line_Notes * MERGEFORMAT } “” *MERGEFORMAT }

Sample code has been attached
ToForum.zip (116.2 KB)

@akondewar The problem is caused when Document.updateFields method is called after executing mail merge. This usually is not necessary since executing mail merge internally calls this method. However, As I can see in your code you modify the document after executing mail merge, so calling Document.updateFields might be required. In your case you can resolve the problem by setting the following cleanup option before executing mail merge:

doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);

In this case containing IF fields will be unlinked, i.e. replaced with static text and subsequent call of Document.updateFields will not affect the value.

Hi Alexey,
The solution has been implemented, but we are seeing regression effects as a result of its implementation. Could you please guide me if anything is missing.

Issue : merged
RSTK-16084.zip (70.4 KB)

field tag printed in generated pdf document when value is null.

«Product_Description»

If I commented ‘doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);’ line then it will not print merged field tag.

Word template file, Sample code and output is attached.

@akondewar The field Product_Description is outside the region, so it is not filled and is not cleared by MailMergeCleanupOptions upon executing mail merge with regions. So you should either execute simple mail merge or delete fields before saving document:

// Reading the data from xml file for the mail merge
DataSet dataSet = new DataSet();
dataSet.readXml("C:\\Temp\\RSTK-16084.xml");

Document doc = new Document("C:\\Temp\\in.docx");
int cleanupOptions = MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
        MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS |
        MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS;

doc.getMailMerge().setCleanupOptions(cleanupOptions);
doc.getMailMerge().executeWithRegions(dataSet);

// Remove remaining merge fields which might be outside the region.
// Alternatively you can execute simple ail merge for these fields.
doc.getMailMerge().deleteFields();

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

Thanks for your valuable information.
Provided solution already implemented in our existing code, it was working fine for (15.11 jar) now we upgrade the aspose jar (22.12).

We are using boiler code since 2015 reference

https://docs.aspose.com/words/java/how-to-apply-custom-logic-to-unmerged-regions/

Once we upgrade the jar following issue has been fixed in the code

  1. https://forum.aspose.com/t/some-of-the-texts-has-the-last-character-followed-by-a-space-for-japanes-font-msgothic-using-aspose-java-word-on-linux/267071

16-Nov-2023.zip (69.2 KB)

  1. https://forum.aspose.com/t/getting-error-java-lang-nullpointerexception-page-mergeformat-numpages-mergeformat-tag-in-word-file/265588/3

  2. https://forum.aspose.com/t/when-using-if-condition-in-a-word-template-characters-starting-with-a-double-quote-do-not-appear-in-the-pdf/268188

  3. https://forum.aspose.com/t/how-to-convert-word-to-pdf-without-compressing-images-using-java-aspose-words-document/258334/4

  4. https://forum.aspose.com/t/mm-dd-yyyy-format-is-not-working-while-localization-is-use/257492/3

Please let us know if we are missing some thing
Code has been attached

@akondewar I have inspected your code once again and as I can see you did not follow the suggestion from my previous answer. In your code you set MailMergeCleanupOptions in two places:

...
merge.setCleanupOptions(merge.getCleanupOptions() & ~MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS) ;
....
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS); //RSTK-14782	

When you set cleanup options the second time, the previously set cleanup options are overridden and no longer have effect.

Also you call doc.updateFields() twice, this is not required, please try calling doc.updateFields() once just before saving the document, updateFields internally updated document layout and layout is cached so doc.getMailMerge().deleteFields() might not have effect after rendering the document to PDF. So please try changing your code like this:

doc.getMailMerge().deleteFields(); //RSTK-16084
doc.updateFields(); 
String path1 = strPath+ "output.pdf";
doc.save(path1,(PdfSaveOptions)saveOption) ; //Image compreess issues

Thanks Alexey,
We already using following code in removeUnusedRegions() method and called our method.

int cleanupOptions = MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
        MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS |
        MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS;

doc.getMailMerge().setCleanupOptions(cleanupOptions);
doc.getMailMerge().executeWithRegions(dataSet);

16-Nov-2023_1.zip (69.1 KB)

So requesting you kindly check attached code. Please let us know is any thing is missing.

@akondewar removeUnusedRegions method is not required, simply set MailMergeCleanupOptions before executing mail merge in your code, just like in the code provided in this answer.