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

Free Support Forum - aspose.com

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");


<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0cm; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>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.