Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

_BaseClasses.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. public virtual List InsertListAfterSelf( List list )
  207. {
  208. for( var i = list.Items.Count - 1; i >= 0; --i )
  209. {
  210. this.Xml.AddAfterSelf( list.Items[ i ].Xml );
  211. }
  212. return list;
  213. }
  214. public virtual List InsertListBeforeSelf( List list )
  215. {
  216. foreach( var item in list.Items )
  217. {
  218. this.Xml.AddBeforeSelf( item.Xml );
  219. }
  220. return list;
  221. }
  222. #endregion
  223. }
  224. public static class XmlTemplates
  225. {
  226. #region Public Constants
  227. public const string TableOfContentsXmlBase = @"
  228. <w:sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  229. <w:sdtPr>
  230. <w:docPartObj>
  231. <w:docPartGallery w:val='Table of Contents'/>
  232. <w:docPartUnique/>
  233. </w:docPartObj>\
  234. </w:sdtPr>
  235. <w:sdtEndPr>
  236. <w:rPr>
  237. <w:rFonts w:asciiTheme='minorHAnsi' w:cstheme='minorBidi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>
  238. <w:color w:val='auto'/>
  239. <w:sz w:val='22'/>
  240. <w:szCs w:val='22'/>
  241. <w:lang w:eastAsia='en-US'/>
  242. </w:rPr>
  243. </w:sdtEndPr>
  244. <w:sdtContent>
  245. <w:p>
  246. <w:pPr>
  247. <w:pStyle w:val='{0}'/>
  248. </w:pPr>
  249. <w:r>
  250. <w:t>{1}</w:t>
  251. </w:r>
  252. </w:p>
  253. <w:p>
  254. <w:pPr>
  255. <w:pStyle w:val='TOC1'/>
  256. <w:tabs>
  257. <w:tab w:val='right' w:leader='dot' w:pos='{2}'/>
  258. </w:tabs>
  259. <w:rPr>
  260. <w:noProof/>
  261. </w:rPr>
  262. </w:pPr>
  263. <w:r>
  264. <w:fldChar w:fldCharType='begin' w:dirty='true'/>
  265. </w:r>
  266. <w:r>
  267. <w:instrText xml:space='preserve'> {3} </w:instrText>
  268. </w:r>
  269. <w:r>
  270. <w:fldChar w:fldCharType='separate'/>
  271. </w:r>
  272. </w:p>
  273. <w:p>
  274. <w:r>
  275. <w:rPr>
  276. <w:b/>
  277. <w:bCs/>
  278. <w:noProof/>
  279. </w:rPr>
  280. <w:fldChar w:fldCharType='end'/>
  281. </w:r>
  282. </w:p>
  283. </w:sdtContent>
  284. </w:sdt>
  285. ";
  286. public const string TableOfContentsHeadingStyleBase = @"
  287. <w:style w:type='paragraph' w:styleId='{0}' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  288. <w:name w:val='TOC Heading'/>
  289. <w:basedOn w:val='Heading1'/>
  290. <w:next w:val='Normal'/>
  291. <w:uiPriority w:val='39'/>
  292. <w:semiHidden/>
  293. <w:unhideWhenUsed/>
  294. <w:qFormat/>
  295. <w:rsid w:val='00E67AA6'/>
  296. <w:pPr>
  297. <w:outlineLvl w:val='9'/>
  298. </w:pPr>
  299. <w:rPr>
  300. <w:lang w:eastAsia='nb-NO'/>
  301. </w:rPr>
  302. </w:style>
  303. ";
  304. public const string TableOfContentsElementStyleBase = @"
  305. <w:style w:type='paragraph' w:styleId='{0}' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  306. <w:name w:val='{1}' />
  307. <w:basedOn w:val='Normal' />
  308. <w:next w:val='Normal' />
  309. <w:autoRedefine />
  310. <w:uiPriority w:val='39' />
  311. <w:unhideWhenUsed />
  312. <w:pPr>
  313. <w:spacing w:after='100' />
  314. <w:ind w:left='440' />
  315. </w:pPr>
  316. </w:style>
  317. ";
  318. public const string TableOfContentsHyperLinkStyleBase = @"
  319. <w:style w:type='character' w:styleId='Hyperlink' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  320. <w:name w:val='Hyperlink' />
  321. <w:basedOn w:val='Normal' />
  322. <w:uiPriority w:val='99' />
  323. <w:unhideWhenUsed />
  324. <w:rPr>
  325. <w:color w:val='0000FF' w:themeColor='hyperlink' />
  326. <w:u w:val='single' />
  327. </w:rPr>
  328. </w:style>
  329. ";
  330. #endregion
  331. }
  332. }