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.

_BaseClasses.cs 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. namespace Novacode
  7. {
  8. /// <summary>
  9. /// All DocX types are derived from DocXElement.
  10. /// This class contains properties which every element of a DocX must contain.
  11. /// </summary>
  12. public abstract class DocXElement
  13. {
  14. /// <summary>
  15. /// This is the actual Xml that gives this element substance.
  16. /// For example, a Paragraphs Xml might look something like the following
  17. /// <p>
  18. /// <r>
  19. /// <t>Hello World!</t>
  20. /// </r>
  21. /// </p>
  22. /// </summary>
  23. private XElement xml;
  24. internal XElement Xml { get { return xml; } set { xml = value; } }
  25. /// <summary>
  26. /// This is a reference to the DocX object that this element belongs to.
  27. /// Every DocX element is connected to a document.
  28. /// </summary>
  29. private DocX document;
  30. internal DocX Document { get { return document; } set { document = value; } }
  31. /// <summary>
  32. /// Store both the document and xml so that they can be accessed by derived types.
  33. /// </summary>
  34. /// <param name="document">The document that this element belongs to.</param>
  35. /// <param name="xml">The Xml that gives this element substance</param>
  36. public DocXElement(DocX document, XElement xml)
  37. {
  38. this.document = document;
  39. this.xml = xml;
  40. }
  41. }
  42. /// <summary>
  43. /// This class provides functions for inserting new DocXElements before or after the current DocXElement.
  44. /// Only certain DocXElements can support these functions without creating invalid documents, at the moment these are Paragraphs and Table.
  45. /// </summary>
  46. public abstract class InsertBeforeOrAfter:DocXElement
  47. {
  48. public InsertBeforeOrAfter(DocX document, XElement xml):base(document, xml) { }
  49. public virtual void InsertPageBreakBeforeSelf()
  50. {
  51. XElement p = new XElement
  52. (
  53. XName.Get("p", DocX.w.NamespaceName),
  54. new XElement
  55. (
  56. XName.Get("r", DocX.w.NamespaceName),
  57. new XElement
  58. (
  59. XName.Get("br", DocX.w.NamespaceName),
  60. new XAttribute(XName.Get("type", DocX.w.NamespaceName), "page")
  61. )
  62. )
  63. );
  64. Xml.AddBeforeSelf(p);
  65. }
  66. public virtual void InsertPageBreakAfterSelf()
  67. {
  68. XElement p = new XElement
  69. (
  70. XName.Get("p", DocX.w.NamespaceName),
  71. new XElement
  72. (
  73. XName.Get("r", DocX.w.NamespaceName),
  74. new XElement
  75. (
  76. XName.Get("br", DocX.w.NamespaceName),
  77. new XAttribute(XName.Get("type", DocX.w.NamespaceName), "page")
  78. )
  79. )
  80. );
  81. Xml.AddAfterSelf(p);
  82. }
  83. public virtual Paragraph InsertParagraphBeforeSelf(Paragraph p)
  84. {
  85. Xml.AddBeforeSelf(p.Xml);
  86. XElement newlyInserted = Xml.ElementsBeforeSelf().First();
  87. p.Xml = newlyInserted;
  88. DocX.RebuildParagraphs(Document);
  89. return p;
  90. }
  91. public virtual Paragraph InsertParagraphAfterSelf(Paragraph p)
  92. {
  93. Xml.AddAfterSelf(p.Xml);
  94. XElement newlyInserted = Xml.ElementsAfterSelf().First();
  95. p.Xml = newlyInserted;
  96. DocX.RebuildParagraphs(Document);
  97. return p;
  98. }
  99. public virtual Paragraph InsertParagraphBeforeSelf(string text)
  100. {
  101. return InsertParagraphBeforeSelf(text, false, new Formatting());
  102. }
  103. public virtual Paragraph InsertParagraphAfterSelf(string text)
  104. {
  105. return InsertParagraphAfterSelf(text, false, new Formatting());
  106. }
  107. public virtual Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
  108. {
  109. return InsertParagraphBeforeSelf(text, trackChanges, new Formatting());
  110. }
  111. public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
  112. {
  113. return InsertParagraphAfterSelf(text, trackChanges, new Formatting());
  114. }
  115. public virtual Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
  116. {
  117. XElement newParagraph = new XElement
  118. (
  119. XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
  120. );
  121. if (trackChanges)
  122. newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
  123. Xml.AddBeforeSelf(newParagraph);
  124. XElement newlyInserted = Xml.ElementsBeforeSelf().First();
  125. Paragraph p = new Paragraph(Document, newlyInserted, -1);
  126. DocX.RebuildParagraphs(Document);
  127. return p;
  128. }
  129. public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
  130. {
  131. XElement newParagraph = new XElement
  132. (
  133. XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
  134. );
  135. if (trackChanges)
  136. newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
  137. Xml.AddAfterSelf(newParagraph);
  138. XElement newlyInserted = Xml.ElementsAfterSelf().First();
  139. Paragraph p = new Paragraph(Document, newlyInserted, -1);
  140. DocX.RebuildParagraphs(Document);
  141. return p;
  142. }
  143. public virtual Table InsertTableAfterSelf(int rowCount, int coloumnCount)
  144. {
  145. XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
  146. Xml.AddAfterSelf(newTable);
  147. XElement newlyInserted = Xml.ElementsAfterSelf().First();
  148. DocX.RebuildTables(Document);
  149. DocX.RebuildParagraphs(Document);
  150. return new Table(Document, newlyInserted);
  151. }
  152. public virtual Table InsertTableAfterSelf(Table t)
  153. {
  154. Xml.AddAfterSelf(t.Xml);
  155. XElement newlyInserted = Xml.ElementsAfterSelf().First();
  156. t.Xml = newlyInserted;
  157. DocX.RebuildTables(Document);
  158. DocX.RebuildParagraphs(Document);
  159. return t;
  160. }
  161. public virtual Table InsertTableBeforeSelf(int rowCount, int coloumnCount)
  162. {
  163. XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
  164. Xml.AddBeforeSelf(newTable);
  165. XElement newlyInserted = Xml.ElementsBeforeSelf().First();
  166. DocX.RebuildTables(Document);
  167. DocX.RebuildParagraphs(Document);
  168. return new Table(Document, newlyInserted);
  169. }
  170. public virtual Table InsertTableBeforeSelf(Table t)
  171. {
  172. Xml.AddBeforeSelf(t.Xml);
  173. XElement newlyInserted = Xml.ElementsBeforeSelf().First();
  174. t.Xml = newlyInserted;
  175. DocX.RebuildTables(Document);
  176. DocX.RebuildParagraphs(Document);
  177. return t;
  178. }
  179. }
  180. }