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 7.3KB

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