Table issue

How to insert table within another table using aspose.words API ? I tried this directly by inserting a cell and then ending the table without ending the outer table but it is giving me exception as “Cannot end a table while not building a table”. Please suggest some way to achieve this.

Hi
Thanks for your inquiry. I think that you can use the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Strat main table
builder.StartTable();
// Insert cell
builder.InsertCell();
// Start nested table
builder.StartTable();
// Generate some table
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 10; j++)
    {
        builder.InsertCell();
        builder.Write("nested table");
    }
    builder.EndRow();
}
// end nested table
builder.EndTable();
// insert one more cell into the main table
builder.InsertCell();
builder.Write("Main table");
// End row
builder.EndRow();
// end main table
builder.EndTable();
// Save document
doc.Save(@"Test280\out.doc");

Hope this helps.
Best regards.

Hi,
Thanks for quick reply.
It solved my problem to some extent.
If I want to apply different formattings for inner and outer tables then what should I do ? Currently
when I start inner table I set new formattings and after that I revert back these for outer one.
But using this my alignment of data is not coming properly. Also I don’t find it proper approach to
achieve this. Can you suggest some better way ?
For your reference I am attaching my output file and code snippet.

public void buildTable(DocumentBuilder builder) throws Exception
{
    // Reset to default font and paragraph formatting.
    builder.getFont().clearFormatting();
    builder.getParagraphFormat().clearFormatting();
    builder.insertParagraph();
    builder.writeln("This shows how to build a table in a document.");
    builder.getFont().setSize(12);
    builder.getFont().setName("Arial");
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    builder.getRowFormat().setHeight(4 * 72);
    builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
    builder.getCellFormat().setWidth(8 * 72);
    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
    Borders borders = builder.getCellFormat().getBorders();
    borders.setLineStyle(LineStyle.SINGLE);
    borders.setLineWidth(2);
    borders.setColor(Color.BLUE);
    builder.startTable();
    for (int y = 0; y < 8; y++)
    {
        builder.insertCell();
        builder.insertHtml("**Earthquake**");

        builder.getRowFormat().setHeight(0.5 * 72);
        builder.getCellFormat().setWidth(2 * 72);

        builder.startTable();
        for (int x = 0; x < 5; x++)
        {
            builder.insertCell();
            builder.write("Earthquake Pacific North West");
            builder.endRow();
        }

        builder.endTable();
        builder.getRowFormat().setHeight(4 * 72);
        builder.getCellFormat().setWidth(8 * 72);

        builder.endRow();
    }
    builder.endTable();
}

Hi
Thanks for your request. Could you also create document that will show me how the output document should looks like?
Best regards.

Please find attached sample document.

Hi
Thank you for more information. I think that you can specify RowAlignment. See the following code snippet.

builder.insertCell();
builder.insertHtml("<b>Earthquake</b>");
builder.getRowFormat().setHeight(0.5 * 72);
builder.getCellFormat().setWidth(2 * 72);
builder.startTable();
builder.getRowFormat().setAlignment(RowAlignment.CENTER);
for (int x = 0; x < 5; x++)
{
    builder.insertCell();
    builder.write("Earthquake Pacific North West");
    builder.endRow();
}
builder.endTable();
builder.getRowFormat().setHeight(4 * 72);
builder.getCellFormat().setWidth(8 * 72);
builder.endRow();

Hope this helps.
Best regards.

Thanks.
Now its working fine.
One more thing can we have bullet along with text in document ?
If yes how can we insert bullets in a document ?

Hi
Thanks for your inquiry. Yes of course you can insert bullets. You should use applyBulletDefault() method. Please see the following code snippet.

builder.startTable();
builder.getRowFormat().setAlignment(RowAlignment.CENTER);
for (int x = 0; x < 5; x++)
{
    builder.insertCell();
    builder.getListFormat().applyBulletDefault();
    builder.write("Earthquake Pacific North West");
    builder.endRow();
}
builder.endTable();

Also see the following link for more information.
https://reference.aspose.com/words/net/aspose.words.lists/listformat/
Best regards.

Hi,
I have one more query. Just like we can insert rows in a table can we insert columns ?
For example suppose we have a table with three rows each of them has one column. Later I want
to add one column to this table. So is there some way to perform this ?

Hi
Thanks for your inquiry. Actually there is no “column” concept in the Word table. By design Microsoft Word design rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width.
So if you need to add column into MS Word table you should add cell to each row in this table. For example see the following code:

// Open docuemnt
Document doc = new Document("in.doc");
// Get first table from document
Table tab = doc.getFirstSection().getBody().getTables().get(0);
// Create new cell
Cell newCell = new Cell(doc);
Paragraph par = new Paragraph(doc);
Run text = new Run(doc, "this is new column");
par.appendChild(text);
newCell.appendChild(par);
newCell.getCellFormat().setWidth(50);
// Add cell to each table row
for (int rowIndex = 0; rowIndex < tab.getRows().getCount(); rowIndex++)
{
    // Clone created cell
    Cell clon = (Cell)newCell.deepClone(true);
    tab.getRows().get(rowIndex).appendChild(clon);
}
// Save output docuemnt
doc.save("out.doc");

Hope this helps.
Best regards.

Thanks for your help.
I caught up in a new issue which is like if my table(I am generating it at run time) is spanning across multiple pages then table structure gets weird. For example two rows get merged in a single row.
Can u suggest how can I overcome this strnge behavior ?

Hi
Thanks for your inquiry. Could you please attach your document and provide me your code? I will investigate this issue and provide you more information.
Best regards.

hi,
Sorry I was mistaken. Actually table got divided into seperate sections on different pages.
but when I joined them by inserting linefeed table appeared perfectly.
I have one doubt. I have one table in my template. In that table heading row is there. In between I am keeping a blank row(with no text). Now at run time I create clone of this blank row and I do insert some text into each of the cell. But when document gets generated the font size of inserted rows are different from original blank row. Can you suggest anything why this is happening ?

Hi
Thanks for your inquiry. Please try using code from the following thread:
https://forum.aspose.com/t/105376
I think this could help you.
Best regards.

Thanks a lot.
Its worked perfectly.