facebooktwittermenuarrow-up

GemBox Support Forum

Change existing cell text preserving original format and size

Hi, I’m trying to modify an existing DOCX document.

It has one table (1 row header and 14 empty rows).
The header is Arial size=6 and empty rows are Arial size=7

When I try to load simple text in some cells, for example:

table.Rows[0].Cells[0].Content.LoadText("Testing LoadText");
table.Rows[0].Cells[1].Content.LoadText("Testing LoadText");
table.Rows[1].Cells[0].Content.LoadText("Testing LoadText");
table.Rows[1].Cells[1].Content.LoadText("Testing LoadText");

The text is loaded ok, but the cell text format changes to Arial size=11.

I’d like to preserve the original format of the cells. Is it possible?

Hi Francesc,

I was unable to reproduce this issue, I tried using the latest bugfix version:

Can you please send us your input DOCX file so that we can try reproducing this issue with it?

Regards,
Mario

Hi, I’m using GemBox.Document 3.3 for .NET Framework 3.5 - 4.8 (version 33.0.35.1290) with ContinueAsTrial license.

You can download the “test.docx” file from:

I’ve found that it works if I use

table.Rows[1].Cells[0].Blocks[0].Content.LoadText("Testing LoadText");

instead of

table.Rows[1].Cells[0].Content.LoadText("Testing LoadText");

Hi Francesc,

I was able to reproduce an issue with your document.

The problem occurs because the Paragraph element’s formatting is changed, more specifically the Paragraph.CharacterFormatForParagraphMark.

In short, some information is lost because the previous Cells[0].Blocks[0] element is replaced with a new Paragraph element which has a default formatting.

We could keep or resolve that character formatting information (the font size, font name, etc.), but note that you will still end up losing the paragraph formatting (the 2pt spacing after and before, the single line spacing, etc.).

Because of that, I believe your current approach (using table.Rows[1].Cells[0].Blocks[0].Content.LoadText) is better.
The only thing I would suggest you is to first check that there is one block in the TableCell.

Regards,
Mario

OK, thank you for your quick help.