We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert dynamic row column in table

Hi,

I have one template which has one row and two columns. one column has a token which I have to replace by value and add number of rows and columns dynamically. I have attached doc one has input table and other has expected output table. Could you please give some details how to build the expected output table?
Info.zip (102.3 KB)
Thanks in advance.

@vke3,

Thanks for your inquiry. You can create the desired table using Aspose.Words. In this case, you need to create the table that has vertically merged cells. Please read the following article about working with merged cells.
Working with Merged Cells

@tahir.manzoor,

Thank you for your support. Actually I am facing problem in getting the cursor point where my token present in input template. I have shared methods which I am using for building table. Also sharing a input template and the Output which I am getting.Info.zip (40.8 KB)

You can see that the values which I want to insert in table that is starting from new row instead of the row where my token present in template resulting a blank row in generated output doc.

@vke3,

Thanks for your inquiry. Following code example shows how to merge the cells vertically of the existing table. Hope this helps you.

Document doc = new Document(MyDir + "Input Template.docx");
Table table  = doc.getFirstSection().getBody().getTables().get(0);
Row row = table.getRows().get(6);
row.getFirstCell().getCellFormat().setVerticalMerge(CellMerge.FIRST);

Row row1 = (Row)row.getParentTable().insertAfter(row.deepClone(true), row);
row1.getFirstCell().removeAllChildren();
row1.getFirstCell().getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);


Row row2 = (Row)row.getParentTable().insertAfter(row.deepClone(true), row);
row2.getFirstCell().removeAllChildren();
row2.getFirstCell().getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);


Row row3 = (Row)row.getParentTable().insertAfter(row.deepClone(true), row);
row3.getFirstCell().removeAllChildren();
row3.getFirstCell().getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

doc.save(MyDir + "18.9.docx");

@tahir.manzoor

Thank you for your support. In my case I implemented the different solution but your provided solution helped me a lot to build that.
There is one issue, As I am adding my new table in existing table row where my input token present so there is one extra new line gets added. I gone throw with different solution but I didn’t found anything which solve my problem. Do you have any idea about this?
I can’t use row.remove() method as that single row contains my whole table which I want in output.
I have attached one doc for your reference. Reference Doc.PNG (4.0 KB)

@vke3,

Thanks for your inquiry. Please share your output document that shows the undesired behavior. We will then share the code example according to your requirement.

@tahir.manzoor,

Sharing the Output table image Output.PNG (56.6 KB)

And can we change the font of table cell content? After generation of doc its changing my font from Arial to caliber

@vke3,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

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.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor,

I have already shared the input and expected output file along with the output I am getting. Can’t share the whole source code as it’s too big and too many flow. but in first massage i.e. in Info.zip I have shared the table formation method with you and how I am inserting the two docs which gives the idea of implementation. Thanks you.

@vke3,

Thanks for your inquiry. You shared two .zip files in this thread. The first ZIP file contains the screenshot of output document. For second ZIP file, we share the code example in this thread.

For this issue, we need your input, output and expected output documents. Please share the documents for investigation. We will then provide you more information about your query.

@tahir.manzoor,

Please find the attached document templates. Files.zip (47.4 KB)

@vke3,

Thanks for sharing the detail. Unfortunately, we have not found the output document that shows the undesired behavior in the ZIP file.

In your case, we suggest you please iterate over the table’s row after generating the table and remove the Row node that has no text inside. You can use Row.toString(SaveFormat.TEXT) method to get the text of table’s row and remove the row using Row.remove() method.

If you still face problem, please attach the output Word file that shows the undesired behavior. We will then share the code example according to your requirement.

@tahir.manzoor,

There is a file named “Output_Which_I_am_getting.png” which shows the undesired behavior. I have highlighted the part also.
I can’t use the row.remove() mehod to remove the row, as I am adding the table inside the one row of the table.( mentioned in ReadMe.txt file in the Zip).
In short suppose I have a table with number of rows 5. And after 3rd row I have to add some extra 2/3 rows. In my case I don’t have the input template further in my process. I just have the position of the row. So I am creating a required table in some other document and inserting that document(table content) to the original template( i.e inside the 3rd row of our input table). So If I try to remove the 3rd row ( which has a content of my added new table) it will remove all my dynamically added rows. In this case I don’t want to remove the row, I want to remove that space in row which I highlighted in output image.

@vke3,

Thanks for sharing the detail. Unfortunately, it is difficult to say what the problem is without problematic output Word document and code example to reproduce this issue. To ensure a timely and accurate response, please attach the requested detail here for testing. We will investigate the issue and provide you modified code example.

@tahir.manzoor,

Please find the attachment. As I mentioned earlier, I have shared the methods of code. Can’t share the whole running code as the process is big(Rather compile java files). So shared the methods with you which will help you to understand the flow. As mentioned earlier, I have tokens in my input template and I am processing that token and getting data and creating output doc/pdf from that data. you can compare the Input template and output template for the sameFiles.zip (54.1 KB)

@vke3,

Thanks for sharing the document. In your document, there is an empty paragraph before the nested table. You can remove this paragraph using the following code snippet. Hope this helps you.

Document doc = new Document(MyDir + "PLD_SZ8 Product Legacy 2015-11-2_B_Draft.docx");
NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table table : (Iterable<Table>) tables)
{
    if(table.getAncestor(NodeType.CELL) != null && table.getPreviousSibling() != null
            && table.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH
            && table.getPreviousSibling().toString(SaveFormat.TEXT).trim().length() == 0)
    {
        table.getPreviousSibling().remove();
    }
}
doc.save(MyDir + "18.9.docx");

@tahir.manzoor,

This is what I was looking for :slight_smile:
It worked in my scenario. Thanks a lost for your continuous support. :+1:

@tahir.manzoor,

Hi, I am facing some issues, one of them is adding image to Documentbuilder.
I tried few ways to add the shape, however it’s not getting added in doc. The Image frame is getting added in docx file but not the Image. Could you please suggest the root cause and solution for this?
There is one more issue. Many time I build the table in Aspose and it works fine, but I am trying to build one more table which is not building properly. It just build the table header not the below rows ( won’t even get the exception or error ). I was not able to find the root cause ( you can check the section 6.1- 6.8 in shared undesired docx file for table).
Sharing the tried ways ( code snippet) and the undesired behavior output file.
File.zip (151.5 KB)

@vke3,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please attach the image file that you want to insert into document.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

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.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor,

I have shared you all the files for both table issue and the image issue.
Image issue I solved with some other implementation. Could you please check the table related issue?
Well If I write the simple java method to get the table it will work. I have already created many tables. I have used some other API which is not the java API and hence the code is not compiling at your side as you don’t have the respective jars. Simple java code for table formation will work no doubt. Shared the same details with you. Could you please check what exactly causing problem to add the rows in table (with my code).
File.zip (91.5 KB)
Thanks,