DocumentBuilder does not format the text bold and underline using C#

I have been applying the lastest builds to my development code base as they come out. It appears the following code in my tables is no longer working. Is this a break-change? I am writing this for a faster response than reviewing all the past release notes.

Here is the code that used to bold the text and underline in a table cell. newRow is a clone of a row int the current table. "builder" is a DocumentBuilder of the current Document. The code now produces a table with the cell text not bold and not underlined.

    	   	    builder.Bold = true;
                    builder.Underline = Underline.Single;
                    oCell = newRow.Cells[0];
                    PrepCellForWrite(ref oCell);
                    builder.MoveTo(oCell.FirstParagraph);
                    builder.Write("Bold Me and Underline me");
 
private void PrepCellForWrite(ref Aspose.Words.Tables.Cell newCell)
        {
            if (newCell.Paragraphs != null)
            {
                while (newCell.Paragraphs.Count > 1)
                {
                    newCell.LastParagraph.Remove();
                }
                // Remove content of first paragraph
                if (newCell.FirstParagraph.HasChildNodes)
                {
                    newCell.FirstParagraph.ChildNodes.Clear();
                }
                // Move DocumentBuilder cursor to the cell
            }
        }
private void PrepCellForWrite(ref Aspose.Words.Tables.Cell newCell)
        {
            if (newCell.Paragraphs != null)
            {
                while (newCell.Paragraphs.Count > 1)
                {
                    newCell.LastParagraph.Remove();
                }
                // Remove content of first paragraph
                if (newCell.FirstParagraph.HasChildNodes)
                {
                    newCell.FirstParagraph.ChildNodes.Clear();
                }
                // Move DocumentBuilder cursor to the cell
            }
        }
 
Thanks for any help or new code to replace the old "Bold" and "Underline" properties of the DocumentBuilder.
 
Bryan

Hi Yuri,

Thanks for your query. Please use the following code snippet to bold/underline the cell’s text of a table. Hope this helps you. Please let me know, If you have any more queries.

Cell cell = table.Rows[0].Cells[0];

// Get Runs in this cell.

NodeCollection runs = cell.GetChildNodes(NodeType.Run, true);

// Loop through all runs and make them bold.

foreach (Run run in runs)

{

run.Font.Bold = true;

run.Font.Underline = Underline.Single;

}

In which build did this functionality change? I would like to go back to that build instead of changing hundreds of lines of code to looping through "RUNS".

In the meantime I will let you know if the code works.

Bryan

Hi Yuri,

Please use the following code snippet to bold/underline the cell’s text of a table. Hope this answers your query.

PrepCellForWrite(ref oCell);

builder.MoveTo(oCell.FirstParagraph);

//builder.Bold = true;

//builder.Underline = Underline.Single;

builder.Font.Bold = true;

builder.Font.Underline = Underline.Single;

builder.Write("Bold Me and Underline me");

That still behaved the same as the old code. No change in the table’s bold or underline of the cell’s text. Still trying your first way don’t know the result yet.

Hi Yuri,

Please use the latest version of Aspose.Words for .NET. I have shared the complete code snippet, please see below. I have also shared the input and output document with this post.

Hope this answers your query. Please let us know, If you have any more queries.

Document doc = new Document(MyDir + "in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

Node[] table = doc.GetChildNodes(NodeType.Table, true).ToArray();

Cell oCell = ((Table)table[0]).Rows[0].Cells[0];

PrepCellForWrite(ref oCell);

builder.MoveTo(oCell.FirstParagraph);

builder.Font.Bold = true;

builder.Font.Underline = Underline.Single;

builder.Write("Bold Me and Underline me");

doc.Save(MyDir + "AsposeOut.doc");

So if your code works, then it is the styles of the base document that I am using. I will try one from scratch and see how it goes. I am ready using the latest version released unless you have a hotfix out there.

Hi Yuri,

Please try the above code and let us know, If you have any more queries.

builder.Font.Bold = true;

did not work. I have attached my table template for you to try and add a row with bold and underline text.

Hi Yuri,

I have worked with shared document and have successfully added the row at the end of table. Please use the following code snippet. I have attached the output document with this post.

Document doc = new Document(MyDir + "TableClosedEndPartner.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

Node[] table = doc.GetChildNodes(NodeType.Table, true).ToArray();

Table awTable = ((Table)table[0]);

//Clone last row

Row newRow = (Row)awTable.LastRow.Clone(true);

//Add cloned row to table

awTable.Rows.Add(newRow);

Cell oCell = awTable.LastRow.Cells[0];

PrepCellForWrite(ref oCell);

builder.MoveTo(oCell.FirstParagraph);

builder.Font.Bold = true;

builder.Font.Underline = Underline.Single;

builder.Write("Bold Me and Underline me");

doc.Save(MyDir + "AsposeOut.docx");