Replace text in table cell but keep existing text format

Hi. I am trying to replace any text in a table cell with a text value. I have two issues.

  1. How can I replace any text in table cell? When I try with regex I get the following error: “The match includes one or more special or break characters and cannot be replaced”.

  2. When I replace text in a table cell, the formating of the original text is lost. How can i preserve that text formating on the new text?

Thanks in advance

Hi Michael,

Thanks
for your inquiry. Unfortunately, currently, you cannot use special character in replacement and captured strings. This is mentioned in the documentation: “An exception is thrown if a captured or replacement string contain one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.

In future we will consider improving Replace method in order to support special characters. Your request has been linked to the appropriate issue (i.e. WORDSNET-1252) in our bug tracking system and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Regarding the issues which you are facing, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please create a standalone/runnable simple application (*for example a Console Application Project) that demonstrates the code (Aspose.Words code) you used to generate your output document
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your target Word document showing the desired behavior. You can * use Microsoft Word to create your target Word document. I will
  • investigate as to how you are expecting your final document be generated * like.

Unfortunately, it is difficult to say what the problem is without the Document(s) and simplified application. We need your Document(s) and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Thank you for you reply. As requested I have attached a zip file containing console app and word documents.

I need to know how to add text to a table cell, replacing the old text (regardless of the text value), but keeping the formating of the cell.

Hi Michael,

Thanks
for sharing the detail. You are facing the expected behavior of Aspose.Words. You are removing all nodes of Cell node and inserting new Run node with default formatting.

Default paragraph formatting is Normal style, left aligned, no indentation, no spacing, no borders and no shading.

Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

In your case, I suggest you please clone the first Run node of table’s cell and set its text as shown in following code example. Hope this helps you. Please let us know if you have any more queries.

Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)bookmark.BookmarkStart.GetAncestor(Aspose.Words.NodeType.Table);
Aspose.Words.Tables.Cell cell = table.FirstRow.Cells[0];
Run run = (Run)cell.FirstParagraph.Runs[0].Clone(true);
run.Text = "My first column";
cell.RemoveAllChildren();
cell.EnsureMinimum();
cell.Paragraphs[0].AppendChild(run);

That did the trick. Thank you so much

Hi Michael,

Thanks
for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi again. We still have some issues. When we format the cell to be rigth aligned in the template document, the alignment is reset when entering the data.
So how can we format a cell with alignment and have that respected when sending data to the document?

Hi Michael,

Thanks
for your inquiry. Please use ParagraphFormat.Alignment property to get or set text alignment for the paragraph. I suggest you please read about specifying formatting from here:
https://docs.aspose.com/words/net/applying-formatting/

Please check the following highlighted code snippet. Hope this helps you. Please let us know if you have any more queries.

MyDocument doc = new MyDocument(MyDir + "Input.dotx");
foreach (Aspose.Words.Bookmark bookmark in doc.Range.Bookmarks)
{
    if (bookmark.Name == "Table")
    {
        Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)bookmark.BookmarkStart.GetAncestor(Aspose.Words.NodeType.Table);
        Aspose.Words.Tables.Cell cell = table.FirstRow.Cells[0];
        Run run = (Run)cell.FirstParagraph.Runs[0].Clone(true);
        run.Text = "My first column";
        cell.RemoveAllChildren();
        cell.EnsureMinimum();
        cell.Paragraphs[0].AppendChild(run);
        cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;
    }
}
doc.Save(MyDir + "Out.docx");

Hi Tahir
Thank you for the answer, but it does not solve our issue.
It must be the Word template that dictates the formatting of the cells. So when a cell has a certain formatting (aligment, font, color), that formatting must be respected and kept when added text to the cells.

Hi Michael,

Thanks
for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please create a standalone/runnable simple application (for example a Console Application Project) that demonstrates the code (Aspose.Words code) you used to generate your output document
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your target Word document showing the desired behavior. You can use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

Unfortunately, it is difficult to say what the problem is without the Document(s) and simplified application. We need your Document(s) and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Thank you for you reply. As requested I have attached a zip file containing console app and word documents.

I need to know how to add text to an empty table cell, but keeping the formatting of the cell.

Hi Michael,

Thanks
for your inquiry. In your case, I suggest you please do not call Cell.RemoveAllChildren method. This method removes the all children nodes of table’s cell along with paragraph formatting. You need to keep the formatted paragraph inside table’s cell.

Please note that if the last child of a cell is not a paragraph, Cell.EnsureMinimum method creates and appends one empty paragraph which contains the default formatting.

Please use one of the following code snippet to achieve your requirements and let us know if you have any more queries.

if (bookmark.Name == "Table")
{
    Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)bookmark.BookmarkStart.GetAncestor(Aspose.Words.NodeType.Table);
    Aspose.Words.Tables.Cell cell = table.LastRow.Cells[0];
    Aspose.Words.Run run = new Aspose.Words.Run(doc);
    run.Text = "Some value";
    cell.Paragraphs[0].AppendChild(run);
}
if (bookmark.Name == "Table")
{
    Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)bookmark.BookmarkStart.GetAncestor(Aspose.Words.NodeType.Table);
    Aspose.Words.Tables.Cell cell = table.LastRow.Cells[0];
    Paragraph para = cell.FirstParagraph;
    para.RemoveAllChildren();
    Aspose.Words.Run run = new Aspose.Words.Run(doc);
    run.Text = "Some value";
    cell.Paragraphs[0].AppendChild(run);
}

Thank you for the answer. The provided code snippet does not do the job. The alignment is correct, but the text color is lost.

Hi Michael,

Thanks
for your inquiry. Please use DocumentBuilder to achieve your requirements as shown in following code example. I suggest you please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/document-builder-overview/
https://docs.aspose.com/words/net/navigation-with-cursor/

Aspose.Words.Document doc = new Aspose.Words.Document(@"c:\temp\Input.dotx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Aspose.Words.Bookmark bookmark in doc.Range.Bookmarks)
{
    if (bookmark.Name == "Table")
    {
        Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)bookmark.BookmarkStart.GetAncestor(Aspose.Words.NodeType.Table);
        Aspose.Words.Tables.Cell cell = table.LastRow.Cells[0];
        builder.MoveTo(cell.FirstParagraph);
        builder.Write("Some value");
    }
}
doc.Save(@"c:\temp\Output.docx");

Hi Tahir. It still does not work. When executing MoveTo I get the error: “The node must be a paragraph or an inline node.”

Hi Michael,

Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v15.2.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v15.2.0 and let us know how it goes on your side. I hope, this will help. I have attached the output document with this post for your kind reference.

Thank you. That did the trick

Hi Michael,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

The issues you have found earlier (filed as WORDSNET-1252) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(31)