This is a migrated thread and some comments may be shown as answers.

Get Table Cell Value

5 Answers 664 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Francois
Top achievements
Rank 1
Veteran
Francois asked on 11 Jan 2021, 05:04 AM

How do I get (or "read") a table cell's text value?

For example:

                            var row = table.Rows[i];
                            int nbCols = row.Cells.Count();
                            if (!(nbCols < 1))
                            {
                                for (int j = 1; j < nbCols; j++)
                                {
                                    Run run = new Run(row.Cells[j].Blocks[0].Document);
                                    if (run.Text == "«Signature»")
                                    {
                                        RadFlowDocumentEditor colEditor = new RadFlowDocumentEditor(row.Cells[j].Blocks[0].Document);
                                        // if it's the Signature cell then replace the content of the cell by the Image ...
                                                      // How do I do this ??
                                        // see https://www.telerik.com/forums/table-cell-width
                                    }
                                }
                            }

 

In the code above I am trying to iterate through the collection of columns to find the one that contains text «Signature» so that I can replace that text with an image. The beast I could do is use the run as shown above but it never finds the value (although the value is in the table).

Thanks

 

 

 

5 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 11 Jan 2021, 12:59 PM

Hi Francois,

You can achieve the desired functionality in the following manner:

List<Run> runs = table.EnumerateChildrenOfType<Run>().ToList();
foreach (Run run in runs)
{
	if (run.Text == "«Signature»")
	{
		Paragraph paragraph = run.Paragraph;
		int childIndex = paragraph.Inlines.IndexOf(run);

		ImageInline image = new ImageInline(document);
		using (Stream stream = File.OpenRead("image1.jpeg"))
		{
			image.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(stream, "jpeg");
		}

		paragraph.Inlines.Insert(childIndex, image);
		paragraph.Inlines.Remove(run);
	}
}

However, we have an item logged in our backlog to provide an easier way to replace text with other document elements (such as images): WordsProcessing: Introduce a way to replace text with other document elements. You can cast your vote for the implementation as well as subscribe to the task by clicking the Follow button so you can receive updates about status changes.

As a side note, in the provided code snippet I am using the EnumerateChildrenOfType<T>() method that allows me to easier iterate through document elements. 

I hope this helps. Please, let me know if there is anything else I can assist you with.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Francois
Top achievements
Rank 1
Veteran
answered on 11 Jan 2021, 08:36 PM

Hi Martin,

Thank you a lot; that worked! Two things though: all my keywords are enclosed in 'french double quotes' as shown in my example code. When I use something like:

rowEditor.ReplaceText("«lQty»", sol.QtySold.ToString());

.. all works fine; but when using:

if (run.Text == "«Signature»")

... the keyword is broken as 3 run.Text objects: the opening dquote, the word SIgnature and the closing dquote. I noticed that not all keywords are broken up like that, for example sometime the opening dquote is alone and then the keyword and the closing dquote are bundled as a single run.text -- so why is that? how can I get the keywords (with the quotes) consistently ?

Second: The 'run' objects & methods are difficult to grasp. I read all the doc and the forums but still not much can be found. Can you point me to articles or other resources to better understand and use run?

Thank you very much.

 

 

0
Martin
Telerik team
answered on 13 Jan 2021, 07:32 AM

Hi Francois,

Thank you for the additional feedback provided.

The separation of the runs might happen due to many different reasons and is indeed something not obvious to the end-user. The way we find that is through opening the XML containing the definition of the document elements and examining it. Until the feature request (Introduce a way to replace text with other document elements) is developed, a possible approach could be to concatenate all the runs text in a single paragraph and search the pattern within the concatenated string, and if it matches to edit all the runs forming the pattern.

As for the information about the Run element you can check the Run help article. If you think this article is not comprehensive enough, please, give us some guidelines (according to you) on how we can improve it.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Francois
Top achievements
Rank 1
Veteran
answered on 20 Jan 2021, 03:29 AM

Martin,

Thank you for your help.

I will post here the method I used to replace CarriageReturn/LineFeed in a text field. My aim is to help someone out there with the same requirement. Also: if there is a better way to do this I appreciate any feedback.

void PrintDescription()
{
    string soDescription = so.Description.Trim();
    if (string.IsNullOrEmpty(soDescription)) return;
    try
    {
        string crlf = Environment.NewLine;
        soDescription += crlf;
        var table = (Table)SOWordDocument.Sections[0].Blocks[2];
        if (table is null) return;
        List<Run> runs = table.EnumerateChildrenOfType<Run>().ToList();
        foreach (Run run in runs)
        {
            if (run.Text == "«Description»")
            {
                Paragraph paragraph = run.Paragraph;
                while (true)
                {
                    int i = soDescription.IndexOf(crlf); if (i <= 0) break;
                    string text = soDescription.Substring(0, i); soDescription = soDescription.Substring(i + crlf.Length);
                    paragraph.Inlines.AddRun(text);
                    paragraph.Inlines.Add(new Break(SOWordDocument));
                    i = soDescription.IndexOf(crlf);
                }
                paragraph.Inlines.Remove(run);
            }
        }
    }
    catch { };
}

 

As you mentioned, I will certainly have a look at https://feedback.telerik.com/document-processing/1356259-wordsprocessing-introduce-a-way-to-replace-text-with-other-document-elements and try to improve the Run help article as I understand it better.

Again: thanks for your help - my app is working as expected now.

Best regards,

0
Martin
Telerik team
answered on 22 Jan 2021, 11:02 AM

Hi Francois,

I am happy you have a working solution now and thank you for the provided code snippet.

Please, continue following the feature request in order to be sure you will be notified when the item is developed. Meanwhile, do not hesitate to contact us if any additional questions arise.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
WordsProcessing
Asked by
Francois
Top achievements
Rank 1
Veteran
Answers by
Martin
Telerik team
Francois
Top achievements
Rank 1
Veteran
Share this question
or