Get text in a cell

I have a table in my word document, and in the cell I have 2 lines, with new line character between them.

I created a function to return the text in the cell, but my problem is that it returns the text without the new line character and the text is merged in one line without spacing between the words.

So for example if my text is

"test1
test2"

I am getting "test1test2"

Here is my code:

private string GetCellString(WTableCell cell)
{
string ret = "";
foreach (WParagraph p in cell.Paragraphs)
{
if (p.Text.Length > 0)
ret += p.Text;
}
return ret;
}

Do I have to assume that there is always new line character between paragraphs, and I hard-code that in? or is there a property that returns the character in between the paragraphs?



3 Replies 1 reply marked as answer

SB Sneha Biju Syncfusion Team January 16, 2024 08:34 AM UTC

Hi Michael,

Based on the provided details, we have found that your requirement is to retrieve the cell content with different paragraphs. In the provided code snippet, you added the paragraph text to a string without distinguishing between the paragraphs, resulting in the content being returned without spaces between the words. To resolve this, you can add a new line character or a space before adding the content of the next paragraph. Please refer to the code snippet below.

private string GetCellString(WTableCell cell)

{

    string ret = "";

    foreach (WParagraph p in cell.Paragraphs)

    {

        if (!string.IsNullOrEmpty(ret))

            ret += "\n";

        if (p.Text.Length > 0)

            ret += p.Text;

    }

    return ret;

}


Regards,
Sneha.



MI Michael January 16, 2024 02:34 PM UTC

So there is nothing to tell me if there is a space or new line or some other character (tab, etc.) between the paragraphs?

We can always assume there is a new line character?



AN Anto Nihil Sahaya Raj Syncfusion Team January 17, 2024 07:44 AM UTC

Regarding - So there is nothing to tell me if there is a space or new line or some other character (tab, etc.) between the paragraphs?
Yes. We don’t have any API to tell if there is a space or new line or some other character between the paragraphs.

Regarding - We can always assume there is a new line character?
If two paragraphs are in the Word document, our DocIO library considers that as two different paragraphs. So you can consider the separator character (new line character) to differentiate these paragraphs. Please refer to below modified code snippet below,


Marked as answer
Loader.
Up arrow icon