Insert Columns to the Left or Right in Word Table with Merged Cells | Java

I have a source document sample doc - source.docx (13.3 KB)

I need to update the table dynamically and I was using Row & Cell objects to build the table with values. Now I need to add the columns dynamically. At most there will be two columns (i.e Old Attributes 2, Old Attributes 3) as shown in the sample doc -final.docxsample doc - final.docx (13.9 KB)

Is there an easy way to automatically add the column to the whole table ?

thank you

@mp2,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-22681. We will further look into the details of this requirement and will keep you updated here on the status of the linked ticket. We apologize for any inconvenience.

is there any work around?

@mp2,

I am afraid, WORDSNET-22681 is currently pending for analysis and is in the queue. For this particular scenario, there isn’t any workaround available at the moment. We will inform you via this thread as soon as this feature (Insert Columns to the Left or Insert Columns to the Right) is supported in future or any workaround may be available. We apologize for any inconvenience.

no problem, i understand.

1 Like

Any update on this? thank you very much

@mp2,

WORDSNET-22681 is currently pending for analysis and is in the queue. We will inform you here as soon as this issue will get resolved or any further updates may be available in future.

@mp2,

Regarding WORDSNET-22681, please check the following points and tell what exactly your requirement is:

  1. Just an example of how one can add a left/right column with a given index. In this case we can suggest the following code:
private void InsertColumnsToTheLeft(Table tbl, int initCellInd)
{
    foreach (Row row in tbl.Rows)
    {
        Cell newCell = new Cell(tbl.Document);
        Paragraph para = new Paragraph(tbl.Document);
        newCell.ChildNodes.Add(para);
        int newCellInd = initCellInd < row.Cells.Count
            ? initCellInd
            : row.Cells.Count - 1;

        row.Cells.Insert(newCellInd, newCell);
    }
}

private void InsertColumnsToTheRight(Table tbl, int initCellInd)
{
    foreach (Row row in tbl.Rows)
    {
        Cell newCell = new Cell(tbl.Document);
        Paragraph para = new Paragraph(tbl.Document);
        newCell.ChildNodes.Add(para);
        int newCellInd = initCellInd < row.Cells.Count
            ? initCellInd + 1
            : row.Cells.Count;

        row.Cells.Insert(newCellInd, newCell);
    }
}
  1. An example of how to thoroughly simulate MS Word’s behavior when calling “Insert Columns to the Left/Right”

  2. Public API of MS Word’s behavior when calling “Insert Columns to the Left/Right”

If you are interested in items 2 and 3, then this will require a significantly longer research and testing of this functionality. This is due to the fact that MS Word’s behavior in this case is not clear, namely, copying of formatting of a cell, paragraph and table style, while there is a dependence on the initially selected cell when calling this command and its position relative to other cells.

can you provide the #1 in Java? Thank you

I think we are looking for #2 but I would like to try #1 first in Java before I confirm

@mp2,

Please check, I have translated above code to Java:

private void insertColumnsToTheLeft(Table tbl, int initCellInd) {
    for (Row row : tbl.getRows()) {
        Cell newCell = new Cell(tbl.getDocument());
        Paragraph para = new Paragraph(tbl.getDocument());
        newCell.getChildNodes().add(para);
        int newCellInd = initCellInd < row.getCells().getCount()
                ? initCellInd
                : row.getCells().getCount() - 1;

        row.getCells().insert(newCellInd, newCell);
    }
}

private void insertColumnsToTheRight(Table tbl, int initCellInd) {
    for (Row row : tbl.getRows()) {
        Cell newCell = new Cell(tbl.getDocument());
        Paragraph para = new Paragraph(tbl.getDocument());
        newCell.getChildNodes().add(para);
        int newCellInd = initCellInd < row.getCells().getCount()
                ? initCellInd + 1
                : row.getCells().getCount();

        row.getCells().insert(newCellInd, newCell);
    }
}

Sure, we will wait for your further input on this topic.

@awais.hafeez - this seems to work. I am wondering how it will perform if we have more rows (like in thousands). Will let you know.
Thank you very much.

@mp2,

It is great that you were able to get the desired results when using this code. We will wait for your any further input on this topic.