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 ??
}
}
}
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
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/.
%20(phone).png)
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.
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/.
%20(phone).png)
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,
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/.