You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hyperlink.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.IO.Packaging;
  7. namespace Novacode
  8. {
  9. /// <summary>
  10. /// Represents a Hyperlink in a document.
  11. /// </summary>
  12. public class Hyperlink: DocXElement
  13. {
  14. internal Uri uri;
  15. internal String text;
  16. internal Dictionary<PackagePart, PackageRelationship> hyperlink_rels;
  17. internal int type;
  18. internal String id;
  19. internal XElement instrText;
  20. internal List<XElement> runs;
  21. /// <summary>
  22. /// Remove a Hyperlink from this Paragraph only.
  23. /// </summary>
  24. /// <example>
  25. /// <code>
  26. /// // Create a document.
  27. /// using (DocX document = DocX.Create(@"Test.docx"))
  28. /// {
  29. /// // Add a hyperlink to this document.
  30. /// Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  31. ///
  32. /// // Add a Paragraph to this document and insert the hyperlink
  33. /// Paragraph p1 = document.InsertParagraph();
  34. /// p1.Append("This is a cool ").AppendHyperlink(h).Append(" .");
  35. ///
  36. /// /*
  37. /// * Remove the hyperlink from this Paragraph only.
  38. /// * Note a reference to the hyperlink will still exist in the document and it can thus be reused.
  39. /// */
  40. /// p1.Hyperlinks[0].Remove();
  41. ///
  42. /// // Add a new Paragraph to this document and reuse the hyperlink h.
  43. /// Paragraph p2 = document.InsertParagraph();
  44. /// p2.Append("This is the same cool ").AppendHyperlink(h).Append(" .");
  45. ///
  46. /// document.Save();
  47. /// }// Release this document from memory.
  48. /// </code>
  49. /// </example>
  50. public void Remove()
  51. {
  52. Xml.Remove();
  53. }
  54. /// <summary>
  55. /// Change the Text of a Hyperlink.
  56. /// </summary>
  57. /// <example>
  58. /// Change the Text of a Hyperlink.
  59. /// <code>
  60. /// // Create a document.
  61. /// using (DocX document = DocX.Load(@"Test.docx"))
  62. /// {
  63. /// // Get all of the hyperlinks in this document
  64. /// List&lt;Hyperlink&gt; hyperlinks = document.Hyperlinks;
  65. ///
  66. /// // Change the first hyperlinks text and Uri
  67. /// Hyperlink h0 = hyperlinks[0];
  68. /// h0.Text = "DocX";
  69. /// h0.Uri = new Uri("http://docx.codeplex.com");
  70. ///
  71. /// // Save this document.
  72. /// document.Save();
  73. /// }
  74. /// </code>
  75. /// </example>
  76. public string Text
  77. {
  78. get
  79. {
  80. return this.text;
  81. }
  82. set
  83. {
  84. XElement rPr =
  85. new XElement
  86. (
  87. DocX.w + "rPr",
  88. new XElement
  89. (
  90. DocX.w + "rStyle",
  91. new XAttribute(DocX.w + "val", "Hyperlink")
  92. )
  93. );
  94. // Format and add the new text.
  95. List<XElement> newRuns = HelperFunctions.FormatInput(value, rPr);
  96. if (type == 0)
  97. {
  98. // Get all the runs in this Text.
  99. var runs = from r in Xml.Elements()
  100. where r.Name.LocalName == "r"
  101. select r;
  102. // Remove each run.
  103. for (int i = 0; i < runs.Count(); i++)
  104. runs.Remove();
  105. Xml.Add(newRuns);
  106. }
  107. else
  108. {
  109. XElement separate = XElement.Parse(@"
  110. <w:r xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  111. <w:fldChar w:fldCharType='separate'/>
  112. </w:r>");
  113. XElement end = XElement.Parse(@"
  114. <w:r xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  115. <w:fldChar w:fldCharType='end' />
  116. </w:r>");
  117. runs.Last().AddAfterSelf(separate, newRuns, end);
  118. runs.ForEach(r => r.Remove());
  119. }
  120. this.text = value;
  121. }
  122. }
  123. /// <summary>
  124. /// Change the Uri of a Hyperlink.
  125. /// </summary>
  126. /// <example>
  127. /// Change the Uri of a Hyperlink.
  128. /// <code>
  129. /// <![CDATA[
  130. /// // Create a document.
  131. /// using (DocX document = DocX.Load(@"Test.docx"))
  132. /// {
  133. /// // Get all of the hyperlinks in this document
  134. /// List<Hyperlink> hyperlinks = document.Hyperlinks;
  135. ///
  136. /// // Change the first hyperlinks text and Uri
  137. /// Hyperlink h0 = hyperlinks[0];
  138. /// h0.Text = "DocX";
  139. /// h0.Uri = new Uri("http://docx.codeplex.com");
  140. ///
  141. /// // Save this document.
  142. /// document.Save();
  143. /// }
  144. /// ]]>
  145. /// </code>
  146. /// </example>
  147. public Uri Uri
  148. {
  149. get
  150. {
  151. if (type == 0 && id != String.Empty)
  152. {
  153. PackageRelationship r = mainPart.GetRelationship(id);
  154. return r.TargetUri;
  155. }
  156. return this.uri;
  157. }
  158. set
  159. {
  160. if (type == 0)
  161. {
  162. PackageRelationship r = mainPart.GetRelationship(id);
  163. // Get all of the information about this relationship.
  164. TargetMode r_tm = r.TargetMode;
  165. string r_rt = r.RelationshipType;
  166. string r_id = r.Id;
  167. // Delete the relationship
  168. mainPart.DeleteRelationship(r_id);
  169. mainPart.CreateRelationship(value, r_tm, r_rt, r_id);
  170. }
  171. else
  172. {
  173. instrText.Value = "HYPERLINK " + "\"" + value + "\"";
  174. }
  175. this.uri = value;
  176. }
  177. }
  178. internal Hyperlink(DocX document, PackagePart mainPart, XElement i): base(document, i)
  179. {
  180. this.type = 0;
  181. this.id = i.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  182. StringBuilder sb = new StringBuilder();
  183. HelperFunctions.GetTextRecursive(i, ref sb);
  184. this.text = sb.ToString();
  185. }
  186. internal Hyperlink(DocX document, XElement instrText, List<XElement> runs) : base(document, null)
  187. {
  188. this.type = 1;
  189. this.instrText = instrText;
  190. this.runs = runs;
  191. try
  192. {
  193. int start = instrText.Value.IndexOf("HYPERLINK \"") + "HYPERLINK \"".Length;
  194. int end = instrText.Value.IndexOf("\"", start);
  195. if (start != -1 && end != -1)
  196. {
  197. this.uri = new Uri(instrText.Value.Substring(start, end - start), UriKind.Absolute);
  198. StringBuilder sb = new StringBuilder();
  199. HelperFunctions.GetTextRecursive(new XElement(XName.Get("temp", DocX.w.NamespaceName), runs), ref sb);
  200. this.text = sb.ToString();
  201. }
  202. }
  203. catch (Exception e){throw e;}
  204. }
  205. }
  206. }