Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Hyperlink.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Dictionary<PackagePart, PackageRelationship> hyperlink_rels;
  16. /// <summary>
  17. /// Remove a Hyperlink from this Paragraph only.
  18. /// </summary>
  19. /// <example>
  20. /// <code>
  21. /// // Create a document.
  22. /// using (DocX document = DocX.Create(@"Test.docx"))
  23. /// {
  24. /// // Add a hyperlink to this document.
  25. /// Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  26. ///
  27. /// // Add a Paragraph to this document and insert the hyperlink
  28. /// Paragraph p1 = document.InsertParagraph();
  29. /// p1.Append("This is a cool ").AppendHyperlink(h).Append(" .");
  30. ///
  31. /// /*
  32. /// * Remove the hyperlink from this Paragraph only.
  33. /// * Note a reference to the hyperlink will still exist in the document and it can thus be reused.
  34. /// */
  35. /// p1.Hyperlinks[0].Remove();
  36. ///
  37. /// // Add a new Paragraph to this document and reuse the hyperlink h.
  38. /// Paragraph p2 = document.InsertParagraph();
  39. /// p2.Append("This is the same cool ").AppendHyperlink(h).Append(" .");
  40. ///
  41. /// document.Save();
  42. /// }// Release this document from memory.
  43. /// </code>
  44. /// </example>
  45. public void Remove()
  46. {
  47. Xml.Remove();
  48. }
  49. /// <summary>
  50. /// Change the Text of a Hyperlink.
  51. /// </summary>
  52. /// <example>
  53. /// Change the Text of a Hyperlink.
  54. /// <code>
  55. /// // Create a document.
  56. /// using (DocX document = DocX.Load(@"Test.docx"))
  57. /// {
  58. /// // Get all of the hyperlinks in this document
  59. /// List<Hyperlink> hyperlinks = document.Hyperlinks;
  60. ///
  61. /// // Change the first hyperlinks text and Uri
  62. /// Hyperlink h0 = hyperlinks[0];
  63. /// h0.Text = "DocX";
  64. /// h0.Uri = new Uri("http://docx.codeplex.com");
  65. ///
  66. /// // Save this document.
  67. /// document.Save();
  68. /// }
  69. /// </code>
  70. /// </example>
  71. public string Text
  72. {
  73. get
  74. {
  75. // Create a string builder.
  76. StringBuilder sb = new StringBuilder();
  77. // Get all the runs in this Text.
  78. var runs = from r in Xml.Elements()
  79. where r.Name.LocalName == "r"
  80. select new Run(Document, r, 0);
  81. // Remove each run.
  82. foreach (Run r in runs)
  83. sb.Append(r.Value);
  84. return sb.ToString();
  85. }
  86. set
  87. {
  88. // Get all the runs in this Text.
  89. var runs = from r in Xml.Elements()
  90. where r.Name.LocalName == "r"
  91. select r;
  92. // Remove each run.
  93. for (int i = 0; i < runs.Count(); i++)
  94. runs.Remove();
  95. XElement rPr =
  96. new XElement
  97. (
  98. DocX.w + "rPr",
  99. new XElement
  100. (
  101. DocX.w + "rStyle",
  102. new XAttribute(DocX.w + "val", "Hyperlink")
  103. )
  104. );
  105. // Format and add the new text.
  106. List<XElement> newRuns = HelperFunctions.FormatInput(value, rPr);
  107. Xml.Add(newRuns);
  108. }
  109. }
  110. /// <summary>
  111. /// Change the Uri of a Hyperlink.
  112. /// </summary>
  113. /// <example>
  114. /// Change the Uri of a Hyperlink.
  115. /// <code>
  116. /// // Create a document.
  117. /// using (DocX document = DocX.Load(@"Test.docx"))
  118. /// {
  119. /// // Get all of the hyperlinks in this document
  120. /// List<Hyperlink> hyperlinks = document.Hyperlinks;
  121. ///
  122. /// // Change the first hyperlinks text and Uri
  123. /// Hyperlink h0 = hyperlinks[0];
  124. /// h0.Text = "DocX";
  125. /// h0.Uri = new Uri("http://docx.codeplex.com");
  126. ///
  127. /// // Save this document.
  128. /// document.Save();
  129. /// }
  130. /// </code>
  131. /// </example>
  132. public Uri Uri
  133. {
  134. get
  135. {
  136. // Return the Hyperlinks Uri.
  137. return uri;
  138. }
  139. set
  140. {
  141. foreach (PackagePart p in hyperlink_rels.Keys)
  142. {
  143. PackageRelationship r = hyperlink_rels[p];
  144. // Get all of the information about this relationship.
  145. TargetMode r_tm = r.TargetMode;
  146. string r_rt = r.RelationshipType;
  147. string r_id = r.Id;
  148. // Delete the relationship
  149. p.DeleteRelationship(r_id);
  150. p.CreateRelationship(value, r_tm, r_rt, r_id);
  151. }
  152. uri = value;
  153. }
  154. }
  155. internal Hyperlink(DocX document, XElement i): base(document, i)
  156. {
  157. hyperlink_rels = new Dictionary<PackagePart, PackageRelationship>();
  158. }
  159. }
  160. }