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

Free Support Forum - aspose.com

Copy table rows and cells without losing text formatting using Java

Hi

I am trying to create a word document with table that has styles. This will run into multiple pages.
I have attached java class and also the target sample document.proposed.zip (11.6 KB)

I am using aspose java 19.12 jdk17.

Appreciate if you can help

@mahesh.pinnamaneni.fda

In your code, you are creating the table and setting its style correctly. We suggest you please read the following article.
Working with Table Styles

For the merged cells, you need to apply the cell formatting. Please read the following article for this case.
Applying Borders and Shading

I am trying to create a final document from the template…template.zip (22.1 KB)

Am i missing anything here?

@mp2

Please clone the table rows and insert the content as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "template.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2016);

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
int rowcount  = table.getRows().getCount();

RowCollection rows = table.getRows();

System.out.println("Total Rows in Word= " + rowcount);

/*
 * for (int i = 0; i < rowcount; i++){ CellCollection cells =
 * rows.get(i).getCells(); for (int j = 0; j < cells.getCount(); j++)
 * System.out.println( "Row: " + i + ", Cell: " + j + "  - " +
 * cells.get(j).getText()); }
 */

builder.moveToCell(0, 1, 1, 0);
builder.write("Aug 01, 2019");

builder.moveToCell(0, 2, 1, 0);
builder.write("Aug 15, 2019");

builder.moveToCell(0, 3, 1, 0);
builder.write("Logitech Technologies");

builder.moveToCell(0, 4, 1, 0);
builder.write("ENDS");

// new product specific attributes row - this is a merged cell with grey background
// we need this so we can add multiple products using this clone after we add multiple attributes rows

Row productRowHead = (Row) rows.get(5).deepClone(true);
Row productRowData = (Row) rows.get(6).deepClone(true); //clone the last empty row so we can add new text

for(Cell cell : productRowHead.getCells())
{
    if(cell.getFirstParagraph().getRuns().getCount() > 0)
    {
        Run run = (Run)cell.getFirstParagraph().getRuns().get(0).deepClone(true);
        run.setText("");
        cell.removeAllChildren();
        cell.ensureMinimum();
        cell.getFirstParagraph().getRuns().add(run);
    }
}

table.appendChild(productRowHead);
builder.moveToCell(0, 7, 0, 0);
builder.write("New inserted Product Name");

table.appendChild(productRowData);

builder.moveToCell(0, 8, 0, 0);
builder.write("Submission Tracking Number");
builder.moveToCell(0, 8, 1, 0);
builder.write("STN 1222");

table.appendChild((Row)rows.get(6).deepClone(true));

builder.moveToCell(0, 9, 0, 0);
builder.write("Submission Tracking Number");
builder.moveToCell(0, 9, 1, 0);
builder.write("STN 1222");

table.appendChild((Row)rows.get(6).deepClone(true));

builder.moveToCell(0, 10, 0, 0);
builder.write("Submission Tracking Number");
builder.moveToCell(0, 10, 1, 0);
builder.write("STN 1222");

doc.save(MyDir + "merged table.docx");

Yep, works like a charm. thank you Tahir.