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

Free Support Forum - aspose.com

Text formatting is lost after editing text in table cell using .NET

I’m using the .NET API to edit table cells and add new rows. The base document has a single row with empty cells in. If I use Word and type text into the columns it is formatted correctly. If I use the Aspose API then the text seems to use some kind of default formatting.

I get the first row and iterate over the cells and do the following to set the text:
cell.FirstParagraph.AppendChild(new Run(document, text));
Note that there are no existing Runs in the Paragraphs so the formatting cannot be on them.

For further rows I clone the Row, append it to the table and iterate over the cells doing the same as above.

Note that in the original document the table has cell borders but after the cells are modified those also seems to be have gone - the table still has it’s borders, it’s just the divisions between the rows and columns that go.

What am I doing wrong?


You are adding new Run node into Paragraph node. So, you are getting default font formatting. We suggest you please clone the first row and add it into table.

In this case, we suggest you following solution.

  1. Please get the first Run of Paragraph.
  2. Set its text with empty string.
  3. Clone the Run node and set its text according to your requirement.
  4. Remove all run nodes of Paragraph.
  5. Add the cloned Run node to Paragraph.

Hope this helps you. If you still face problem, please ZIP and attach your input and expected output documents. We will then answer your query according to your requirement.

There is no existing Run to clone as far as I can see.

Here is an example document: AsposeTableFormatProblem.zip (21.8 KB)

You should see a difference if you add text manually through Word vs using Aspose to edit the empty cells of the table.

Note that this would be a whole lot easier if we could use mail merge or change the word document but I’m trying to replicate some legacy behaviour so I can’t do that.


Please move the cursor to the table’s cell and write some text as shown below to mimic the behavior of MS Word. The default font name for Run node is ‘Times New Roman’.

Document doc = new Document(MyDir + "AsposeTableFormatProblem.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = doc.FirstSection.Body.Tables[0];

Run run = new Run(doc);
doc.Save(MyDir + "20.5.docx");

Could you please share some more detail about your requirement along with input and expected output documents? We will then provide you more information on it.


Ok that worked for the most part - at least the formatting is correct - I hadn’t realised you could use DocumentBuilder to edit as all the examples I found were for creating new tables.

A couple things though:
Is there a way to delete/overwrite existing text in the cell using DocumentBuilder?

Also curious about why this works. What is DocumentBuilder.Write doing to add text to a paragraph that is different than when you try to add text to a paragraph without using DocumentBuilder. As I said previously, there is no existing Run node so the formatting is not coming from there. If it’s coming from the Paragraph or Table or somewhere else, is there a way to construct a Run node that correctly inherits formatting from it’s parent(s)?

Actually I just realised that I could the previous solution for clearing the cells and it doesn’t have any effect on formatting (which makes sense given no runs existed at the start anyway). Still interested in the second part of my question about what the DocumentBuilder.Write function is doing different when it creates a Run.


With DocumentBuilder, you can remove current node and paragraph. However, in your case, we suggest you please remove the text using Run.Remove method or Range.Replace method to replace the text with empty string.

Please note that DocumentBuilder writes the text with font formatting of current node (DocumentBuilder.CurrentNode). Move the cursor to the desired node and write the test to check this behavior. Please read the following article.
Document Builder Overview
Navigation with Cursor

However, if you create Run and Paragraph node and insert it into document, the default font name is used i.e. ‘Times New Roman’.

Please use DocumentBuilder to write the text into document.

Please move the cursor to the any node of document and write some text. The font formatting will be according to current node where the cursor exits. Please check the properties DocumentBuilder.CurrentParagraph and DocumentBuilder.CurrentNode.

CurrentNode is a cursor of DocumentBuilder and points to a Node that is a direct child of a Paragraph. Any insert operations you perform using DocumentBuilder will insert before the CurrentNode.

Hope this answers your query. If you still face problem while using Aspose.Words, please ZIP and attach your simplified input and expected output documents. We will then provide you more information about your query.