Hi Team,
I have attached a screenshot of the issue. I am trying to access a particular cell table and render html text in it…it should actually appear as a textbox enclosing some string value.
However the entire html code is getting displayed. Could you please guide me on what I might be doing wrong here? I can’t really use builder.InsertHtml() because I need to insert it based on a particular cell in a particular row.
var field1 = item as FieldRenderSpecification;
Aspose.Words.Tables.Cell cell = fieldGroupTable.Rows[item.RenderSpecification.Row].Cells[item.RenderSpecification.Column - 1];
cell.FirstParagraph.AppendChild(new Run(cell.Document, field1.Title));
string value = "";
string html = " ";
formValues.TryGetValue(field1.Field, out value);
if (value != null)
{
html = "<div>" +
"<p style=\"border: 1px solid black;min-height:20px; min-width: 150px; margin:5px; padding:5px; \">" + value + "</p>" +
"</div>";
}
Aspose.Words.Paragraph para = new Aspose.Words.Paragraph(fieldGroupTable.Document);
Aspose.Words.Run run = new Aspose.Words.Run(fieldGroupTable.Document, html);
para.AppendChild(run);
cell.AppendChild(para);
Thank you in advance for your help!
Actual.JPG (75.2 KB)
Expected.JPG (5.9 KB)
Hi Alexey,
Based on your suggestion in Unable to render a table correctly I have created a very simple method that can be used in a .Net console application (Sorry i was unable to attach .zip files as i was getting an error here while attaching). Would it be possible for you to check with this method?
public static void AddHtmlToSpecificCell()
{
DocumentBuilder builder = new DocumentBuilder();
// Build a table with 2 rows and 2 columns
var table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell 1");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 3");
builder.EndRow();
builder.EndTable();
string html = "<div>" +
"<p style=\"border: 1px solid black;min-height:20px; min-width: 150px; margin:5px; padding:5px; \">" + "This is some text" + "</p>" +
"</div>";
Aspose.Words.Tables.Cell cell = table.Rows[1].Cells[0];
cell.FirstParagraph.AppendChild(new Run(cell.Document, html));
builder.Document.Save(dataDir + "wordtrial.pdf");
}
@SumithaS You should use DocumentBuilder.InsertHtml
method to insert HTML formated content into the document. For example see the following code:
DocumentBuilder builder = new DocumentBuilder();
// Build a table with 2 rows and 2 columns
var table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell 1");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 3");
builder.EndRow();
builder.EndTable();
string html = "<div>" +
"<p style=\"border: 1px solid black;min-height:20px; min-width: 150px; margin:5px; padding:5px; \">" + "This is some text" + "</p>" +
"</div>";
Aspose.Words.Tables.Cell cell = table.Rows[1].Cells[0];
// Move buider into the cell.
builder.MoveTo(cell.FirstParagraph);
// Insert HTML
builder.InsertHtml(html);
builder.Document.Save(@"C:\Temp\out.pdf");
1 Like
@alexey.noskov I did try this method, but I noticed that in the case I have nested tables, then when the builder moves to this cell, it sorts of messes with the rest of the table’s structure while I am constructing it using a for loop.
Do you think there might be a workaround for this?
@SumithaS Could you please create a simple code example, that can be run on our side and demonstrate the problem.
Also, please try moving the DocumentBuilder
cursor to the last paragraph in the cell. Like this:
builder.MoveTo(cell.LastParagraph);
In case of nested tables, the table cannot be the only child of the cell, there is always a paragraph after the table in the story, so you can be sure the last paragraph is after the nested table.
1 Like
Hi Alexey,
Could you pls check this updated code?:
public static void AddHtmlToSpecificCell()
{
DocumentBuilder builder = new DocumentBuilder();
var table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell 1");
builder.EndRow();
builder.InsertCell();
string html = "<div>" +
"<p style=\"border: 1px solid black;min-height:20px; min-width: 150px; margin:5px; padding:5px; \">" + "This is some text" + "</p>" +
"</div>";
builder.Write("Cell 2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 3");
int count = 0;
for (int i = 0; i < 2; i++)
{
var nestedTable = builder.StartTable();
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 3; k++)
{
builder.InsertCell();
}
builder.EndRow();
}
Aspose.Words.Tables.Cell cellNested = nestedTable.Rows[count].Cells[count + 1];
builder.MoveTo(cellNested.LastParagraph);
builder.InsertHtml(html);
count++;
}
Aspose.Words.Tables.Cell cell = table.Rows[1].Cells[0];
builder.MoveTo(cell.LastParagraph);
// Insert HTML
builder.InsertHtml(html);
builder.EndRow();
builder.EndTable();
builder.Document.Save(dataDir + "wordtrial.pdf");
}
I have attached the output here too:
Output.JPG (39.7 KB)
Here I have included 2 tables in the “Cell 3”. As you can see, the html is being displayed wrong - firstly, “this is some text” is being displayed outside the table cell and also the second table’s html should hve been displayed on row 1 and column 2 based on the “count” but the position is wrong.
Could you please let me know how to fix it? I’m so new to aspose.words tables and aspose in general. Any help would be greatly appreciated.
Thanks in advance.
@SumithaS The problem occurs because you move builder upon building the table. You can work this around using another builder instance for inserting content at the random position. For example see the following code:
DocumentBuilder builder = new DocumentBuilder();
DocumentBuilder htmlBuilder = new DocumentBuilder(builder.Document);
var table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell 1");
builder.EndRow();
builder.InsertCell();
string html = "<div>" +
"<p style=\"border: 1px solid black;min-height:20px; min-width: 150px; margin:5px; padding:5px; \">" + "This is some text" + "</p>" +
"</div>";
builder.Write("Cell 2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 3");
int count = 0;
for (int i = 0; i < 2; i++)
{
var nestedTable = builder.StartTable();
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 3; k++)
{
builder.InsertCell();
}
builder.EndRow();
}
// End the nested table.
builder.EndTable();
// Put HTML into the nested table.
Aspose.Words.Tables.Cell cellNested = nestedTable.Rows[count].Cells[count + 1];
htmlBuilder.MoveTo(cellNested.LastParagraph);
htmlBuilder.InsertHtml(html);
count++;
}
// End the outer table
builder.EndTable();
// Put HTML into the outer table
Aspose.Words.Tables.Cell cell = table.Rows[1].Cells[0];
htmlBuilder.MoveTo(cell.LastParagraph);
// Insert HTML
htmlBuilder.InsertHtml(html);
builder.Document.Save(@"C:\Temp\out.pdf");
1 Like
@alexey.noskov Hi! Thank you so much for your reply!
I tried out your solution, but unfortunately it is displaying the text in the wrong row, somehow the builder added additional 2 rows. The column count is correct though. Also the html is now displayed well!
I have attached the output that i got…
I was expecting the nestedtable to have just 2 rows and 3 columns. however there are 4 rows in total. I have rechecked my for loop and it seems to be correct. It didn’t add any new rows. Do you know why this may be happening? :
Output.JPG (33.4 KB)
Hi @alexey.noskov,
Thank you so much for all your help. I was able to tweak my code a little and made it work.
Best regards,
Sumitha
@SumithaS In the code tables are creates one after another. In this case, the table are concatenated. To have separate tables, it is required to insert a paragraph break between them. For example see the following modified code:
for (int i = 0; i < 2; i++)
{
var nestedTable = builder.StartTable();
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 3; k++)
{
builder.InsertCell();
}
builder.EndRow();
}
// End the nested table.
builder.EndTable();
// Insert a paragraph between the tables
// If the tables are one after another they are concatenated.
builder.Writeln();
// Put HTML into the nested table.
Aspose.Words.Tables.Cell cellNested = nestedTable.Rows[0].Cells[1];
htmlBuilder.MoveTo(cellNested.LastParagraph);
htmlBuilder.InsertHtml(html);
}