I have one table sheet on the document and I wanted to display checkbox instead of some values.
But MoveToMergeField function does not work properly. Please see below code sample.
Interesting thing is If I call the function MoveToMergeField two time, it works.
To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:
Your simplified input Word document
Aspose.Words 19.1 generated output DOCX document showing the undesired behavior
Your expected document showing the correct output. You can create expected document by using MS Word.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
Do you want me to prepare input/output word documents and to test it on the version 19.1 for you?
I spent whole day to investigate and tried to figure out the issue. I think that I provided you enough information how it is working and expected and unexpected results. If you see the pictures things are clear.
So, rest of things are your work guys, because this is your issue not mine.
if you see those documents you will see the differences and you will understand the issue.
Anyways, let me try to make you understand.
If you see the data that is provided in the code, there are 2 tables: category and product.
Category table has 2 rows such as Fruit and Beverage.
Product table has 2 rows (Coca cola and Pepsi) that have the value of category Beverage. There is no record for the category Fruit.
Those 2 tables are joined by their category id.
So that, the output document should be as follows:
it should generate as many sheets as how many records the category table has. Output doc should have 2 sheets for Fruit and Beverage.
1st sheet (for Fruit) should not have any records. Because there is no products for this category.
2nd sheet (for Beverage) should have 2 records. Because there are 2 records (coca cola and pepsi) for this category.
“Status” column of the sheets should only contain check boxes instead of plain text such as “Active” or “Deactive”.
Now, if we see the output document 19.1-once.pdf, 2) and 4) are incorrect.
You will see that “Status” column’s check boxes of the first row of the 2nd sheet moved into 1st sheet and it takes plain text “Active” instead of check boxes.
Adding a record into the joined table, it works.
But, this is not correct solution to resolve the issue. The bug is still there.
Our case is, all data comes from database depending on business logic and I cannot add or modify the data.
Is it the bug that needs to be resolved or not?
Otherwise, I have the workaround that I found it luckily mentioned earlier.
This is not a bug in Aspose.Words API. Please see these input/output documents (Documents.zip (23.9 KB)) and try running the following code. You just need to replace:
Document doc = new Document(“E:\temp\in-189238.docx”);
//doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
//doc.MailMerge.FieldMergingCallback = new HandleMergeCheckBox();
doc.MailMerge.ExecuteWithRegions(ds);
//doc.MailMerge.DeleteFields(); // it removes empty MailMerge fields
doc.Save("E:\\temp\\19.1-RemoveEmptyParagraphs.docx");
In the 19.1-RemoveUnusedRegions.docx, you can see MailMergeCleanupOptions.RemoveUnusedRegions removes the whole row with its content. So, inside IFieldMergingCallback.FieldMerging, a single MoveToMergeField call will move the cursor to the product_status field in second table.
However, as you can see in the ‘19.1-RemoveEmptyParagraphs.docx’, the MailMergeCleanupOptions.RemoveEmptyParagraphs removes empty paragraphs around the first table (but not the mail merge region). This is the behavior of MS Word that Microsoft Word merges two or more consecutive Tables into one big Table if there are no Paragraphs in between them. So, inside IFieldMergingCallback.FieldMerging, a single MoveToMergeField call will move the cursor to the product_status field in first table row. And to insert the value at the correct position, you had to call MoveToMergeField again. (this is Aspose.Words behavior e.g. if Word document contains three merge fields with same name and to be able to insert value only at the third merge field, you will have to call MoveToMergeField method three times.)
So, this is the expected behavior. If we can help you with anything, please feel free to ask.