We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Hyperlinks removal

Hi,

Currently I'm trying to open a template word document using DocIO which contains a bunch of hyperlinks that maps to the database. Now the problem I'm having is actually replacing those hyperlinks with actual data. I've manage to use checks all over my code to get all of the hyperlinks and then remove them after retrieving the values and placing a new WTextRange with the text set to the value.

My question is:
1) Is there an easier way to retrieve all of the hyperlinks that are available in a document?
2) What is the proper way of removing a hyperlink from a text? Do i have to remove the WField type (which contains the FieldType.FieldHyperlink type) and all of the text between that and a WFieldMark with type FieldEnd?

Thanks
Leo


2 Replies

BP Bhuvaneswari P Syncfusion Team November 17, 2008 11:58 AM UTC

Hi Leo,

Thank you for your interest in Syncfusion products.

1) Way to retrieve Hyperlinks?

Please refer the below code to retrieve the hyperlinks in the document:
foreach (Entity ent in document.ChildEntities)
{
if (ent is WSection)
{
WSection section = ent as WSection;
foreach (WParagraph paragraph in section.Body.Paragraphs)
{
for (int i = 0; i < paragraph.Items.Count; i++)
{
if (paragraph.Items[i] is WField && (paragraph.Items[i] as WField).FieldType == FieldType.FieldHyperlink)
{
Hyperlink hlink = new Hyperlink(paragraph.Items[i] as WField);
}
}
}
}
}

2) Proper way to removing a hyperlinks?
Yes, for removing the hyperlink have to remove those mentioned fields since Hyperlink consists of WField, then goes WFieldMark ( field separator ), then WTextRange ( one or several ) and then WFieldmark ( field end)

Please refer the below code snippet to remove the hyperlinks:

foreach (Entity ent in document.ChildEntities)
{
if (ent is WSection)
{
WSection section = ent as WSection;
foreach (WParagraph paragraph in section.Body.Paragraphs)
{
for (int i = 0; i < paragraph.Items.Count; i++)
{
if (paragraph.Items[i] is WField && (paragraph.Items[i] as WField).FieldType == FieldType.FieldHyperlink)
{
while (!(paragraph.Items[i] is WFieldMark &&
(paragraph.Items[i] as WFieldMark).Type == FieldMarkType.FieldEnd))
paragraph.Items.RemoveAt(i);

// Remove field end
paragraph.Items.RemoveAt(i);
break;
}
}
}
}
}


Here is the sample for your reference:
http://websamples.syncfusion.com/samples/Bookmark_77765.zip

Please let me know if this helps you.

Best Regards,
Bhuvana





LC Leonardo Cavan Suryana November 18, 2008 12:11 AM UTC

Thanks that answers my questions.


Loader.
Live Chat Icon For mobile
Up arrow icon