I’m currently evaluating if “Aspose.Words” can identify all plain text URLs included in a Word doc and convert them into hyperlinks.
I think the following is a way to do that. But, I wonder if there’s a quicker/easier way to do the conversion.
static void Main(string[] args)
{
Document doc = new Document(FOLDER_PATH + "example.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
// Loop through all paragraphs
foreach (Node paragraph in paragraphs)
{
// Get paragraph
Paragraph par = (Paragraph)paragraph;
// Get all text elements for the paragraph
NodeCollection runs = par.GetChildNodes(NodeType.Run, true);
for (int i = 0; i < runs.Count; i++)
{
Run curElement = (Run)runs[i];
string curText = curElement.GetText();
// If text starts with "http" but not a hyperlink, convert the text URL to a hyperlink
if (!string.IsNullOrEmpty(curText) && curText.Contains("http") && !curText.Contains("HYPERLINK"))
{
Run prevElement = i > 0 ? (Run)runs[i - 1] : null;
if (prevElement == null || !prevElement.GetText().Contains("HYPERLINK"))
{
builder.MoveTo(curElement);
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
builder.InsertHyperlink(curText, curText, false);
curElement.Remove();
}
}
}
}
doc.Save(FOLDER_PATH + "example.rtf");
}