Problem with table creation

Hi,

I’m trying to create a 2 column table with 2 rows.
first column is an image, second column is a text

Result is a table with 2 columns and 2 rows.
The text is correctly added to the second column.
However the image is not.

the first image is on the second row and the second image is beneath the table.

I’ve included my code that I use currently (not 100% the same but some problem).
I think the problem lies in the use of the document builder to add the image to the cell’s Paragraph.

Version of Aspose.Word.Net is 11.8.0.0

Document doc = new Document();
DocumentBuilder _builder = new DocumentBuilder(doc);

Table table = _builder.StartTable();
Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);

// We can now apply any auto fit settings.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.CellFormat.Width = 80;

// Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(new Paragraph(doc)); 
row.AppendChild(cell);
_builder.MoveTo(cell.FirstParagraph);
Shape _pic = _builder.InsertImage(@"C:\tmp\color.gif");

// Create a cell and add it to the row
cell = new Cell(doc);
cell.CellFormat.Width = 80;

cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text"));
row.AppendChild(cell);

// NEW ROW

row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);

cell = new Cell(doc);
cell.CellFormat.Width = 80;

// Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(new Paragraph(doc));
row.AppendChild(cell);
_builder.MoveTo(cell.FirstParagraph);
_pic = _builder.InsertImage(@"C:\tmp\pak.gif");
_pic.Height = 20;
_pic.Width = 20;

// Create a cell and add it to the row
cell = new Cell(doc);
cell.CellFormat.Width = 80;

cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Row 2, Cell 2 Text"));
row.AppendChild(cell);

table.ClearBorders();

doc.Save(@"C:\tmp\test.doc");

Hi Sven,

Thanks for your inquiry. Please use the following modified code to achieve your requirements.

Document doc = new Document();
DocumentBuilder _builder = new DocumentBuilder(doc);
// Table table = _builder.StartTable();
Table table = new Table(doc);
// Add the table to the document.
doc.FirstSection.Body.AppendChild(table);
Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);
// We can now apply any auto fit settings.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.CellFormat.Width = 80;
// Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(new Paragraph(doc));
row.AppendChild(cell);
_builder.MoveTo(cell.FirstParagraph);
Shape _pic = _builder.InsertImage("http://www.aspose.com/images/aspose-logo.gif");
_pic.Height = 20;
_pic.Width = 20;
// Create a cell and add it to the row
cell = new Cell(doc);
cell.CellFormat.Width = 80;
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text"));
row.AppendChild(cell);
// NEW ROW
row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);
cell = new Cell(doc);
cell.CellFormat.Width = 80;
// Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(new Paragraph(doc));
row.AppendChild(cell);
_builder.MoveTo(cell.FirstParagraph);
_pic = _builder.InsertImage("http://www.aspose.com/images/aspose-logo.gif");
_pic.Height = 20;
_pic.Width = 20;
// Create a cell and add it to the row
cell = new Cell(doc);
cell.CellFormat.Width = 80;
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Row 2, Cell 2 Text"));
row.AppendChild(cell);
table.ClearBorders();
doc.Save(MyDir + "Out.doc");

Hope this helps you. Please let us know if you have any more queries.

Hi Tahir,

It isn’t the placement of the table that goes wrong.
Even if I apply your modification, the last image is inserted beneath the table instead of the first column of the second row.

Please try it yourself (just replace path the image file).

regards,
Sven Peeters

Hi Tahir,

I’m sorry but your solution did fix the problem.
However I need to use the document builder to create the table.

Because I have to add the table add exactly the same place as the TextRun containing the tag.
I cannot add a table to the paragraph containing the TextRun, I tried that but he doesn’t allow that.

Is there another method to add the image to the cell (without the use of the documentbuilder)?

Regards,
sven Peeters

Hi,

I found the solution myself

I forgot to do a

table.EndTable()

after

Table table = new Table(doc)

Now it is working properly.

Regards,
Sven Peeters

Hi Sven,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.