Free Support Forum - aspose.com

How to loop inside a Word Table containing merged cells

Hello everybody!


I am creating this post because dealing with a specific issue. Following is the context:
- I am using Aspose.Words for java
- I have a Word document which is my template
- On my website, I click on a button which action is to fill the template with Java objects and to generate the final Word document.

So here is the issue : in the Word template, I have a table that looks like this :

--------------------------------------------------------------------------------------
| Title 1 | Title 2 | Title 3 | Title 4 | Title 5 |
--------------------------------------------------------------------------------------
| | | | | |
| merged | merged | | | |
| cells | cells | -------------------------------------------------
| | | | | |
| | | | | |
--------------------------------------------------------------------------------------

What I’m trying to do is to browse through a Java table and to loop in order to fill the above Word table. For instance :

--------------------------------------------------------------------------------------
| Title 1 | Title 2 | Title 3 | Title 4 | Title 5 |
--------------------------------------------------------------------------------------
| | | | | |
| { MERGE | | | | |
| FIELD | | | | |
| TableSt | | -------------------------------------------------
| art:table } | | | | { MERGE |
| | | | | FIELD |
| | | | | TableEnd:|
| | | | | table } |
--------------------------------------------------------------------------------------

The expected result is a unique Word table with 1 header and as many rows as the size of the Java object “table”. For instance, for a size of 2, it should looks like this:

--------------------------------------------------------------------------------------
| Title 1 | Title 2 | Title 3 | Title 4 | Title 5 |
--------------------------------------------------------------------------------------
| | | | | |
| | | | | |
| | | -------------------------------------------------
| | | | | |
| | | | | |
--------------------------------------------------------------------------------------
| | | | | |
| | | | | |
| | | -------------------------------------------------
| | | | | |
| | | | | |
--------------------------------------------------------------------------------------


That doesn’t work. Not only the Word table doesn’t display as expected but the rest of the generated Word document becomes corrupted.

Did I put the { MERGEFIELD TableStart / End } at the wrong places ?
Can Aspose deal with table with merged cells ?
Is it a Word problem ?
Is there another way to achieve my goal ?

FYI, if we try with a simple table made of just 1 row, the following works:

--------------------------------------------------------------------------------------
| Title 1 | Title 2 | Title 3 | Title 4 | Title 5 |
--------------------------------------------------------------------------------------
| { MERGE | | | | { MERGE |
| FIELD | | | | FIELD |
| TableSt | | | | TableEnd: |
| art:table } | | | | table } |
--------------------------------------------------------------------------------------

This generates (let say “table” is a table of size 3):

--------------------------------------------------------------------------------------
| Title 1 | Title 2 | Title 3 | Title 4 | Title 5 |
--------------------------------------------------------------------------------------
| | | | | |
| | | | | |
--------------------------------------------------------------------------------------
| | | | | |
| | | | | |
--------------------------------------------------------------------------------------
| | | | | |
| | | | | |
--------------------------------------------------------------------------------------


Any idea or help would be much appreciated :slight_smile:

Thank you! Do not hesitate if you need more details or examples.
Best regards,
Alex

Hi Alex,

Thanks for your inquiry. Aspose.Words does support merge cells. However, TableStart and TableEnd must be inside the same row in the table. Please read the rules when marking a region from here:
http://www.aspose.com/docs/display/wordsjava/Mail+Merge+with+Regions+Explained

It would be great if you please share following detail for investigation purposes.


  • Please attach your input Word document.
  • Please

    create a simple Java application that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir, thank you for your reply. Interesting to see that it has to be on the same row. We will try this solution.


I’m afraid it would be too much time consuming to give you a sample (Java + Word template) of our project. It will depend on the decision of our project manager.

But I think that the tables I gave in my previous post show the expected result, no matter the content of the cells in the table. Even producing an empty table like the one I expect would be great.

Anyway, thanks again!

Hi Alex,

Thanks for your inquiry. Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.