Remove Hyperlinks from a Text with the RadFlowDocument

1 Answer 37 Views
General Discussions
Wolfgang
Top achievements
Rank 1
Wolfgang asked on 08 Dec 2023, 02:47 PM

I have an HTML code with hyperlinks in the text, this must be converted to rtf and the hyperlinks in the text need to be removed, but I did not find a way to remove the hyperlink/uri.

Is there an example for that?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Dec 2023, 02:37 PM | edited on 13 Dec 2023, 02:46 PM

Hi, Wolfgang,

The hyperlinks are stored with the help of FieldCharacters in the RadFlowDocument. More information about the internal structure of the hyperlink fields is available in the online documentation: Hyperlink Field.

I have prepared a sample code snippet demonstrating how to load a sample HTML documents with hyperlinks and remove them before exporting to RTF. Since the DeleteContent method removes the hyperlink field elements and leave only the text run that store the text itself. Afterwards we remove the custom Hyperlink style that is applied to the text, to make it look like the rest of the content:

            using (Stream input = File.OpenRead(@"..\..\sample.html"))
            {
                Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
                document = provider.Import(input);
                RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
                var hyperlinkElements = document.EnumerateChildrenOfType<FieldCharacter>().Where(x => x.FieldCharacterType == FieldCharacterType.Start).ToList();
                foreach (FieldCharacter hyperlink in hyperlinkElements)
                {
                    editor.DeleteContent(hyperlink.FieldInfo.End, hyperlink.FieldInfo.End);
                    editor.DeleteContent(hyperlink.FieldInfo.Start, hyperlink.FieldInfo.Separator);
                }
                var hyperlinkRuns = document.EnumerateChildrenOfType<Run>().Where(x => x.StyleId.Contains("Hyperlink")).ToList();
                foreach (Run r in hyperlinkRuns)
                {
                    r.StyleId = "Normal";
                }
                var output = provider.Export(document);
            }

Then, you can export the RadFlowDocument to RTF by using the RtfFormatProvider.

Please give it a try and see how it works on your end. If you need any further assistance please let me know.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
General Discussions
Asked by
Wolfgang
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or