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

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