Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

_BaseClasses.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.IO.Packaging;
  11. using System.Linq;
  12. using System.Xml.Linq;
  13. namespace Xceed.Words.NET
  14. {
  15. /// <summary>
  16. /// All DocX types are derived from DocXElement.
  17. /// This class contains properties which every element of a DocX must contain.
  18. /// </summary>
  19. public abstract class DocXElement
  20. {
  21. #region Private Members
  22. private PackagePart _mainPart;
  23. #endregion
  24. #region Public Properties
  25. /// <summary>
  26. /// This is the actual Xml that gives this element substance.
  27. /// For example, a Paragraphs Xml might look something like the following
  28. /// <p>
  29. /// <r>
  30. /// <t>Hello World!</t>
  31. /// </r>
  32. /// </p>
  33. /// </summary>
  34. public XElement Xml { get; set; }
  35. public PackagePart PackagePart
  36. {
  37. get
  38. {
  39. return _mainPart;
  40. }
  41. set
  42. {
  43. _mainPart = value;
  44. }
  45. }
  46. #endregion
  47. #region Internal Properties
  48. /// <summary>
  49. /// This is a reference to the DocX object that this element belongs to.
  50. /// Every DocX element is connected to a document.
  51. /// </summary>
  52. internal DocX Document { get; set; }
  53. #endregion
  54. #region Constructors
  55. /// <summary>
  56. /// Store both the document and xml so that they can be accessed by derived types.
  57. /// </summary>
  58. /// <param name="document">The document that this element belongs to.</param>
  59. /// <param name="xml">The Xml that gives this element substance</param>
  60. public DocXElement( DocX document, XElement xml )
  61. {
  62. this.Document = document;
  63. this.Xml = xml;
  64. }
  65. #endregion
  66. }
  67. /// <summary>
  68. /// This class provides functions for inserting new DocXElements before or after the current DocXElement.
  69. /// Only certain DocXElements can support these functions without creating invalid documents, at the moment these are Paragraphs and Table.
  70. /// </summary>
  71. public abstract class InsertBeforeOrAfter : DocXElement
  72. {
  73. #region Constructors
  74. public InsertBeforeOrAfter( DocX document, XElement xml )
  75. : base( document, xml )
  76. {
  77. }
  78. #endregion
  79. #region Public Methods
  80. public virtual void InsertPageBreakBeforeSelf()
  81. {
  82. XElement p = new XElement
  83. (
  84. XName.Get( "p", DocX.w.NamespaceName ),
  85. new XElement
  86. (
  87. XName.Get( "r", DocX.w.NamespaceName ),
  88. new XElement
  89. (
  90. XName.Get( "br", DocX.w.NamespaceName ),
  91. new XAttribute( XName.Get( "type", DocX.w.NamespaceName ), "page" )
  92. )
  93. )
  94. );
  95. Xml.AddBeforeSelf( p );
  96. }
  97. public virtual void InsertPageBreakAfterSelf()
  98. {
  99. XElement p = new XElement
  100. (
  101. XName.Get( "p", DocX.w.NamespaceName ),
  102. new XElement
  103. (
  104. XName.Get( "r", DocX.w.NamespaceName ),
  105. new XElement
  106. (
  107. XName.Get( "br", DocX.w.NamespaceName ),
  108. new XAttribute( XName.Get( "type", DocX.w.NamespaceName ), "page" )
  109. )
  110. )
  111. );
  112. Xml.AddAfterSelf( p );
  113. }
  114. public virtual Paragraph InsertParagraphBeforeSelf( Paragraph p )
  115. {
  116. Xml.AddBeforeSelf( p.Xml );
  117. XElement newlyInserted = Xml.ElementsBeforeSelf().First();
  118. p.Xml = newlyInserted;
  119. return p;
  120. }
  121. public virtual Paragraph InsertParagraphAfterSelf( Paragraph p )
  122. {
  123. Xml.AddAfterSelf( p.Xml );
  124. XElement newlyInserted = Xml.ElementsAfterSelf().First();
  125. //Dmitchern
  126. if( this as Paragraph != null )
  127. return new Paragraph( Document, newlyInserted, ( this as Paragraph )._endIndex );
  128. p.Xml = newlyInserted; //IMPORTANT: I think we have return new paragraph in any case, but I dont know what to put as startIndex parameter into Paragraph constructor
  129. return p;
  130. }
  131. public virtual Paragraph InsertParagraphBeforeSelf( string text )
  132. {
  133. return InsertParagraphBeforeSelf( text, false, new Formatting() );
  134. }
  135. public virtual Paragraph InsertParagraphAfterSelf( string text )
  136. {
  137. return InsertParagraphAfterSelf( text, false, new Formatting() );
  138. }
  139. public virtual Paragraph InsertParagraphBeforeSelf( string text, bool trackChanges )
  140. {
  141. return InsertParagraphBeforeSelf( text, trackChanges, new Formatting() );
  142. }
  143. public virtual Paragraph InsertParagraphAfterSelf( string text, bool trackChanges )
  144. {
  145. return InsertParagraphAfterSelf( text, trackChanges, new Formatting() );
  146. }
  147. public virtual Paragraph InsertParagraphBeforeSelf( string text, bool trackChanges, Formatting formatting )
  148. {
  149. XElement newParagraph = new XElement
  150. (
  151. XName.Get( "p", DocX.w.NamespaceName ), new XElement( XName.Get( "pPr", DocX.w.NamespaceName ) ), HelperFunctions.FormatInput( text, formatting.Xml )
  152. );
  153. if( trackChanges )
  154. newParagraph = Paragraph.CreateEdit( EditType.ins, DateTime.Now, newParagraph );
  155. Xml.AddBeforeSelf( newParagraph );
  156. XElement newlyInserted = Xml.ElementsBeforeSelf().Last();
  157. return new Paragraph( Document, newlyInserted, -1 );
  158. }
  159. public virtual Paragraph InsertParagraphAfterSelf( string text, bool trackChanges, Formatting formatting )
  160. {
  161. XElement newParagraph = new XElement
  162. (
  163. XName.Get( "p", DocX.w.NamespaceName ), new XElement( XName.Get( "pPr", DocX.w.NamespaceName ) ), HelperFunctions.FormatInput( text, formatting.Xml )
  164. );
  165. if( trackChanges )
  166. newParagraph = Paragraph.CreateEdit( EditType.ins, DateTime.Now, newParagraph );
  167. Xml.AddAfterSelf( newParagraph );
  168. XElement newlyInserted = Xml.ElementsAfterSelf().First();
  169. Paragraph p = new Paragraph( Document, newlyInserted, -1 );
  170. return p;
  171. }
  172. public virtual Table InsertTableAfterSelf( int rowCount, int columnCount )
  173. {
  174. var newTable = HelperFunctions.CreateTable( rowCount, columnCount );
  175. Xml.AddAfterSelf( newTable );
  176. var newlyInserted = this.Xml.ElementsAfterSelf().First();
  177. var table = new Table( this.Document, newlyInserted );
  178. table.PackagePart = this.PackagePart;
  179. return table;
  180. }
  181. public virtual Table InsertTableAfterSelf( Table t )
  182. {
  183. this.Xml.AddAfterSelf( t.Xml );
  184. var newlyInserted = this.Xml.ElementsAfterSelf().First();
  185. var table = new Table( this.Document, newlyInserted );
  186. table.PackagePart = this.PackagePart;
  187. return table;
  188. }
  189. public virtual Table InsertTableBeforeSelf( int rowCount, int columnCount )
  190. {
  191. var newTable = HelperFunctions.CreateTable( rowCount, columnCount );
  192. this.Xml.AddBeforeSelf( newTable );
  193. var newlyInserted = this.Xml.ElementsBeforeSelf().Last();
  194. var table = new Table( this.Document, newlyInserted );
  195. table.PackagePart = this.PackagePart;
  196. return table;
  197. }
  198. public virtual Table InsertTableBeforeSelf( Table t )
  199. {
  200. this.Xml.AddBeforeSelf( t.Xml );
  201. var newlyInserted = this.Xml.ElementsBeforeSelf().Last();
  202. var table = new Table( this.Document, newlyInserted );
  203. table.PackagePart = this.PackagePart;
  204. return table;
  205. }
  206. #endregion
  207. }
  208. public static class XmlTemplates
  209. {
  210. #region Public Constants
  211. public const string TableOfContentsXmlBase = @"
  212. <w:sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  213. <w:sdtPr>
  214. <w:docPartObj>
  215. <w:docPartGallery w:val='Table of Contents'/>
  216. <w:docPartUnique/>
  217. </w:docPartObj>\
  218. </w:sdtPr>
  219. <w:sdtEndPr>
  220. <w:rPr>
  221. <w:rFonts w:asciiTheme='minorHAnsi' w:cstheme='minorBidi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>
  222. <w:color w:val='auto'/>
  223. <w:sz w:val='22'/>
  224. <w:szCs w:val='22'/>
  225. <w:lang w:eastAsia='en-US'/>
  226. </w:rPr>
  227. </w:sdtEndPr>
  228. <w:sdtContent>
  229. <w:p>
  230. <w:pPr>
  231. <w:pStyle w:val='{0}'/>
  232. </w:pPr>
  233. <w:r>
  234. <w:t>{1}</w:t>
  235. </w:r>
  236. </w:p>
  237. <w:p>
  238. <w:pPr>
  239. <w:pStyle w:val='TOC1'/>
  240. <w:tabs>
  241. <w:tab w:val='right' w:leader='dot' w:pos='{2}'/>
  242. </w:tabs>
  243. <w:rPr>
  244. <w:noProof/>
  245. </w:rPr>
  246. </w:pPr>
  247. <w:r>
  248. <w:fldChar w:fldCharType='begin' w:dirty='true'/>
  249. </w:r>
  250. <w:r>
  251. <w:instrText xml:space='preserve'> {3} </w:instrText>
  252. </w:r>
  253. <w:r>
  254. <w:fldChar w:fldCharType='separate'/>
  255. </w:r>
  256. </w:p>
  257. <w:p>
  258. <w:r>
  259. <w:rPr>
  260. <w:b/>
  261. <w:bCs/>
  262. <w:noProof/>
  263. </w:rPr>
  264. <w:fldChar w:fldCharType='end'/>
  265. </w:r>
  266. </w:p>
  267. </w:sdtContent>
  268. </w:sdt>
  269. ";
  270. public const string TableOfContentsHeadingStyleBase = @"
  271. <w:style w:type='paragraph' w:styleId='{0}' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  272. <w:name w:val='TOC Heading'/>
  273. <w:basedOn w:val='Heading1'/>
  274. <w:next w:val='Normal'/>
  275. <w:uiPriority w:val='39'/>
  276. <w:semiHidden/>
  277. <w:unhideWhenUsed/>
  278. <w:qFormat/>
  279. <w:rsid w:val='00E67AA6'/>
  280. <w:pPr>
  281. <w:outlineLvl w:val='9'/>
  282. </w:pPr>
  283. <w:rPr>
  284. <w:lang w:eastAsia='nb-NO'/>
  285. </w:rPr>
  286. </w:style>
  287. ";
  288. public const string TableOfContentsElementStyleBase = @"
  289. <w:style w:type='paragraph' w:styleId='{0}' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  290. <w:name w:val='{1}' />
  291. <w:basedOn w:val='Normal' />
  292. <w:next w:val='Normal' />
  293. <w:autoRedefine />
  294. <w:uiPriority w:val='39' />
  295. <w:unhideWhenUsed />
  296. <w:pPr>
  297. <w:spacing w:after='100' />
  298. <w:ind w:left='440' />
  299. </w:pPr>
  300. </w:style>
  301. ";
  302. public const string TableOfContentsHyperLinkStyleBase = @"
  303. <w:style w:type='character' w:styleId='Hyperlink' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  304. <w:name w:val='Hyperlink' />
  305. <w:basedOn w:val='Normal' />
  306. <w:uiPriority w:val='99' />
  307. <w:unhideWhenUsed />
  308. <w:rPr>
  309. <w:color w:val='0000FF' w:themeColor='hyperlink' />
  310. <w:u w:val='single' />
  311. </w:rPr>
  312. </w:style>
  313. ";
  314. #endregion
  315. }
  316. }