Insert Hyperlink

Jun 16, 2010 at 5:55 PM
Edited Jun 16, 2010 at 6:00 PM
Hi Cathal, I had a need to insert a hyperlink at a specified index in the paragraph instead of just appending it, so I created this function. It may not be perfectly elegant, but it appears to work. Hopefully it will be of some use to you or others. cheers, -Brian I'm not sure how to get this site to format the code correctly, so please excuse the jumbled mess. public Paragraph InsertHyperlink(int index, Hyperlink h) { Run run = GetFirstRunEffectedByInsert(index); if (run == null) { return AppendHyperlink(h); } else { // The parent of this Run XElement parentElement = run.Xml.Parent; // Split this run at the point you want to insert XElement[] splitRun = Run.SplitRun(run, index); XElement link = h.Xml; // Replace the origional run run.Xml.ReplaceWith ( splitRun[0], link, splitRun[1] ); } // Rebuild the run lookup for this paragraph runLookup.Clear(); BuildRunLookup(Xml); DocX.RenumberIDs(Document); return this; }
Coordinator
Jun 16, 2010 at 7:01 PM
Brian,

this function will make an excellent addition to DocX. I will add it
to the source for v1.0.0.10. Thank you for your contribution, my hope
is that more and more people will add to DocX.

Kind regards,
Cathal