We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace table cell hyperlink with text

I've created an Aspose table by inserting an html table. The resultant Aspose table has hyperlinks which i want to replace/format with normal text.

How do i go about navigating to a table cell and replacing the text and change the font?

Hi Mark,


Thanks for your inquiry. Please follow up the code snippet to break the hyperlinks.

Document doc = new Document(@“Test202\in.doc”);<o:p></o:p>

BreakHyperlinks(doc);<o:p></o:p>

doc.Save(@“Test202\out.doc”);<o:p></o:p>

<o:p> </o:p>

///

<o:p></o:p>

/// Method breaks hyperlink in the document.<o:p></o:p>

/// Hyperlinks in the word document is HYPERLINK fields.<o:p></o:p>

/// Fields looks like the following [FieldStart]field code[FieldSeparator]field value[FieldEnd]<o:p></o:p>

/// <o:p></o:p>

/// Input document<o:p></o:p>

private void BreakHyperlinks(Document doc)<o:p></o:p>

{<o:p></o:p>

//Get collection of FieldStart nodes in the document<o:p></o:p>

NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);<o:p></o:p>

<o:p> </o:p>

//Create temporary arraylist where we will store FieldStart nodes of Hyperlinks<o:p></o:p>

//After processing all these nodes will be removed<o:p></o:p>

ArrayList hyperlinkStarts = new ArrayList();<o:p></o:p>

<o:p> </o:p>

//Loop through all FieldStart Nodes and search for Hyperlink field<o:p></o:p>

foreach (FieldStart start in starts)<o:p></o:p>

{<o:p></o:p>

if (start.FieldType == FieldType.FieldHyperlink)<o:p></o:p>

{<o:p></o:p>

hyperlinkStarts.Add(start);<o:p></o:p>

<o:p> </o:p>

Node currentNode = start.NextSibling;<o:p></o:p>

Node fieldSeparator = null;<o:p></o:p>

//Remove all nodes between FieldStart and FieldSeparator nodes<o:p></o:p>

//Field code will be removed<o:p></o:p>

while (currentNode.NodeType != NodeType.FieldSeparator)<o:p></o:p>

{<o:p></o:p>

currentNode = currentNode.NextSibling;<o:p></o:p>

currentNode.PreviousSibling.Remove();<o:p></o:p>

}<o:p></o:p>

//Look for Field end node<o:p></o:p>

fieldSeparator = currentNode;<o:p></o:p>

while (currentNode.NodeType != NodeType.FieldEnd)<o:p></o:p>

{<o:p></o:p>

if (currentNode.NodeType.Equals(NodeType.Run))<o:p></o:p>

{<o:p></o:p>

(currentNode as Run).Font.Color = Color.Black;<o:p></o:p>

(currentNode as Run).Font.Underline = Underline.None;<o:p></o:p>

}<o:p></o:p>

currentNode = currentNode.NextSibling;<o:p></o:p>

}<o:p></o:p>

//Remove FieldSeparator and FieldEnd nodes<o:p></o:p>

fieldSeparator.Remove();<o:p></o:p>

currentNode.Remove();<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

//Now we can remove FieldStart nodes of Hyperlink fields<o:p></o:p>

foreach (FieldStart start in hyperlinkStarts)<o:p></o:p>

{<o:p></o:p>

start.Remove();<o:p></o:p>

}<o:p></o:p>

}


Hope this will help.


Thanks. One last question, how do i go about editing the text in the table cell?

Hi Mark,


Thanks for your inquiry. Please follow up the code to edit cell text.

Document doc = new Document(“c:/temp/Print35.docx”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToCell(0, 0, 0, 0);

Regex regex = new Regex(@"[^\r\n]*", RegexOptions.IgnoreCase);

builder.CurrentNode.Range.Replace(regex, builder.CurrentNode.GetText()+"Modify");

doc.Save("c:/temp/PrintOut.docx");


Hope this will help.