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.

Table.cs 131KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049
  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.Xml.Linq;
  13. using System.IO.Packaging;
  14. using System.IO;
  15. using System.Drawing;
  16. using System.Globalization;
  17. using System.Collections.ObjectModel;
  18. namespace Xceed.Words.NET
  19. {
  20. /// <summary>
  21. /// Represents a Table in a document.
  22. /// </summary>
  23. public class Table : InsertBeforeOrAfter
  24. {
  25. #region Private Members
  26. private Alignment _alignment;
  27. private AutoFit _autofit;
  28. private float[] _columnWidths;
  29. private TableDesign _design;
  30. /// <summary>
  31. /// The custom design\style to apply to this table.
  32. ///
  33. /// </summary>
  34. private string _customTableDesignName;
  35. private int _cachedColumnCount = -1;
  36. #endregion
  37. #region Public Properties
  38. /// <summary>
  39. /// Returns a list of all Paragraphs inside this container.
  40. /// </summary>
  41. ///
  42. public virtual List<Paragraph> Paragraphs
  43. {
  44. get
  45. {
  46. var paragraphs = new List<Paragraph>();
  47. foreach( Row r in Rows )
  48. paragraphs.AddRange( r.Paragraphs );
  49. return paragraphs;
  50. }
  51. }
  52. /// <summary>
  53. /// Returns a list of all Pictures in a Table.
  54. /// </summary>
  55. /// <example>
  56. /// Returns a list of all Pictures in a Table.
  57. /// <code>
  58. /// <![CDATA[
  59. /// // Create a document.
  60. /// using (DocX document = DocX.Load(@"Test.docx"))
  61. /// {
  62. /// // Get the first Table in a document.
  63. /// Table t = document.Tables[0];
  64. ///
  65. /// // Get all of the Pictures in this Table.
  66. /// List<Picture> pictures = t.Pictures;
  67. ///
  68. /// // Save this document.
  69. /// document.Save();
  70. /// }
  71. /// ]]>
  72. /// </code>
  73. /// </example>
  74. public List<Picture> Pictures
  75. {
  76. get
  77. {
  78. var pictures = new List<Picture>();
  79. foreach( Row r in Rows )
  80. {
  81. pictures.AddRange( r.Pictures );
  82. }
  83. return pictures;
  84. }
  85. }
  86. /// <summary>
  87. /// Get all of the Hyperlinks in this Table.
  88. /// </summary>
  89. /// <example>
  90. /// Get all of the Hyperlinks in this Table.
  91. /// <code>
  92. /// // Create a document.
  93. /// using (DocX document = DocX.Load(@"Test.docx"))
  94. /// {
  95. /// // Get the first Table in this document.
  96. /// Table t = document.Tables[0];
  97. ///
  98. /// // Get a list of all Hyperlinks in this Table.
  99. /// List&lt;Hyperlink&gt; hyperlinks = t.Hyperlinks;
  100. ///
  101. /// // Save this document.
  102. /// document.Save();
  103. /// }
  104. /// </code>
  105. /// </example>
  106. public List<Hyperlink> Hyperlinks
  107. {
  108. get
  109. {
  110. var hyperlinks = new List<Hyperlink>();
  111. foreach( Row r in Rows )
  112. {
  113. hyperlinks.AddRange( r.Hyperlinks );
  114. }
  115. return hyperlinks;
  116. }
  117. }
  118. /// <summary>
  119. /// Returns the number of rows in this table.
  120. /// </summary>
  121. public Int32 RowCount
  122. {
  123. get
  124. {
  125. return this.Xml.Elements( XName.Get( "tr", DocX.w.NamespaceName ) ).Count();
  126. }
  127. }
  128. /// <summary>
  129. /// Returns the number of columns in this table.
  130. /// </summary>
  131. public Int32 ColumnCount
  132. {
  133. get
  134. {
  135. if( this.RowCount == 0 )
  136. return 0;
  137. if( _cachedColumnCount == -1 )
  138. {
  139. foreach( var r in this.Rows )
  140. {
  141. _cachedColumnCount = Math.Max( _cachedColumnCount, r.ColumnCount );
  142. }
  143. }
  144. return _cachedColumnCount;
  145. }
  146. }
  147. /// <summary>
  148. /// Returns a list of rows in this table.
  149. /// </summary>
  150. public List<Row> Rows
  151. {
  152. get
  153. {
  154. var rows =
  155. (
  156. from r in Xml.Elements( XName.Get( "tr", DocX.w.NamespaceName ) )
  157. select new Row( this, Document, r )
  158. ).ToList();
  159. return rows;
  160. }
  161. }
  162. public Alignment Alignment
  163. {
  164. get
  165. {
  166. return _alignment;
  167. }
  168. set
  169. {
  170. string alignmentString = string.Empty;
  171. switch( value )
  172. {
  173. case Alignment.left:
  174. {
  175. alignmentString = "left";
  176. break;
  177. }
  178. case Alignment.both:
  179. {
  180. alignmentString = "both";
  181. break;
  182. }
  183. case Alignment.right:
  184. {
  185. alignmentString = "right";
  186. break;
  187. }
  188. case Alignment.center:
  189. {
  190. alignmentString = "center";
  191. break;
  192. }
  193. }
  194. XElement tblPr = Xml.Descendants( XName.Get( "tblPr", DocX.w.NamespaceName ) ).First();
  195. XElement jc = tblPr.Descendants( XName.Get( "jc", DocX.w.NamespaceName ) ).FirstOrDefault();
  196. jc?.Remove();
  197. jc = new XElement( XName.Get( "jc", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), alignmentString ) );
  198. tblPr.Add( jc );
  199. _alignment = value;
  200. }
  201. }
  202. /// <summary>
  203. /// Auto size this table according to some rule.
  204. /// </summary>
  205. public AutoFit AutoFit
  206. {
  207. get
  208. {
  209. return _autofit;
  210. }
  211. set
  212. {
  213. string tableAttributeValue = string.Empty;
  214. string columnAttributeValue = string.Empty;
  215. switch( value )
  216. {
  217. case AutoFit.ColumnWidth:
  218. {
  219. tableAttributeValue = "auto";
  220. columnAttributeValue = "dxa";
  221. // Disable "Automatically resize to fit contents" option
  222. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  223. if( tblPr != null )
  224. {
  225. var layout = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
  226. if( layout == null )
  227. {
  228. tblPr.Add( new XElement( XName.Get( "tblLayout", DocX.w.NamespaceName ) ) );
  229. layout = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
  230. }
  231. var type = layout.Attribute( XName.Get( "type", DocX.w.NamespaceName ) );
  232. if( type == null )
  233. {
  234. layout.Add( new XAttribute( XName.Get( "type", DocX.w.NamespaceName ), String.Empty ) );
  235. type = layout.Attribute( XName.Get( "type", DocX.w.NamespaceName ) );
  236. }
  237. type.Value = "fixed";
  238. }
  239. break;
  240. }
  241. case AutoFit.Contents:
  242. {
  243. tableAttributeValue = columnAttributeValue = "auto";
  244. break;
  245. }
  246. case AutoFit.Window:
  247. {
  248. tableAttributeValue = columnAttributeValue = "pct";
  249. break;
  250. }
  251. case AutoFit.Fixed:
  252. {
  253. tableAttributeValue = columnAttributeValue = "dxa";
  254. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  255. var tblLayout = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
  256. if( tblLayout == null )
  257. {
  258. var tmp = tblPr.Element( XName.Get( "tblInd", DocX.w.NamespaceName ) ) ?? tblPr.Element( XName.Get( "tblW", DocX.w.NamespaceName ) );
  259. tmp.AddAfterSelf( new XElement( XName.Get( "tblLayout", DocX.w.NamespaceName ) ) );
  260. tmp = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
  261. tmp.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "fixed" );
  262. tmp = tblPr.Element( XName.Get( "tblW", DocX.w.NamespaceName ) );
  263. Double totalWidth = 0;
  264. foreach( Double columnWidth in ColumnWidths )
  265. {
  266. totalWidth += columnWidth;
  267. }
  268. tmp.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), totalWidth.ToString() );
  269. break;
  270. }
  271. else
  272. {
  273. var types = from d in Xml.Descendants()
  274. let type = d.Attribute( XName.Get( "type", DocX.w.NamespaceName ) )
  275. where ( d.Name.LocalName == "tblLayout" ) && type != null
  276. select type;
  277. foreach( XAttribute type in types )
  278. {
  279. type.Value = "fixed";
  280. }
  281. var tmp = tblPr.Element( XName.Get( "tblW", DocX.w.NamespaceName ) );
  282. Double totalWidth = 0;
  283. foreach( Double columnWidth in ColumnWidths )
  284. {
  285. totalWidth += columnWidth;
  286. }
  287. tmp.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), totalWidth.ToString() );
  288. break;
  289. }
  290. }
  291. }
  292. // Set table attributes
  293. var query = from d in Xml.Descendants()
  294. let type = d.Attribute( XName.Get( "type", DocX.w.NamespaceName ) )
  295. where ( d.Name.LocalName == "tblW" ) && type != null
  296. select type;
  297. foreach( XAttribute type in query )
  298. {
  299. type.Value = tableAttributeValue;
  300. }
  301. // Set column attributes
  302. query = from d in Xml.Descendants()
  303. let type = d.Attribute( XName.Get( "type", DocX.w.NamespaceName ) )
  304. where ( d.Name.LocalName == "tcW" ) && type != null
  305. select type;
  306. foreach( XAttribute type in query )
  307. {
  308. type.Value = columnAttributeValue;
  309. }
  310. _autofit = value;
  311. }
  312. }
  313. /// <summary>
  314. /// The design\style to apply to this table.
  315. /// </summary>
  316. public TableDesign Design
  317. {
  318. get
  319. {
  320. return _design;
  321. }
  322. set
  323. {
  324. XElement tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  325. XElement style = tblPr.Element( XName.Get( "tblStyle", DocX.w.NamespaceName ) );
  326. if( style == null )
  327. {
  328. tblPr.Add( new XElement( XName.Get( "tblStyle", DocX.w.NamespaceName ) ) );
  329. style = tblPr.Element( XName.Get( "tblStyle", DocX.w.NamespaceName ) );
  330. }
  331. XAttribute val = style.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  332. if( val == null )
  333. {
  334. style.Add( new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), "" ) );
  335. val = style.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  336. }
  337. _design = value;
  338. if( _design == TableDesign.None )
  339. {
  340. if( style != null )
  341. style.Remove();
  342. }
  343. if( _design != TableDesign.Custom )
  344. {
  345. switch( _design )
  346. {
  347. case TableDesign.TableNormal:
  348. val.Value = "TableNormal";
  349. break;
  350. case TableDesign.TableGrid:
  351. val.Value = "TableGrid";
  352. break;
  353. case TableDesign.LightShading:
  354. val.Value = "LightShading";
  355. break;
  356. case TableDesign.LightShadingAccent1:
  357. val.Value = "LightShading-Accent1";
  358. break;
  359. case TableDesign.LightShadingAccent2:
  360. val.Value = "LightShading-Accent2";
  361. break;
  362. case TableDesign.LightShadingAccent3:
  363. val.Value = "LightShading-Accent3";
  364. break;
  365. case TableDesign.LightShadingAccent4:
  366. val.Value = "LightShading-Accent4";
  367. break;
  368. case TableDesign.LightShadingAccent5:
  369. val.Value = "LightShading-Accent5";
  370. break;
  371. case TableDesign.LightShadingAccent6:
  372. val.Value = "LightShading-Accent6";
  373. break;
  374. case TableDesign.LightList:
  375. val.Value = "LightList";
  376. break;
  377. case TableDesign.LightListAccent1:
  378. val.Value = "LightList-Accent1";
  379. break;
  380. case TableDesign.LightListAccent2:
  381. val.Value = "LightList-Accent2";
  382. break;
  383. case TableDesign.LightListAccent3:
  384. val.Value = "LightList-Accent3";
  385. break;
  386. case TableDesign.LightListAccent4:
  387. val.Value = "LightList-Accent4";
  388. break;
  389. case TableDesign.LightListAccent5:
  390. val.Value = "LightList-Accent5";
  391. break;
  392. case TableDesign.LightListAccent6:
  393. val.Value = "LightList-Accent6";
  394. break;
  395. case TableDesign.LightGrid:
  396. val.Value = "LightGrid";
  397. break;
  398. case TableDesign.LightGridAccent1:
  399. val.Value = "LightGrid-Accent1";
  400. break;
  401. case TableDesign.LightGridAccent2:
  402. val.Value = "LightGrid-Accent2";
  403. break;
  404. case TableDesign.LightGridAccent3:
  405. val.Value = "LightGrid-Accent3";
  406. break;
  407. case TableDesign.LightGridAccent4:
  408. val.Value = "LightGrid-Accent4";
  409. break;
  410. case TableDesign.LightGridAccent5:
  411. val.Value = "LightGrid-Accent5";
  412. break;
  413. case TableDesign.LightGridAccent6:
  414. val.Value = "LightGrid-Accent6";
  415. break;
  416. case TableDesign.MediumShading1:
  417. val.Value = "MediumShading1";
  418. break;
  419. case TableDesign.MediumShading1Accent1:
  420. val.Value = "MediumShading1-Accent1";
  421. break;
  422. case TableDesign.MediumShading1Accent2:
  423. val.Value = "MediumShading1-Accent2";
  424. break;
  425. case TableDesign.MediumShading1Accent3:
  426. val.Value = "MediumShading1-Accent3";
  427. break;
  428. case TableDesign.MediumShading1Accent4:
  429. val.Value = "MediumShading1-Accent4";
  430. break;
  431. case TableDesign.MediumShading1Accent5:
  432. val.Value = "MediumShading1-Accent5";
  433. break;
  434. case TableDesign.MediumShading1Accent6:
  435. val.Value = "MediumShading1-Accent6";
  436. break;
  437. case TableDesign.MediumShading2:
  438. val.Value = "MediumShading2";
  439. break;
  440. case TableDesign.MediumShading2Accent1:
  441. val.Value = "MediumShading2-Accent1";
  442. break;
  443. case TableDesign.MediumShading2Accent2:
  444. val.Value = "MediumShading2-Accent2";
  445. break;
  446. case TableDesign.MediumShading2Accent3:
  447. val.Value = "MediumShading2-Accent3";
  448. break;
  449. case TableDesign.MediumShading2Accent4:
  450. val.Value = "MediumShading2-Accent4";
  451. break;
  452. case TableDesign.MediumShading2Accent5:
  453. val.Value = "MediumShading2-Accent5";
  454. break;
  455. case TableDesign.MediumShading2Accent6:
  456. val.Value = "MediumShading2-Accent6";
  457. break;
  458. case TableDesign.MediumList1:
  459. val.Value = "MediumList1";
  460. break;
  461. case TableDesign.MediumList1Accent1:
  462. val.Value = "MediumList1-Accent1";
  463. break;
  464. case TableDesign.MediumList1Accent2:
  465. val.Value = "MediumList1-Accent2";
  466. break;
  467. case TableDesign.MediumList1Accent3:
  468. val.Value = "MediumList1-Accent3";
  469. break;
  470. case TableDesign.MediumList1Accent4:
  471. val.Value = "MediumList1-Accent4";
  472. break;
  473. case TableDesign.MediumList1Accent5:
  474. val.Value = "MediumList1-Accent5";
  475. break;
  476. case TableDesign.MediumList1Accent6:
  477. val.Value = "MediumList1-Accent6";
  478. break;
  479. case TableDesign.MediumList2:
  480. val.Value = "MediumList2";
  481. break;
  482. case TableDesign.MediumList2Accent1:
  483. val.Value = "MediumList2-Accent1";
  484. break;
  485. case TableDesign.MediumList2Accent2:
  486. val.Value = "MediumList2-Accent2";
  487. break;
  488. case TableDesign.MediumList2Accent3:
  489. val.Value = "MediumList2-Accent3";
  490. break;
  491. case TableDesign.MediumList2Accent4:
  492. val.Value = "MediumList2-Accent4";
  493. break;
  494. case TableDesign.MediumList2Accent5:
  495. val.Value = "MediumList2-Accent5";
  496. break;
  497. case TableDesign.MediumList2Accent6:
  498. val.Value = "MediumList2-Accent6";
  499. break;
  500. case TableDesign.MediumGrid1:
  501. val.Value = "MediumGrid1";
  502. break;
  503. case TableDesign.MediumGrid1Accent1:
  504. val.Value = "MediumGrid1-Accent1";
  505. break;
  506. case TableDesign.MediumGrid1Accent2:
  507. val.Value = "MediumGrid1-Accent2";
  508. break;
  509. case TableDesign.MediumGrid1Accent3:
  510. val.Value = "MediumGrid1-Accent3";
  511. break;
  512. case TableDesign.MediumGrid1Accent4:
  513. val.Value = "MediumGrid1-Accent4";
  514. break;
  515. case TableDesign.MediumGrid1Accent5:
  516. val.Value = "MediumGrid1-Accent5";
  517. break;
  518. case TableDesign.MediumGrid1Accent6:
  519. val.Value = "MediumGrid1-Accent6";
  520. break;
  521. case TableDesign.MediumGrid2:
  522. val.Value = "MediumGrid2";
  523. break;
  524. case TableDesign.MediumGrid2Accent1:
  525. val.Value = "MediumGrid2-Accent1";
  526. break;
  527. case TableDesign.MediumGrid2Accent2:
  528. val.Value = "MediumGrid2-Accent2";
  529. break;
  530. case TableDesign.MediumGrid2Accent3:
  531. val.Value = "MediumGrid2-Accent3";
  532. break;
  533. case TableDesign.MediumGrid2Accent4:
  534. val.Value = "MediumGrid2-Accent4";
  535. break;
  536. case TableDesign.MediumGrid2Accent5:
  537. val.Value = "MediumGrid2-Accent5";
  538. break;
  539. case TableDesign.MediumGrid2Accent6:
  540. val.Value = "MediumGrid2-Accent6";
  541. break;
  542. case TableDesign.MediumGrid3:
  543. val.Value = "MediumGrid3";
  544. break;
  545. case TableDesign.MediumGrid3Accent1:
  546. val.Value = "MediumGrid3-Accent1";
  547. break;
  548. case TableDesign.MediumGrid3Accent2:
  549. val.Value = "MediumGrid3-Accent2";
  550. break;
  551. case TableDesign.MediumGrid3Accent3:
  552. val.Value = "MediumGrid3-Accent3";
  553. break;
  554. case TableDesign.MediumGrid3Accent4:
  555. val.Value = "MediumGrid3-Accent4";
  556. break;
  557. case TableDesign.MediumGrid3Accent5:
  558. val.Value = "MediumGrid3-Accent5";
  559. break;
  560. case TableDesign.MediumGrid3Accent6:
  561. val.Value = "MediumGrid3-Accent6";
  562. break;
  563. case TableDesign.DarkList:
  564. val.Value = "DarkList";
  565. break;
  566. case TableDesign.DarkListAccent1:
  567. val.Value = "DarkList-Accent1";
  568. break;
  569. case TableDesign.DarkListAccent2:
  570. val.Value = "DarkList-Accent2";
  571. break;
  572. case TableDesign.DarkListAccent3:
  573. val.Value = "DarkList-Accent3";
  574. break;
  575. case TableDesign.DarkListAccent4:
  576. val.Value = "DarkList-Accent4";
  577. break;
  578. case TableDesign.DarkListAccent5:
  579. val.Value = "DarkList-Accent5";
  580. break;
  581. case TableDesign.DarkListAccent6:
  582. val.Value = "DarkList-Accent6";
  583. break;
  584. case TableDesign.ColorfulShading:
  585. val.Value = "ColorfulShading";
  586. break;
  587. case TableDesign.ColorfulShadingAccent1:
  588. val.Value = "ColorfulShading-Accent1";
  589. break;
  590. case TableDesign.ColorfulShadingAccent2:
  591. val.Value = "ColorfulShading-Accent2";
  592. break;
  593. case TableDesign.ColorfulShadingAccent3:
  594. val.Value = "ColorfulShading-Accent3";
  595. break;
  596. case TableDesign.ColorfulShadingAccent4:
  597. val.Value = "ColorfulShading-Accent4";
  598. break;
  599. case TableDesign.ColorfulShadingAccent5:
  600. val.Value = "ColorfulShading-Accent5";
  601. break;
  602. case TableDesign.ColorfulShadingAccent6:
  603. val.Value = "ColorfulShading-Accent6";
  604. break;
  605. case TableDesign.ColorfulList:
  606. val.Value = "ColorfulList";
  607. break;
  608. case TableDesign.ColorfulListAccent1:
  609. val.Value = "ColorfulList-Accent1";
  610. break;
  611. case TableDesign.ColorfulListAccent2:
  612. val.Value = "ColorfulList-Accent2";
  613. break;
  614. case TableDesign.ColorfulListAccent3:
  615. val.Value = "ColorfulList-Accent3";
  616. break;
  617. case TableDesign.ColorfulListAccent4:
  618. val.Value = "ColorfulList-Accent4";
  619. break;
  620. case TableDesign.ColorfulListAccent5:
  621. val.Value = "ColorfulList-Accent5";
  622. break;
  623. case TableDesign.ColorfulListAccent6:
  624. val.Value = "ColorfulList-Accent6";
  625. break;
  626. case TableDesign.ColorfulGrid:
  627. val.Value = "ColorfulGrid";
  628. break;
  629. case TableDesign.ColorfulGridAccent1:
  630. val.Value = "ColorfulGrid-Accent1";
  631. break;
  632. case TableDesign.ColorfulGridAccent2:
  633. val.Value = "ColorfulGrid-Accent2";
  634. break;
  635. case TableDesign.ColorfulGridAccent3:
  636. val.Value = "ColorfulGrid-Accent3";
  637. break;
  638. case TableDesign.ColorfulGridAccent4:
  639. val.Value = "ColorfulGrid-Accent4";
  640. break;
  641. case TableDesign.ColorfulGridAccent5:
  642. val.Value = "ColorfulGrid-Accent5";
  643. break;
  644. case TableDesign.ColorfulGridAccent6:
  645. val.Value = "ColorfulGrid-Accent6";
  646. break;
  647. default:
  648. break;
  649. }
  650. }
  651. if( Document._styles == null )
  652. {
  653. PackagePart word_styles = Document._package.GetPart( new Uri( "/word/styles.xml", UriKind.Relative ) );
  654. using( TextReader tr = new StreamReader( word_styles.GetStream() ) )
  655. Document._styles = XDocument.Load( tr );
  656. }
  657. var tableStyle =
  658. (
  659. from e in Document._styles.Descendants()
  660. let styleId = e.Attribute( XName.Get( "styleId", DocX.w.NamespaceName ) )
  661. where ( styleId != null && styleId.Value == val.Value )
  662. select e
  663. ).FirstOrDefault();
  664. if( tableStyle == null )
  665. {
  666. XDocument external_style_doc = HelperFunctions.DecompressXMLResource( "Xceed.Words.NET.Resources.styles.xml.gz" );
  667. var styleElement =
  668. (
  669. from e in external_style_doc.Descendants()
  670. let styleId = e.Attribute( XName.Get( "styleId", DocX.w.NamespaceName ) )
  671. where ( styleId != null && styleId.Value == val.Value )
  672. select e
  673. ).FirstOrDefault();
  674. if( styleElement != null )
  675. Document._styles.Element( XName.Get( "styles", DocX.w.NamespaceName ) ).Add( styleElement );
  676. }
  677. }
  678. }
  679. /// <summary>
  680. /// Returns the index of this Table.
  681. /// </summary>
  682. /// <example>
  683. /// Replace the first table in this document with a new Table.
  684. /// <code>
  685. /// // Load a document into memory.
  686. /// using (DocX document = DocX.Load(@"Test.docx"))
  687. /// {
  688. /// // Get the first Table in this document.
  689. /// Table t = document.Tables[0];
  690. ///
  691. /// // Get the character index of Table t in this document.
  692. /// int index = t.Index;
  693. ///
  694. /// // Remove Table t.
  695. /// t.Remove();
  696. ///
  697. /// // Insert a new Table at the original index of Table t.
  698. /// Table newTable = document.InsertTable(index, 4, 4);
  699. ///
  700. /// // Set the design of this new Table, so that we can see it.
  701. /// newTable.Design = TableDesign.LightShadingAccent1;
  702. ///
  703. /// // Save all changes made to the document.
  704. /// document.Save();
  705. /// } // Release this document from memory.
  706. /// </code>
  707. /// </example>
  708. public int Index
  709. {
  710. get
  711. {
  712. int index = 0;
  713. IEnumerable<XElement> previous = Xml.ElementsBeforeSelf();
  714. foreach( XElement e in previous )
  715. index += Paragraph.GetElementTextLength( e );
  716. return index;
  717. }
  718. }
  719. /// <summary>
  720. /// The custom design/style to apply to this table.
  721. /// </summary>
  722. public string CustomTableDesignName
  723. {
  724. get
  725. {
  726. return _customTableDesignName;
  727. }
  728. set
  729. {
  730. _customTableDesignName = value;
  731. this.Design = TableDesign.Custom;
  732. }
  733. }
  734. /// <summary>
  735. /// Gets or sets the value of the Table Caption (Alternate Text Title) of this table.
  736. /// </summary>
  737. public string TableCaption
  738. {
  739. get
  740. {
  741. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  742. var caption = tblPr?.Element( XName.Get( "tblCaption", DocX.w.NamespaceName ) );
  743. if( caption != null )
  744. return caption.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ) );
  745. return null;
  746. }
  747. set
  748. {
  749. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  750. if( tblPr != null )
  751. {
  752. var caption = tblPr.Descendants( XName.Get( "tblCaption", DocX.w.NamespaceName ) ).FirstOrDefault();
  753. if( caption != null )
  754. {
  755. caption.Remove();
  756. }
  757. caption = new XElement( XName.Get( "tblCaption", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), value ) );
  758. tblPr.Add( caption );
  759. }
  760. }
  761. }
  762. /// <summary>
  763. /// Gets or sets the value of the Table Description (Alternate Text Description) of this table.
  764. /// </summary>
  765. public string TableDescription
  766. {
  767. get
  768. {
  769. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  770. var description = tblPr?.Element( XName.Get( "tblDescription", DocX.w.NamespaceName ) );
  771. if( description != null )
  772. return description.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ) );
  773. return null;
  774. }
  775. set
  776. {
  777. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  778. if( tblPr != null )
  779. {
  780. var description = tblPr.Descendants( XName.Get( "tblDescription", DocX.w.NamespaceName ) ).FirstOrDefault();
  781. description?.Remove();
  782. description = new XElement( XName.Get( "tblDescription", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), value ) );
  783. tblPr.Add( description );
  784. }
  785. }
  786. }
  787. public TableLook TableLook
  788. {
  789. get;
  790. set;
  791. }
  792. public List<Double> ColumnWidths
  793. {
  794. get
  795. {
  796. var columnWidths = new List<Double>();
  797. // get the table grid property
  798. XElement grid = Xml.Element( XName.Get( "tblGrid", DocX.w.NamespaceName ) );
  799. // get the columns properties
  800. var columns = grid?.Elements( XName.Get( "gridCol", DocX.w.NamespaceName ) );
  801. if( columns == null )
  802. return null;
  803. foreach( var column in columns )
  804. {
  805. string value = column.GetAttribute( XName.Get( "w", DocX.w.NamespaceName ) );
  806. columnWidths.Add( Convert.ToDouble( value, new CultureInfo( "en-US" ) ) );
  807. }
  808. return columnWidths;
  809. }
  810. }
  811. #endregion
  812. #region Constructors
  813. internal Table( DocX document, XElement xml )
  814. : base( document, xml )
  815. {
  816. _autofit = AutoFit.ColumnWidth;
  817. this.Xml = xml;
  818. this.PackagePart = document.PackagePart;
  819. var properties = xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  820. var alignment = properties.Element( XName.Get( "jc", DocX.w.NamespaceName ) );
  821. if( alignment != null )
  822. {
  823. var val = alignment.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  824. if( val != null )
  825. {
  826. _alignment = (Alignment)Enum.Parse( typeof( Alignment ), val.Value );
  827. }
  828. }
  829. var style = properties?.Element( XName.Get( "tblStyle", DocX.w.NamespaceName ) );
  830. if( style != null )
  831. {
  832. var val = style.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  833. if( val != null )
  834. {
  835. String cleanValue = val.Value.Replace( "-", string.Empty );
  836. if( Enum.IsDefined( typeof( TableDesign ), cleanValue ) )
  837. {
  838. _design = ( TableDesign )Enum.Parse( typeof( TableDesign ), cleanValue );
  839. }
  840. else
  841. {
  842. _design = TableDesign.Custom;
  843. }
  844. }
  845. else
  846. {
  847. _design = TableDesign.None;
  848. }
  849. }
  850. else
  851. {
  852. _design = TableDesign.None;
  853. }
  854. var tableLook = properties?.Element( XName.Get( "tblLook", DocX.w.NamespaceName ) );
  855. if( tableLook != null )
  856. {
  857. this.TableLook = new TableLook( tableLook.GetAttribute( XName.Get( "firstRow", DocX.w.NamespaceName ) ) == "1",
  858. tableLook.GetAttribute( XName.Get( "lastRow", DocX.w.NamespaceName ) ) == "1",
  859. tableLook.GetAttribute( XName.Get( "firstColumn", DocX.w.NamespaceName ) ) == "1",
  860. tableLook.GetAttribute( XName.Get( "lastColumn", DocX.w.NamespaceName ) ) == "1",
  861. tableLook.GetAttribute( XName.Get( "noHBand", DocX.w.NamespaceName ) ) == "1",
  862. tableLook.GetAttribute( XName.Get( "noVBand", DocX.w.NamespaceName ) ) == "1" );
  863. }
  864. }
  865. #endregion
  866. #region Public Methods
  867. /// <summary>
  868. /// Merge cells in given column starting with startRow and ending with endRow.
  869. /// </summary>
  870. public void MergeCellsInColumn( int columnIndex, int startRow, int endRow )
  871. {
  872. // Check for valid start and end indexes.
  873. if( columnIndex < 0 || columnIndex >= ColumnCount )
  874. throw new IndexOutOfRangeException();
  875. if( startRow < 0 || endRow <= startRow || endRow >= Rows.Count )
  876. throw new IndexOutOfRangeException();
  877. // Foreach each Cell between startIndex and endIndex inclusive.
  878. foreach( Row row in Rows.Where( ( z, i ) => i > startRow && i <= endRow ) )
  879. {
  880. var c = row.Cells[ columnIndex ];
  881. var tcPr = c.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  882. if( tcPr == null )
  883. {
  884. c.Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  885. tcPr = c.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  886. }
  887. var vMerge = tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
  888. if( vMerge == null )
  889. {
  890. tcPr.SetElementValue( XName.Get( "vMerge", DocX.w.NamespaceName ), string.Empty );
  891. vMerge = tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
  892. }
  893. }
  894. /*
  895. * Get the tcPr (table cell properties) element for the first cell in this merge,
  896. * null will be returned if no such element exists.
  897. */
  898. var startRowCellsCount = this.Rows[ startRow ].Cells.Count;
  899. var start_tcPr = ( columnIndex > startRowCellsCount )
  900. ? this.Rows[ startRow ].Cells[ startRowCellsCount - 1 ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) )
  901. : this.Rows[ startRow ].Cells[ columnIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  902. if( start_tcPr == null )
  903. {
  904. this.Rows[ startRow ].Cells[ columnIndex ].Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  905. start_tcPr = this.Rows[ startRow ].Cells[ columnIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  906. }
  907. /*
  908. * Get the gridSpan element of this row,
  909. * null will be returned if no such element exists.
  910. */
  911. var start_vMerge = start_tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
  912. if( start_vMerge == null )
  913. {
  914. start_tcPr.SetElementValue( XName.Get( "vMerge", DocX.w.NamespaceName ), string.Empty );
  915. start_vMerge = start_tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
  916. }
  917. start_vMerge.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), "restart" );
  918. }
  919. /// <summary>
  920. /// Set the direction of all content in this Table.
  921. /// </summary>
  922. /// <param name="direction">(Left to Right) or (Right to Left)</param>
  923. /// <example>
  924. /// Set the content direction for all content in a table to RightToLeft.
  925. /// <code>
  926. /// // Load a document.
  927. /// using (DocX document = DocX.Load(@"Test.docx"))
  928. /// {
  929. /// // Get the first table in a document.
  930. /// Table table = document.Tables[0];
  931. ///
  932. /// // Set the content direction for all content in this table to RightToLeft.
  933. /// table.SetDirection(Direction.RightToLeft);
  934. ///
  935. /// // Save all changes made to this document.
  936. /// document.Save();
  937. /// }
  938. /// </code>
  939. /// </example>
  940. public void SetDirection( Direction direction )
  941. {
  942. var tblPr = GetOrCreate_tblPr();
  943. tblPr.Add( new XElement( DocX.w + "bidiVisual" ) );
  944. foreach( Row r in Rows )
  945. {
  946. r.SetDirection( direction );
  947. }
  948. }
  949. /// <summary>
  950. /// Remove this Table from this document.
  951. /// </summary>
  952. /// <example>
  953. /// Remove the first Table from this document.
  954. /// <code>
  955. /// // Load a document into memory.
  956. /// using (DocX document = DocX.Load(@"Test.docx"))
  957. /// {
  958. /// // Get the first Table in this document.
  959. /// Table t = d.Tables[0];
  960. ///
  961. /// // Remove this Table.
  962. /// t.Remove();
  963. ///
  964. /// // Save all changes made to the document.
  965. /// document.Save();
  966. /// } // Release this document from memory.
  967. /// </code>
  968. /// </example>
  969. public void Remove()
  970. {
  971. this.Xml.Remove();
  972. }
  973. /// <summary>
  974. /// Insert a row at the end of this table.
  975. /// </summary>
  976. /// <example>
  977. /// <code>
  978. /// // Load a document.
  979. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  980. /// {
  981. /// // Get the first table in this document.
  982. /// Table table = document.Tables[0];
  983. ///
  984. /// // Insert a new row at the end of this table.
  985. /// Row row = table.InsertRow();
  986. ///
  987. /// // Loop through each cell in this new row.
  988. /// foreach (Cell c in row.Cells)
  989. /// {
  990. /// // Set the text of each new cell to "Hello".
  991. /// c.Paragraphs[0].InsertText("Hello", false);
  992. /// }
  993. ///
  994. /// // Save the document to a new file.
  995. /// document.SaveAs(@"C:\Example\Test2.docx");
  996. /// }// Release this document from memory.
  997. /// </code>
  998. /// </example>
  999. /// <returns>A new row.</returns>
  1000. public Row InsertRow()
  1001. {
  1002. return this.InsertRow( this.RowCount );
  1003. }
  1004. /// <summary>
  1005. /// Insert a copy of a row at the end of this table.
  1006. /// </summary>
  1007. /// <returns>A new row.</returns>
  1008. public Row InsertRow( Row row, bool keepFormatting = false )
  1009. {
  1010. return this.InsertRow( row, this.RowCount, keepFormatting );
  1011. }
  1012. /// <summary>
  1013. /// Insert a column to the right of a Table.
  1014. /// </summary>
  1015. /// <example>
  1016. /// <code>
  1017. /// // Load a document.
  1018. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1019. /// {
  1020. /// // Get the first Table in this document.
  1021. /// Table table = document.Tables[0];
  1022. ///
  1023. /// // Insert a new column to this right of this table.
  1024. /// table.InsertColumn();
  1025. ///
  1026. /// // Set the new columns text to "Row no."
  1027. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  1028. ///
  1029. /// // Loop through each row in the table.
  1030. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  1031. /// {
  1032. /// // The current row.
  1033. /// Row row = table.Rows[i];
  1034. ///
  1035. /// // The cell in this row that belongs to the new column.
  1036. /// Cell cell = row.Cells[table.ColumnCount - 1];
  1037. ///
  1038. /// // The first Paragraph that this cell houses.
  1039. /// Paragraph p = cell.Paragraphs[0];
  1040. ///
  1041. /// // Insert this rows index.
  1042. /// p.InsertText(i.ToString(), false);
  1043. /// }
  1044. ///
  1045. /// document.Save();
  1046. /// }// Release this document from memory.
  1047. /// </code>
  1048. /// </example>
  1049. public void InsertColumn()
  1050. {
  1051. this.InsertColumn( this.ColumnCount, true );
  1052. }
  1053. /// <summary>
  1054. /// Remove the last row from this Table.
  1055. /// </summary>
  1056. /// <example>
  1057. /// Remove the last row from a Table.
  1058. /// <code>
  1059. /// // Load a document.
  1060. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1061. /// {
  1062. /// // Get the first table in this document.
  1063. /// Table table = document.Tables[0];
  1064. ///
  1065. /// // Remove the last row from this table.
  1066. /// table.RemoveRow();
  1067. ///
  1068. /// // Save the document.
  1069. /// document.Save();
  1070. /// }// Release this document from memory.
  1071. /// </code>
  1072. /// </example>
  1073. public void RemoveRow()
  1074. {
  1075. this.RemoveRow( RowCount - 1 );
  1076. }
  1077. /// <summary>
  1078. /// Remove a row from this Table.
  1079. /// </summary>
  1080. /// <param name="index">The row to remove.</param>
  1081. /// <example>
  1082. /// Remove the first row from a Table.
  1083. /// <code>
  1084. /// // Load a document.
  1085. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1086. /// {
  1087. /// // Get the first table in this document.
  1088. /// Table table = document.Tables[0];
  1089. ///
  1090. /// // Remove the first row from this table.
  1091. /// table.RemoveRow(0);
  1092. ///
  1093. /// // Save the document.
  1094. /// document.Save();
  1095. /// }// Release this document from memory.
  1096. /// </code>
  1097. /// </example>
  1098. public void RemoveRow( int index )
  1099. {
  1100. if( index < 0 || index > RowCount - 1 )
  1101. throw new IndexOutOfRangeException();
  1102. this.Rows[ index ].Xml.Remove();
  1103. if( this.Rows.Count == 0 )
  1104. {
  1105. this.Remove();
  1106. }
  1107. }
  1108. /// <summary>
  1109. /// Remove the last column for this Table.
  1110. /// </summary>
  1111. /// <example>
  1112. /// Remove the last column from a Table.
  1113. /// <code>
  1114. /// // Load a document.
  1115. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1116. /// {
  1117. /// // Get the first table in this document.
  1118. /// Table table = document.Tables[0];
  1119. ///
  1120. /// // Remove the last column from this table.
  1121. /// table.RemoveColumn();
  1122. ///
  1123. /// // Save the document.
  1124. /// document.Save();
  1125. /// }// Release this document from memory.
  1126. /// </code>
  1127. /// </example>
  1128. public void RemoveColumn()
  1129. {
  1130. this.RemoveColumn( this.ColumnCount - 1 );
  1131. }
  1132. /// <summary>
  1133. /// Remove a column from this Table.
  1134. /// </summary>
  1135. /// <param name="index">The column to remove.</param>
  1136. /// <example>
  1137. /// Remove the first column from a Table.
  1138. /// <code>
  1139. /// // Load a document.
  1140. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1141. /// {
  1142. /// // Get the first table in this document.
  1143. /// Table table = document.Tables[0];
  1144. ///
  1145. /// // Remove the first column from this table.
  1146. /// table.RemoveColumn(0);
  1147. ///
  1148. /// // Save the document.
  1149. /// document.Save();
  1150. /// }// Release this document from memory.
  1151. /// </code>
  1152. /// </example>
  1153. public void RemoveColumn( int index )
  1154. {
  1155. if( ( index < 0 ) || ( index > this.ColumnCount - 1 ) )
  1156. throw new IndexOutOfRangeException();
  1157. foreach( Row r in Rows )
  1158. {
  1159. if( r.Cells.Count < this.ColumnCount )
  1160. {
  1161. int gridAfterValue = r.GridAfter;
  1162. int posIndex = 0;
  1163. int currentPos = 0;
  1164. for( int i = 0; i < r.Cells.Count; ++i )
  1165. {
  1166. var rowCell = r.Cells[ i ];
  1167. int gridSpanValue = ( rowCell.GridSpan != 0 ) ? rowCell.GridSpan - 1 : 0;
  1168. // checks to see if index is between the lowest and highest cell value.
  1169. if( ( ( index - gridAfterValue ) >= currentPos )
  1170. && ( ( index - gridAfterValue ) <= ( currentPos + gridSpanValue ) ) )
  1171. {
  1172. r.Cells[ posIndex ].Xml.Remove();
  1173. break;
  1174. }
  1175. ++posIndex;
  1176. currentPos += ( gridSpanValue + 1 );
  1177. }
  1178. }
  1179. else
  1180. {
  1181. r.Cells[ index ].Xml.Remove();
  1182. }
  1183. }
  1184. _cachedColumnCount = -1;
  1185. }
  1186. /// <summary>
  1187. /// Insert a row into this table.
  1188. /// </summary>
  1189. /// <example>
  1190. /// <code>
  1191. /// // Load a document.
  1192. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1193. /// {
  1194. /// // Get the first table in this document.
  1195. /// Table table = document.Tables[0];
  1196. ///
  1197. /// // Insert a new row at index 1 in this table.
  1198. /// Row row = table.InsertRow(1);
  1199. ///
  1200. /// // Loop through each cell in this new row.
  1201. /// foreach (Cell c in row.Cells)
  1202. /// {
  1203. /// // Set the text of each new cell to "Hello".
  1204. /// c.Paragraphs[0].InsertText("Hello", false);
  1205. /// }
  1206. ///
  1207. /// // Save the document to a new file.
  1208. /// document.SaveAs(@"C:\Example\Test2.docx");
  1209. /// }// Release this document from memory.
  1210. /// </code>
  1211. /// </example>
  1212. /// <param name="index">Index to insert row at.</param>
  1213. /// <returns>A new Row</returns>
  1214. public Row InsertRow( int index )
  1215. {
  1216. if( ( index < 0 ) || ( index > this.RowCount ) )
  1217. throw new IndexOutOfRangeException();
  1218. var content = new List<XElement>();
  1219. for( int i = 0; i < ColumnCount; i++ )
  1220. {
  1221. var cell = ( ( _columnWidths != null ) && ( _columnWidths.Length > i ) )
  1222. ? HelperFunctions.CreateTableCell( _columnWidths[ i ] * 20 )
  1223. : HelperFunctions.CreateTableCell();
  1224. content.Add( cell );
  1225. }
  1226. return this.InsertRow( content, index );
  1227. }
  1228. /// <summary>
  1229. /// Insert a copy of a row into this table.
  1230. /// </summary>
  1231. /// <param name="row">Row to copy and insert.</param>
  1232. /// <param name="index">Index to insert row at.</param>
  1233. /// <param name="keepFormatting">True to clone everithing, False to clone cell structure only.</param>
  1234. /// <returns>A new Row</returns>
  1235. public Row InsertRow( Row row, int index, bool keepFormatting = false )
  1236. {
  1237. if( row == null )
  1238. throw new ArgumentNullException( "row" );
  1239. if( index < 0 || index > RowCount )
  1240. throw new IndexOutOfRangeException();
  1241. List<XElement> content;
  1242. if( keepFormatting )
  1243. content = row.Xml.Elements().Select( element => HelperFunctions.CloneElement( element ) ).ToList();
  1244. else
  1245. content = row.Xml.Elements( XName.Get( "tc", DocX.w.NamespaceName ) ).Select( element => HelperFunctions.CloneElement( element ) ).ToList();
  1246. return InsertRow( content, index );
  1247. }
  1248. /// <summary>
  1249. /// Insert a column into a table.
  1250. /// </summary>
  1251. /// <param name="index">The index to insert the column at.</param>
  1252. /// <param name="direction">The side in which you wish to place the colum : True for right, false for left.</param>
  1253. /// <example>
  1254. /// Insert a column to the left of a table.
  1255. /// <code>
  1256. /// // Load a document.
  1257. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1258. /// {
  1259. /// // Get the first Table in this document.
  1260. /// Table table = document.Tables[0];
  1261. ///
  1262. /// // Insert a new column to this left of this table.
  1263. /// table.InsertColumn(0, false);
  1264. ///
  1265. /// // Set the new columns text to "Row no."
  1266. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  1267. ///
  1268. /// // Loop through each row in the table.
  1269. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  1270. /// {
  1271. /// // The current row.
  1272. /// Row row = table.Rows[i];
  1273. ///
  1274. /// // The cell in this row that belongs to the new column.
  1275. /// Cell cell = row.Cells[table.ColumnCount - 1];
  1276. ///
  1277. /// // The first Paragraph that this cell houses.
  1278. /// Paragraph p = cell.Paragraphs[0];
  1279. ///
  1280. /// // Insert this rows index.
  1281. /// p.InsertText(i.ToString(), false);
  1282. /// }
  1283. ///
  1284. /// document.Save();
  1285. /// }// Release this document from memory.
  1286. /// </code>
  1287. /// </example>
  1288. public void InsertColumn( int index, bool direction )
  1289. {
  1290. var colCount = this.ColumnCount;
  1291. if( (index <= 0) && (index > colCount ) )
  1292. throw new NullReferenceException( "index should be greater than 0 and smaller or equals to this.ColumnCount." );
  1293. if( this.RowCount > 0 )
  1294. {
  1295. _cachedColumnCount = -1;
  1296. foreach( Row r in this.Rows )
  1297. {
  1298. // create cell
  1299. var cell = HelperFunctions.CreateTableCell();
  1300. // insert cell
  1301. if( r.Cells.Count < colCount )
  1302. {
  1303. if( index >= colCount )
  1304. {
  1305. this.AddCellToRow( r, cell, r.Cells.Count - 1, direction );
  1306. }
  1307. else
  1308. {
  1309. int gridAfterValue = r.GridAfter;
  1310. int currentPosition = 1;
  1311. int posIndex = 1;
  1312. foreach( var rowCell in r.Cells )
  1313. {
  1314. int gridSpanValue = ( rowCell.GridSpan != 0 ) ? rowCell.GridSpan - 1 : 0;
  1315. var tcPr = rowCell.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  1316. // Check if the cell have a gridSpan and if the index is between the lowest and highest cell value
  1317. if( ( ( index - gridAfterValue ) >= currentPosition )
  1318. && ( ( index - gridAfterValue ) <= ( currentPosition + gridSpanValue ) ) )
  1319. {
  1320. var dir = ( direction && ( index == ( currentPosition + gridSpanValue ) ) );
  1321. this.AddCellToRow( r, cell, posIndex - 1, dir );
  1322. break;
  1323. }
  1324. ++posIndex;
  1325. currentPosition += (gridSpanValue + 1);
  1326. }
  1327. }
  1328. }
  1329. else
  1330. {
  1331. this.AddCellToRow( r, cell, index - 1, direction );
  1332. }
  1333. }
  1334. }
  1335. }
  1336. /// <summary>
  1337. /// Insert a page break before a Table.
  1338. /// </summary>
  1339. /// <example>
  1340. /// Insert a Table and a Paragraph into a document with a page break between them.
  1341. /// <code>
  1342. /// // Create a new document.
  1343. /// using (DocX document = DocX.Create(@"Test.docx"))
  1344. /// {
  1345. /// // Insert a new Paragraph.
  1346. /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
  1347. ///
  1348. /// // Insert a new Table.
  1349. /// Table t1 = document.InsertTable(2, 2);
  1350. /// t1.Design = TableDesign.LightShadingAccent1;
  1351. ///
  1352. /// // Insert a page break before this Table.
  1353. /// t1.InsertPageBreakBeforeSelf();
  1354. ///
  1355. /// // Save this document.
  1356. /// document.Save();
  1357. /// }// Release this document from memory.
  1358. /// </code>
  1359. /// </example>
  1360. public override void InsertPageBreakBeforeSelf()
  1361. {
  1362. base.InsertPageBreakBeforeSelf();
  1363. }
  1364. public void SetWidths( float[] widths )
  1365. {
  1366. _columnWidths = widths;
  1367. for( int i = 0; i < this.Rows.Count; ++i )
  1368. {
  1369. var row = this.Rows[ i ];
  1370. for( int j = 0; j < widths.Length; ++j )
  1371. {
  1372. if( row.Cells.Count > j )
  1373. {
  1374. row.Cells[ j ].Width = widths[ j ];
  1375. }
  1376. }
  1377. }
  1378. }
  1379. public void SetWidthsPercentage( float[] widthsPercentage, float? totalWidth )
  1380. {
  1381. if( totalWidth == null )
  1382. totalWidth = this.Document.PageWidth - this.Document.MarginLeft - this.Document.MarginRight;
  1383. List<float> widths = new List<float>( widthsPercentage.Length );
  1384. widthsPercentage.ToList().ForEach( pWidth => { widths.Add( ( pWidth * totalWidth.Value / 100 ) * ( 96 / 72 ) ); } );
  1385. SetWidths( widths.ToArray() );
  1386. }
  1387. /// <summary>
  1388. /// Insert a page break after a Table.
  1389. /// </summary>
  1390. /// <example>
  1391. /// Insert a Table and a Paragraph into a document with a page break between them.
  1392. /// <code>
  1393. /// // Create a new document.
  1394. /// using (DocX document = DocX.Create(@"Test.docx"))
  1395. /// {
  1396. /// // Insert a new Table.
  1397. /// Table t1 = document.InsertTable(2, 2);
  1398. /// t1.Design = TableDesign.LightShadingAccent1;
  1399. ///
  1400. /// // Insert a page break after this Table.
  1401. /// t1.InsertPageBreakAfterSelf();
  1402. ///
  1403. /// // Insert a new Paragraph.
  1404. /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
  1405. ///
  1406. /// // Save this document.
  1407. /// document.Save();
  1408. /// }// Release this document from memory.
  1409. /// </code>
  1410. /// </example>
  1411. public override void InsertPageBreakAfterSelf()
  1412. {
  1413. base.InsertPageBreakAfterSelf();
  1414. }
  1415. /// <summary>
  1416. /// Insert a new Table before this Table, this Table can be from this document or another document.
  1417. /// </summary>
  1418. /// <param name="t">The Table t to be inserted</param>
  1419. /// <returns>A new Table inserted before this Table.</returns>
  1420. /// <example>
  1421. /// Insert a new Table before this Table.
  1422. /// <code>
  1423. /// // Place holder for a Table.
  1424. /// Table t;
  1425. ///
  1426. /// // Load document a.
  1427. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1428. /// {
  1429. /// // Get the first Table from this document.
  1430. /// t = documentA.Tables[0];
  1431. /// }
  1432. ///
  1433. /// // Load document b.
  1434. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1435. /// {
  1436. /// // Get the first Table in document b.
  1437. /// Table t2 = documentB.Tables[0];
  1438. ///
  1439. /// // Insert the Table from document a before this Table.
  1440. /// Table newTable = t2.InsertTableBeforeSelf(t);
  1441. ///
  1442. /// // Save all changes made to document b.
  1443. /// documentB.Save();
  1444. /// }// Release this document from memory.
  1445. /// </code>
  1446. /// </example>
  1447. public override Table InsertTableBeforeSelf( Table t )
  1448. {
  1449. return base.InsertTableBeforeSelf( t );
  1450. }
  1451. /// <summary>
  1452. /// Insert a new Table into this document before this Table.
  1453. /// </summary>
  1454. /// <param name="rowCount">The number of rows this Table should have.</param>
  1455. /// <param name="columnCount">The number of columns this Table should have.</param>
  1456. /// <returns>A new Table inserted before this Table.</returns>
  1457. /// <example>
  1458. /// <code>
  1459. /// // Create a new document.
  1460. /// using (DocX document = DocX.Create(@"Test.docx"))
  1461. /// {
  1462. /// //Insert a Table into this document.
  1463. /// Table t = document.InsertTable(2, 2);
  1464. /// t.Design = TableDesign.LightShadingAccent1;
  1465. /// t.Alignment = Alignment.center;
  1466. ///
  1467. /// // Insert a new Table before this Table.
  1468. /// Table newTable = t.InsertTableBeforeSelf(2, 2);
  1469. /// newTable.Design = TableDesign.LightShadingAccent2;
  1470. /// newTable.Alignment = Alignment.center;
  1471. ///
  1472. /// // Save all changes made to this document.
  1473. /// document.Save();
  1474. /// }// Release this document from memory.
  1475. /// </code>
  1476. /// </example>
  1477. public override Table InsertTableBeforeSelf( int rowCount, int columnCount )
  1478. {
  1479. return base.InsertTableBeforeSelf( rowCount, columnCount );
  1480. }
  1481. /// <summary>
  1482. /// Insert a new Table after this Table, this Table can be from this document or another document.
  1483. /// </summary>
  1484. /// <param name="t">The Table t to be inserted</param>
  1485. /// <returns>A new Table inserted after this Table.</returns>
  1486. /// <example>
  1487. /// Insert a new Table after this Table.
  1488. /// <code>
  1489. /// // Place holder for a Table.
  1490. /// Table t;
  1491. ///
  1492. /// // Load document a.
  1493. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1494. /// {
  1495. /// // Get the first Table from this document.
  1496. /// t = documentA.Tables[0];
  1497. /// }
  1498. ///
  1499. /// // Load document b.
  1500. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1501. /// {
  1502. /// // Get the first Table in document b.
  1503. /// Table t2 = documentB.Tables[0];
  1504. ///
  1505. /// // Insert the Table from document a after this Table.
  1506. /// Table newTable = t2.InsertTableAfterSelf(t);
  1507. ///
  1508. /// // Save all changes made to document b.
  1509. /// documentB.Save();
  1510. /// }// Release this document from memory.
  1511. /// </code>
  1512. /// </example>
  1513. public override Table InsertTableAfterSelf( Table t )
  1514. {
  1515. return base.InsertTableAfterSelf( t );
  1516. }
  1517. /// <summary>
  1518. /// Insert a new Table into this document after this Table.
  1519. /// </summary>
  1520. /// <param name="rowCount">The number of rows this Table should have.</param>
  1521. /// <param name="columnCount">The number of columns this Table should have.</param>
  1522. /// <returns>A new Table inserted before this Table.</returns>
  1523. /// <example>
  1524. /// <code>
  1525. /// // Create a new document.
  1526. /// using (DocX document = DocX.Create(@"Test.docx"))
  1527. /// {
  1528. /// //Insert a Table into this document.
  1529. /// Table t = document.InsertTable(2, 2);
  1530. /// t.Design = TableDesign.LightShadingAccent1;
  1531. /// t.Alignment = Alignment.center;
  1532. ///
  1533. /// // Insert a new Table after this Table.
  1534. /// Table newTable = t.InsertTableAfterSelf(2, 2);
  1535. /// newTable.Design = TableDesign.LightShadingAccent2;
  1536. /// newTable.Alignment = Alignment.center;
  1537. ///
  1538. /// // Save all changes made to this document.
  1539. /// document.Save();
  1540. /// }// Release this document from memory.
  1541. /// </code>
  1542. /// </example>
  1543. public override Table InsertTableAfterSelf( int rowCount, int columnCount )
  1544. {
  1545. return base.InsertTableAfterSelf( rowCount, columnCount );
  1546. }
  1547. /// <summary>
  1548. /// Insert a Paragraph before this Table, this Paragraph may have come from the same or another document.
  1549. /// </summary>
  1550. /// <param name="p">The Paragraph to insert.</param>
  1551. /// <returns>The Paragraph now associated with this document.</returns>
  1552. /// <example>
  1553. /// Take a Paragraph from document a, and insert it into document b before this Table.
  1554. /// <code>
  1555. /// // Place holder for a Paragraph.
  1556. /// Paragraph p;
  1557. ///
  1558. /// // Load document a.
  1559. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1560. /// {
  1561. /// // Get the first paragraph from this document.
  1562. /// p = documentA.Paragraphs[0];
  1563. /// }
  1564. ///
  1565. /// // Load document b.
  1566. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1567. /// {
  1568. /// // Get the first Table in document b.
  1569. /// Table t = documentB.Tables[0];
  1570. ///
  1571. /// // Insert the Paragraph from document a before this Table.
  1572. /// Paragraph newParagraph = t.InsertParagraphBeforeSelf(p);
  1573. ///
  1574. /// // Save all changes made to document b.
  1575. /// documentB.Save();
  1576. /// }// Release this document from memory.
  1577. /// </code>
  1578. /// </example>
  1579. public override Paragraph InsertParagraphBeforeSelf( Paragraph p )
  1580. {
  1581. return base.InsertParagraphBeforeSelf( p );
  1582. }
  1583. /// <summary>
  1584. /// Insert a new Paragraph before this Table.
  1585. /// </summary>
  1586. /// <param name="text">The initial text for this new Paragraph.</param>
  1587. /// <returns>A new Paragraph inserted before this Table.</returns>
  1588. /// <example>
  1589. /// Insert a new Paragraph before the first Table in this document.
  1590. /// <code>
  1591. /// // Create a new document.
  1592. /// using (DocX document = DocX.Create(@"Test.docx"))
  1593. /// {
  1594. /// // Insert a Table into this document.
  1595. /// Table t = document.InsertTable(2, 2);
  1596. ///
  1597. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.");
  1598. ///
  1599. /// // Save all changes made to this new document.
  1600. /// document.Save();
  1601. /// }// Release this new document form memory.
  1602. /// </code>
  1603. /// </example>
  1604. public override Paragraph InsertParagraphBeforeSelf( string text )
  1605. {
  1606. return base.InsertParagraphBeforeSelf( text );
  1607. }
  1608. /// <summary>
  1609. /// Insert a new Paragraph before this Table.
  1610. /// </summary>
  1611. /// <param name="text">The initial text for this new Paragraph.</param>
  1612. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1613. /// <returns>A new Paragraph inserted before this Table.</returns>
  1614. /// <example>
  1615. /// Insert a new paragraph before the first Table in this document.
  1616. /// <code>
  1617. /// // Create a new document.
  1618. /// using (DocX document = DocX.Create(@"Test.docx"))
  1619. /// {
  1620. /// // Insert a Table into this document.
  1621. /// Table t = document.InsertTable(2, 2);
  1622. ///
  1623. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false);
  1624. ///
  1625. /// // Save all changes made to this new document.
  1626. /// document.Save();
  1627. /// }// Release this new document form memory.
  1628. /// </code>
  1629. /// </example>
  1630. public override Paragraph InsertParagraphBeforeSelf( string text, bool trackChanges )
  1631. {
  1632. return base.InsertParagraphBeforeSelf( text, trackChanges );
  1633. }
  1634. /// <summary>
  1635. /// Insert a new Paragraph before this Table.
  1636. /// </summary>
  1637. /// <param name="text">The initial text for this new Paragraph.</param>
  1638. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1639. /// <param name="formatting">The formatting to apply to this insertion.</param>
  1640. /// <returns>A new Paragraph inserted before this Table.</returns>
  1641. /// <example>
  1642. /// Insert a new paragraph before the first Table in this document.
  1643. /// <code>
  1644. /// // Create a new document.
  1645. /// using (DocX document = DocX.Create(@"Test.docx"))
  1646. /// {
  1647. /// // Insert a Table into this document.
  1648. /// Table t = document.InsertTable(2, 2);
  1649. ///
  1650. /// Formatting boldFormatting = new Formatting();
  1651. /// boldFormatting.Bold = true;
  1652. ///
  1653. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting);
  1654. ///
  1655. /// // Save all changes made to this new document.
  1656. /// document.Save();
  1657. /// }// Release this new document form memory.
  1658. /// </code>
  1659. /// </example>
  1660. public override Paragraph InsertParagraphBeforeSelf( string text, bool trackChanges, Formatting formatting )
  1661. {
  1662. return base.InsertParagraphBeforeSelf( text, trackChanges, formatting );
  1663. }
  1664. /// <summary>
  1665. /// Insert a Paragraph after this Table, this Paragraph may have come from the same or another document.
  1666. /// </summary>
  1667. /// <param name="p">The Paragraph to insert.</param>
  1668. /// <returns>The Paragraph now associated with this document.</returns>
  1669. /// <example>
  1670. /// Take a Paragraph from document a, and insert it into document b after this Table.
  1671. /// <code>
  1672. /// // Place holder for a Paragraph.
  1673. /// Paragraph p;
  1674. ///
  1675. /// // Load document a.
  1676. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1677. /// {
  1678. /// // Get the first paragraph from this document.
  1679. /// p = documentA.Paragraphs[0];
  1680. /// }
  1681. ///
  1682. /// // Load document b.
  1683. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1684. /// {
  1685. /// // Get the first Table in document b.
  1686. /// Table t = documentB.Tables[0];
  1687. ///
  1688. /// // Insert the Paragraph from document a after this Table.
  1689. /// Paragraph newParagraph = t.InsertParagraphAfterSelf(p);
  1690. ///
  1691. /// // Save all changes made to document b.
  1692. /// documentB.Save();
  1693. /// }// Release this document from memory.
  1694. /// </code>
  1695. /// </example>
  1696. public override Paragraph InsertParagraphAfterSelf( Paragraph p )
  1697. {
  1698. return base.InsertParagraphAfterSelf( p );
  1699. }
  1700. /// <summary>
  1701. /// Insert a new Paragraph after this Table.
  1702. /// </summary>
  1703. /// <param name="text">The initial text for this new Paragraph.</param>
  1704. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1705. /// <param name="formatting">The formatting to apply to this insertion.</param>
  1706. /// <returns>A new Paragraph inserted after this Table.</returns>
  1707. /// <example>
  1708. /// Insert a new paragraph after the first Table in this document.
  1709. /// <code>
  1710. /// // Create a new document.
  1711. /// using (DocX document = DocX.Create(@"Test.docx"))
  1712. /// {
  1713. /// // Insert a Table into this document.
  1714. /// Table t = document.InsertTable(2, 2);
  1715. ///
  1716. /// Formatting boldFormatting = new Formatting();
  1717. /// boldFormatting.Bold = true;
  1718. ///
  1719. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting);
  1720. ///
  1721. /// // Save all changes made to this new document.
  1722. /// document.Save();
  1723. /// }// Release this new document form memory.
  1724. /// </code>
  1725. /// </example>
  1726. public override Paragraph InsertParagraphAfterSelf( string text, bool trackChanges, Formatting formatting )
  1727. {
  1728. return base.InsertParagraphAfterSelf( text, trackChanges, formatting );
  1729. }
  1730. /// <summary>
  1731. /// Insert a new Paragraph after this Table.
  1732. /// </summary>
  1733. /// <param name="text">The initial text for this new Paragraph.</param>
  1734. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1735. /// <returns>A new Paragraph inserted after this Table.</returns>
  1736. /// <example>
  1737. /// Insert a new paragraph after the first Table in this document.
  1738. /// <code>
  1739. /// // Create a new document.
  1740. /// using (DocX document = DocX.Create(@"Test.docx"))
  1741. /// {
  1742. /// // Insert a Table into this document.
  1743. /// Table t = document.InsertTable(2, 2);
  1744. ///
  1745. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false);
  1746. ///
  1747. /// // Save all changes made to this new document.
  1748. /// document.Save();
  1749. /// }// Release this new document form memory.
  1750. /// </code>
  1751. /// </example>
  1752. public override Paragraph InsertParagraphAfterSelf( string text, bool trackChanges )
  1753. {
  1754. return base.InsertParagraphAfterSelf( text, trackChanges );
  1755. }
  1756. /// <summary>
  1757. /// Insert a new Paragraph after this Table.
  1758. /// </summary>
  1759. /// <param name="text">The initial text for this new Paragraph.</param>
  1760. /// <returns>A new Paragraph inserted after this Table.</returns>
  1761. /// <example>
  1762. /// Insert a new Paragraph after the first Table in this document.
  1763. /// <code>
  1764. /// // Create a new document.
  1765. /// using (DocX document = DocX.Create(@"Test.docx"))
  1766. /// {
  1767. /// // Insert a Table into this document.
  1768. /// Table t = document.InsertTable(2, 2);
  1769. ///
  1770. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.");
  1771. ///
  1772. /// // Save all changes made to this new document.
  1773. /// document.Save();
  1774. /// }// Release this new document form memory.
  1775. /// </code>
  1776. /// </example>
  1777. public override Paragraph InsertParagraphAfterSelf( string text )
  1778. {
  1779. return base.InsertParagraphAfterSelf( text );
  1780. }
  1781. /// <summary>
  1782. /// Set a table border
  1783. /// </summary>
  1784. /// <example>
  1785. /// <code>
  1786. /// // Create a new document.
  1787. ///using (DocX document = DocX.Create("Test.docx"))
  1788. ///{
  1789. /// // Insert a table into this document.
  1790. /// Table t = document.InsertTable(3, 3);
  1791. ///
  1792. /// // Create a large blue border.
  1793. /// Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
  1794. ///
  1795. /// // Set the tables Top, Bottom, Left and Right Borders to b.
  1796. /// t.SetBorder(TableBorderType.Top, b);
  1797. /// t.SetBorder(TableBorderType.Bottom, b);
  1798. /// t.SetBorder(TableBorderType.Left, b);
  1799. /// t.SetBorder(TableBorderType.Right, b);
  1800. ///
  1801. /// // Save the document.
  1802. /// document.Save();
  1803. ///}
  1804. /// </code>
  1805. /// </example>
  1806. /// <param name="borderType">The table border to set</param>
  1807. /// <param name="border">Border object to set the table border</param>
  1808. public void SetBorder( TableBorderType borderType, Border border )
  1809. {
  1810. /*
  1811. * Get the tblPr (table properties) element for this Table,
  1812. * null will be return if no such element exists.
  1813. */
  1814. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  1815. if( tblPr == null )
  1816. {
  1817. this.Xml.SetElementValue( XName.Get( "tblPr", DocX.w.NamespaceName ), string.Empty );
  1818. tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  1819. }
  1820. /*
  1821. * Get the tblBorders (table borders) element for this Table,
  1822. * null will be return if no such element exists.
  1823. */
  1824. var tblBorders = tblPr.Element( XName.Get( "tblBorders", DocX.w.NamespaceName ) );
  1825. if( tblBorders == null )
  1826. {
  1827. tblPr.SetElementValue( XName.Get( "tblBorders", DocX.w.NamespaceName ), string.Empty );
  1828. tblBorders = tblPr.Element( XName.Get( "tblBorders", DocX.w.NamespaceName ) );
  1829. }
  1830. /*
  1831. * Get the 'borderType' (table border) element for this Table,
  1832. * null will be return if no such element exists.
  1833. */
  1834. string tbordertype;
  1835. tbordertype = borderType.ToString();
  1836. // only lower the first char of string (because of insideH and insideV)
  1837. tbordertype = tbordertype.Substring( 0, 1 ).ToLower() + tbordertype.Substring( 1 );
  1838. var tblBorderType = tblBorders.Element( XName.Get( borderType.ToString(), DocX.w.NamespaceName ) );
  1839. if( tblBorderType == null )
  1840. {
  1841. tblBorders.SetElementValue( XName.Get( tbordertype, DocX.w.NamespaceName ), string.Empty );
  1842. tblBorderType = tblBorders.Element( XName.Get( tbordertype, DocX.w.NamespaceName ) );
  1843. }
  1844. // get string value of border style
  1845. var borderstyle = border.Tcbs.ToString().Substring( 5 );
  1846. borderstyle = borderstyle.Substring( 0, 1 ).ToLower() + borderstyle.Substring( 1 );
  1847. // The val attribute is used for the border style
  1848. tblBorderType.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), borderstyle );
  1849. if( border.Tcbs != BorderStyle.Tcbs_nil )
  1850. {
  1851. int size;
  1852. switch( border.Size )
  1853. {
  1854. case BorderSize.one:
  1855. size = 2;
  1856. break;
  1857. case BorderSize.two:
  1858. size = 4;
  1859. break;
  1860. case BorderSize.three:
  1861. size = 6;
  1862. break;
  1863. case BorderSize.four:
  1864. size = 8;
  1865. break;
  1866. case BorderSize.five:
  1867. size = 12;
  1868. break;
  1869. case BorderSize.six:
  1870. size = 18;
  1871. break;
  1872. case BorderSize.seven:
  1873. size = 24;
  1874. break;
  1875. case BorderSize.eight:
  1876. size = 36;
  1877. break;
  1878. case BorderSize.nine:
  1879. size = 48;
  1880. break;
  1881. default:
  1882. size = 2;
  1883. break;
  1884. }
  1885. // The sz attribute is used for the border size
  1886. tblBorderType.SetAttributeValue( XName.Get( "sz", DocX.w.NamespaceName ), ( size ).ToString() );
  1887. // The space attribute is used for the cell spacing (probably '0')
  1888. tblBorderType.SetAttributeValue( XName.Get( "space", DocX.w.NamespaceName ), ( border.Space ).ToString() );
  1889. // The color attribute is used for the border color
  1890. tblBorderType.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), border.Color.ToHex() );
  1891. }
  1892. }
  1893. /// <summary>
  1894. /// Get a table border
  1895. /// </summary>
  1896. /// <param name="borderType">The table border to get</param>
  1897. public Border GetBorder( TableBorderType borderType )
  1898. {
  1899. // instance with default border values
  1900. var b = new Border();
  1901. /*
  1902. * Get the tblPr (table properties) element for this Table,
  1903. * null will be return if no such element exists.
  1904. */
  1905. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  1906. if( tblPr == null )
  1907. {
  1908. // uses default border style
  1909. }
  1910. /*
  1911. * Get the tblBorders (table borders) element for this Table,
  1912. * null will be return if no such element exists.
  1913. */
  1914. var tblBorders = tblPr.Element( XName.Get( "tblBorders", DocX.w.NamespaceName ) );
  1915. if( tblBorders == null )
  1916. {
  1917. // uses default border style
  1918. }
  1919. /*
  1920. * Get the 'borderType' (table border) element for this Table,
  1921. * null will be return if no such element exists.
  1922. */
  1923. string tbordertype = borderType.ToString();
  1924. // only lower the first char of string (because of insideH and insideV)
  1925. tbordertype = tbordertype.Substring( 0, 1 ).ToLower() + tbordertype.Substring( 1 );
  1926. var tblBorderType = tblBorders.Element( XName.Get( tbordertype, DocX.w.NamespaceName ) );
  1927. if( tblBorderType == null )
  1928. {
  1929. // uses default border style
  1930. }
  1931. // The val attribute is used for the border style
  1932. var val = tblBorderType.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  1933. // If val is null, this table contains no border information.
  1934. if( val == null )
  1935. {
  1936. // uses default border style
  1937. }
  1938. else
  1939. {
  1940. try
  1941. {
  1942. var bordertype = "Tcbs_" + val.Value;
  1943. b.Tcbs = ( BorderStyle )Enum.Parse( typeof( BorderStyle ), bordertype );
  1944. }
  1945. catch
  1946. {
  1947. val.Remove();
  1948. // uses default border style
  1949. }
  1950. }
  1951. // The sz attribute is used for the border size
  1952. var sz = tblBorderType.Attribute( XName.Get( "sz", DocX.w.NamespaceName ) );
  1953. // If sz is null, this border contains no size information.
  1954. if( sz == null )
  1955. {
  1956. // uses default border style
  1957. }
  1958. else
  1959. {
  1960. // If sz is not an int, something is wrong with this attributes value, so remove it
  1961. int numerical_size;
  1962. if( !int.TryParse( sz.Value, out numerical_size ) )
  1963. {
  1964. sz.Remove();
  1965. }
  1966. else
  1967. {
  1968. switch( numerical_size )
  1969. {
  1970. case 2:
  1971. b.Size = BorderSize.one;
  1972. break;
  1973. case 4:
  1974. b.Size = BorderSize.two;
  1975. break;
  1976. case 6:
  1977. b.Size = BorderSize.three;
  1978. break;
  1979. case 8:
  1980. b.Size = BorderSize.four;
  1981. break;
  1982. case 12:
  1983. b.Size = BorderSize.five;
  1984. break;
  1985. case 18:
  1986. b.Size = BorderSize.six;
  1987. break;
  1988. case 24:
  1989. b.Size = BorderSize.seven;
  1990. break;
  1991. case 36:
  1992. b.Size = BorderSize.eight;
  1993. break;
  1994. case 48:
  1995. b.Size = BorderSize.nine;
  1996. break;
  1997. default:
  1998. b.Size = BorderSize.one;
  1999. break;
  2000. }
  2001. }
  2002. }
  2003. // The space attribute is used for the border spacing (probably '0')
  2004. var space = tblBorderType.Attribute( XName.Get( "space", DocX.w.NamespaceName ) );
  2005. // If space is null, this border contains no space information.
  2006. if( space == null )
  2007. {
  2008. // uses default border style
  2009. }
  2010. else
  2011. {
  2012. // If space is not an int, something is wrong with this attributes value, so remove it
  2013. int borderspace;
  2014. if( !int.TryParse( space.Value, out borderspace ) )
  2015. {
  2016. space.Remove();
  2017. // uses default border style
  2018. }
  2019. else
  2020. {
  2021. b.Space = borderspace;
  2022. }
  2023. }
  2024. // The color attribute is used for the border color
  2025. var color = tblBorderType.Attribute( XName.Get( "color", DocX.w.NamespaceName ) );
  2026. if( color == null )
  2027. {
  2028. // uses default border style
  2029. }
  2030. else
  2031. {
  2032. // If color is not a Color, something is wrong with this attributes value, so remove it
  2033. try
  2034. {
  2035. b.Color = ColorTranslator.FromHtml( string.Format( "#{0}", color.Value ) );
  2036. }
  2037. catch
  2038. {
  2039. color.Remove();
  2040. // uses default border style
  2041. }
  2042. }
  2043. return b;
  2044. }
  2045. public Double GetColumnWidth( Int32 columnIndex )
  2046. {
  2047. List<Double> columnWidths = ColumnWidths;
  2048. if( columnWidths == null || columnIndex > columnWidths.Count - 1 )
  2049. return Double.NaN;
  2050. return columnWidths[ columnIndex ];
  2051. }
  2052. public void SetColumnWidth( int columnIndex, double width )
  2053. {
  2054. var columnWidths = this.ColumnWidths;
  2055. if( columnWidths == null || ( columnIndex > columnWidths.Count - 1 ) )
  2056. {
  2057. if( this.Rows.Count == 0 )
  2058. throw new Exception( "There is at least one row required to detect the existing columns." );
  2059. columnWidths = new List<Double>();
  2060. foreach( Cell c in Rows[ Rows.Count - 1 ].Cells )
  2061. {
  2062. columnWidths.Add( c.Width );
  2063. }
  2064. }
  2065. // check if the columnIndex is valid
  2066. if( columnIndex > (columnWidths.Count - 1) )
  2067. throw new Exception( "The index is greather than the available table columns." );
  2068. // append a new grid if null
  2069. var grid = Xml.Element( XName.Get( "tblGrid", DocX.w.NamespaceName ) );
  2070. if( grid == null )
  2071. {
  2072. var tblPr = GetOrCreate_tblPr();
  2073. tblPr.AddAfterSelf( new XElement( XName.Get( "tblGrid", DocX.w.NamespaceName ) ) );
  2074. grid = Xml.Element( XName.Get( "tblGrid", DocX.w.NamespaceName ) );
  2075. }
  2076. // remove all existing values
  2077. grid?.RemoveAll();
  2078. // append new column widths
  2079. int index = 0;
  2080. foreach( var columnWidth in columnWidths )
  2081. {
  2082. double newWidth = columnWidth;
  2083. if( index == columnIndex )
  2084. {
  2085. newWidth = width;
  2086. }
  2087. var newColumn = new XElement( XName.Get( "gridCol", DocX.w.NamespaceName ), new XAttribute( XName.Get( "w", DocX.w.NamespaceName ), newWidth ) );
  2088. grid?.Add( newColumn );
  2089. index += 1;
  2090. }
  2091. // remove cell widths
  2092. foreach( Row row in this.Rows )
  2093. {
  2094. foreach( Cell cell in row.Cells )
  2095. {
  2096. cell.Width = -1;
  2097. }
  2098. }
  2099. // set AutoFit to Fixed
  2100. this.AutoFit = AutoFit.Fixed;
  2101. }
  2102. public void SetTableCellMargin( TableCellMarginType type, double margin )
  2103. {
  2104. var tblPr = this.GetOrCreate_tblPr();
  2105. var tblCellMarXName = XName.Get( "tblCellMar", DocX.w.NamespaceName );
  2106. var typeXName = XName.Get( type.ToString(), DocX.w.NamespaceName );
  2107. var tblCellMar = tblPr.Element( tblCellMarXName );
  2108. if( tblCellMar == null )
  2109. {
  2110. tblPr.AddFirst( new XElement( tblCellMarXName ) );
  2111. tblCellMar = tblPr.Element( tblCellMarXName );
  2112. }
  2113. var side = tblCellMar.Element( typeXName );
  2114. if( side == null )
  2115. {
  2116. tblCellMar.AddFirst( new XElement( typeXName ) );
  2117. side = tblCellMar.Element( typeXName );
  2118. }
  2119. side.RemoveAttributes();
  2120. // Set value and side for cell Margin
  2121. side.Add( new XAttribute( XName.Get( "w", DocX.w.NamespaceName ), margin ) );
  2122. side.Add( new XAttribute( XName.Get( "type", DocX.w.NamespaceName ), "dxa" ) );
  2123. }
  2124. /// <summary>
  2125. /// Deletes a cell in a row and shift the others to the left.
  2126. /// </summary>
  2127. /// <param name="rowIndex">index of the row where a cell will be removed.</param>
  2128. /// <param name="celIndex">index of the cell to remove in the row.</param>
  2129. public void DeleteAndShiftCellsLeft( int rowIndex, int celIndex )
  2130. {
  2131. var trPr = this.Rows[ rowIndex ].Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2132. if( trPr != null )
  2133. {
  2134. var gridAfter = trPr.Element( XName.Get( "gridAfter", DocX.w.NamespaceName ) );
  2135. if( gridAfter != null )
  2136. {
  2137. var gridAfterValAttr = gridAfter.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  2138. gridAfterValAttr.Value = ( gridAfterValAttr != null ) ? int.Parse( gridAfterValAttr.Value ).ToString() : "1";
  2139. }
  2140. else
  2141. {
  2142. gridAfter.SetAttributeValue( "val", 1 );
  2143. }
  2144. }
  2145. else
  2146. {
  2147. var gridAfterXElement = new XElement( XName.Get( "gridAfter", DocX.w.NamespaceName ) );
  2148. var valXAttribute = new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), 1 );
  2149. gridAfterXElement.Add( valXAttribute );
  2150. var trPrXElement = new XElement( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2151. trPrXElement.Add( gridAfterXElement );
  2152. this.Rows[ rowIndex ].Xml.AddFirst( trPrXElement );
  2153. }
  2154. if( ( celIndex <= this.ColumnCount ) && ( this.Rows[ rowIndex ].ColumnCount <= this.ColumnCount ) )
  2155. {
  2156. this.Rows[ rowIndex ].Cells[ celIndex ].Xml.Remove();
  2157. }
  2158. }
  2159. #endregion
  2160. #region Internal Methods
  2161. /// <summary>
  2162. /// If the tblPr element doesent exist it is created, either way it is returned by this function.
  2163. /// </summary>
  2164. /// <returns>The tblPr element for this Table.</returns>
  2165. internal XElement GetOrCreate_tblPr()
  2166. {
  2167. // Get the element.
  2168. var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  2169. // If it dosen't exist, create it.
  2170. if( tblPr == null )
  2171. {
  2172. this.Xml.AddFirst( new XElement( XName.Get( "tblPr", DocX.w.NamespaceName ) ) );
  2173. tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
  2174. }
  2175. // Return the pPr element for this Paragraph.
  2176. return tblPr;
  2177. }
  2178. #endregion
  2179. #region Private Methods
  2180. private Row InsertRow( List<XElement> content, Int32 index )
  2181. {
  2182. Row newRow = new Row( this, Document, new XElement( XName.Get( "tr", DocX.w.NamespaceName ), content ) );
  2183. XElement rowXml;
  2184. if( index == Rows.Count )
  2185. {
  2186. rowXml = Rows.Last().Xml;
  2187. rowXml.AddAfterSelf( newRow.Xml );
  2188. }
  2189. else
  2190. {
  2191. rowXml = Rows[ index ].Xml;
  2192. rowXml.AddBeforeSelf( newRow.Xml );
  2193. }
  2194. return newRow;
  2195. }
  2196. private void AddCellToRow( Row row, XElement cell, int index, bool direction )
  2197. {
  2198. if( index >= row.Cells.Count )
  2199. throw new IndexOutOfRangeException( "index is greater or equals to row.Cells.Count." );
  2200. if( direction )
  2201. {
  2202. row.Cells[ index ].Xml.AddAfterSelf( cell );
  2203. }
  2204. else
  2205. {
  2206. row.Cells[ index ].Xml.AddBeforeSelf( cell );
  2207. }
  2208. }
  2209. #endregion
  2210. }
  2211. /// <summary>
  2212. /// Represents a single row in a Table.
  2213. /// </summary>
  2214. public class Row : Container
  2215. {
  2216. #region Internal Members
  2217. internal Table _table;
  2218. #endregion
  2219. #region Public Properties
  2220. /// <summary>
  2221. /// Calculates columns count in the row, taking spanned cells into account
  2222. /// </summary>
  2223. public Int32 ColumnCount
  2224. {
  2225. get
  2226. {
  2227. int gridSpanSum = this.GridAfter;
  2228. // Foreach each Cell between startIndex and endIndex inclusive.
  2229. foreach( Cell c in Cells )
  2230. {
  2231. if( c.GridSpan != 0 )
  2232. {
  2233. gridSpanSum += (c.GridSpan - 1);
  2234. }
  2235. }
  2236. // return cells count + count of spanned cells
  2237. return Cells.Count + gridSpanSum;
  2238. }
  2239. }
  2240. /// <summary>
  2241. /// Returns the row.GridAfter => The number of deleted cells in a row.
  2242. /// </summary>
  2243. public int GridAfter
  2244. {
  2245. get
  2246. {
  2247. var trPr = this.Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2248. if( trPr != null )
  2249. {
  2250. var gridAfter = trPr.Element( XName.Get( "gridAfter", DocX.w.NamespaceName ) );
  2251. var gridAfterAttrVal = gridAfter?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  2252. if( gridAfterAttrVal != null )
  2253. {
  2254. return int.Parse( gridAfterAttrVal.Value );
  2255. }
  2256. }
  2257. return 0;
  2258. }
  2259. }
  2260. /// <summary>
  2261. /// A list of Cells in this Row.
  2262. /// </summary>
  2263. public List<Cell> Cells
  2264. {
  2265. get
  2266. {
  2267. List<Cell> cells =
  2268. (
  2269. from c in Xml.Elements( XName.Get( "tc", DocX.w.NamespaceName ) )
  2270. select new Cell( this, Document, c )
  2271. ).ToList();
  2272. return cells;
  2273. }
  2274. }
  2275. public override ReadOnlyCollection<Paragraph> Paragraphs
  2276. {
  2277. get
  2278. {
  2279. var paragraphs =
  2280. (
  2281. from p in Xml.Descendants( DocX.w + "p" )
  2282. select new Paragraph( Document, p, 0 )
  2283. ).ToList();
  2284. foreach( Paragraph p in paragraphs )
  2285. {
  2286. p.PackagePart = _table.PackagePart;
  2287. }
  2288. return paragraphs.AsReadOnly();
  2289. }
  2290. }
  2291. /// <summary>
  2292. /// Height in pixels.
  2293. /// </summary>
  2294. public double Height
  2295. {
  2296. get
  2297. {
  2298. /*
  2299. * Get the trPr (table row properties) element for this Row,
  2300. * null will be return if no such element exists.
  2301. */
  2302. XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2303. // If trPr is null, this row contains no height information.
  2304. // Get the trHeight element for this Row,
  2305. // null will be return if no such element exists.
  2306. XElement trHeight = trPr?.Element( XName.Get( "trHeight", DocX.w.NamespaceName ) );
  2307. // If trHeight is null, this row contains no height information.
  2308. // Get the val attribute for this trHeight element.
  2309. XAttribute val = trHeight?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  2310. // If w is null, this cell contains no width information.
  2311. if( val == null )
  2312. return double.NaN;
  2313. // If val is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2314. double heightInWordUnits;
  2315. if( !double.TryParse( val.Value, out heightInWordUnits ) )
  2316. {
  2317. val.Remove();
  2318. return double.NaN;
  2319. }
  2320. // Using 20 to match DocX._pageSizeMultiplier.
  2321. return ( heightInWordUnits / 20 );
  2322. }
  2323. set
  2324. {
  2325. SetHeight( value, true );
  2326. }
  2327. }
  2328. /// <summary>
  2329. /// Minimum Height in pixels.
  2330. /// </summary>
  2331. public double MinHeight
  2332. {
  2333. get
  2334. {
  2335. return Height;
  2336. }
  2337. set
  2338. {
  2339. SetHeight( value, false );
  2340. }
  2341. }
  2342. public bool TableHeader
  2343. {
  2344. get
  2345. {
  2346. XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2347. XElement tblHeader = trPr?.Element( XName.Get( "tblHeader", DocX.w.NamespaceName ) );
  2348. return tblHeader != null;
  2349. }
  2350. set
  2351. {
  2352. XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2353. if( trPr == null )
  2354. {
  2355. Xml.SetElementValue( XName.Get( "trPr", DocX.w.NamespaceName ), string.Empty );
  2356. trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2357. }
  2358. XElement tblHeader = trPr.Element( XName.Get( "tblHeader", DocX.w.NamespaceName ) );
  2359. if( tblHeader == null && value )
  2360. trPr.SetElementValue( XName.Get( "tblHeader", DocX.w.NamespaceName ), string.Empty );
  2361. if( tblHeader != null && !value )
  2362. tblHeader.Remove();
  2363. }
  2364. }
  2365. /// <summary>
  2366. /// Allow row to break across pages.
  2367. /// Default value is True : Word will break the contents of the row across the pages.
  2368. /// When False, the contents of the row will not be split across the pages; it will be entirely moved to the next page.
  2369. /// </summary>
  2370. public bool BreakAcrossPages
  2371. {
  2372. get
  2373. {
  2374. var trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2375. var cantSplit = trPr?.Element( XName.Get( "cantSplit", DocX.w.NamespaceName ) );
  2376. return cantSplit == null;
  2377. }
  2378. set
  2379. {
  2380. var trPrXName = XName.Get( "trPr", DocX.w.NamespaceName );
  2381. var cantSplitXName = XName.Get( "cantSplit", DocX.w.NamespaceName );
  2382. if( value )
  2383. {
  2384. var trPr = Xml.Element( trPrXName );
  2385. var cantSplit = trPr?.Element( cantSplitXName );
  2386. if( cantSplit != null )
  2387. cantSplit.Remove();
  2388. }
  2389. else
  2390. {
  2391. var trPr = Xml.Element( trPrXName );
  2392. if( trPr == null )
  2393. {
  2394. Xml.SetElementValue( trPrXName, string.Empty );
  2395. trPr = Xml.Element( trPrXName );
  2396. }
  2397. var cantSplit = trPr.Element( cantSplitXName );
  2398. if( cantSplit == null )
  2399. {
  2400. trPr.SetElementValue( cantSplitXName, string.Empty );
  2401. }
  2402. }
  2403. }
  2404. }
  2405. #endregion
  2406. #region Constructors
  2407. internal Row( Table table, DocX document, XElement xml )
  2408. : base( document, xml )
  2409. {
  2410. _table = table;
  2411. this.PackagePart = table.PackagePart;
  2412. }
  2413. #endregion
  2414. #region Private Methods
  2415. private void SetHeight( double height, bool isHeightExact )
  2416. {
  2417. XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2418. if( trPr == null )
  2419. {
  2420. Xml.SetElementValue( XName.Get( "trPr", DocX.w.NamespaceName ), string.Empty );
  2421. trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
  2422. }
  2423. XElement tc = Xml.Element( XName.Get( "tc", DocX.w.NamespaceName ) );
  2424. if( tc != null )
  2425. {
  2426. trPr.Remove();
  2427. tc.AddBeforeSelf( trPr );
  2428. }
  2429. XElement trHeight = trPr.Element( XName.Get( "trHeight", DocX.w.NamespaceName ) );
  2430. if( trHeight == null )
  2431. {
  2432. trPr.SetElementValue( XName.Get( "trHeight", DocX.w.NamespaceName ), string.Empty );
  2433. trHeight = trPr.Element( XName.Get( "trHeight", DocX.w.NamespaceName ) );
  2434. }
  2435. // The hRule attribute needs to be set.
  2436. trHeight.SetAttributeValue( XName.Get( "hRule", DocX.w.NamespaceName ), isHeightExact ? "exact" : "atLeast" );
  2437. // Using 20 to match DocX._pageSizeMultiplier.
  2438. trHeight.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), ( ( int )( Math.Round( height * 20, 0 ) ) ).ToString( CultureInfo.InvariantCulture ) );
  2439. }
  2440. #endregion
  2441. #region Public Methods
  2442. public void Remove()
  2443. {
  2444. XElement table = Xml.Parent;
  2445. Xml.Remove();
  2446. if( !table.Elements( XName.Get( "tr", DocX.w.NamespaceName ) ).Any() )
  2447. table.Remove();
  2448. }
  2449. /// <summary>
  2450. /// Merge cells starting with startIndex and ending with endIndex.
  2451. /// </summary>
  2452. public void MergeCells( int startIndex, int endIndex )
  2453. {
  2454. // Check for valid start and end indexes.
  2455. if( startIndex < 0 || endIndex <= startIndex || endIndex > Cells.Count + 1 )
  2456. throw new IndexOutOfRangeException();
  2457. // The sum of all merged gridSpans.
  2458. int gridSpanSum = 0;
  2459. // Foreach each Cell between startIndex and endIndex inclusive.
  2460. foreach( Cell c in Cells.Where( ( z, i ) => i > startIndex && i <= endIndex ) )
  2461. {
  2462. XElement tcPr = c.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2463. XElement gridSpan = tcPr?.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
  2464. if( gridSpan != null )
  2465. {
  2466. XAttribute val = gridSpan.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  2467. int value;
  2468. if( val != null && int.TryParse( val.Value, out value ) )
  2469. gridSpanSum += value - 1;
  2470. }
  2471. // Add this cells Pragraph to the merge start Cell.
  2472. Cells[ startIndex ].Xml.Add( c.Xml.Elements( XName.Get( "p", DocX.w.NamespaceName ) ) );
  2473. // Remove this Cell.
  2474. c.Xml.Remove();
  2475. }
  2476. // Trim cell's paragraphs to remove extra blank lines, if any
  2477. int index = 0;
  2478. do
  2479. {
  2480. // If the cell doesn't have multiple paragraphs, leave the loop
  2481. if ( Cells[ startIndex ].Paragraphs.Count < 2 )
  2482. break;
  2483. // Remove the last paragraph if it's a blank line, otherwise trimming is done
  2484. index = Cells[ startIndex ].Paragraphs.Count - 1;
  2485. if ( Cells[ startIndex ].Paragraphs[ index ].Text.Trim() == "" )
  2486. Cells[ startIndex ].Paragraphs[ index ].Remove( false );
  2487. else
  2488. break;
  2489. } while( true );
  2490. /*
  2491. * Get the tcPr (table cell properties) element for the first cell in this merge,
  2492. * null will be returned if no such element exists.
  2493. */
  2494. XElement start_tcPr = Cells[ startIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2495. if( start_tcPr == null )
  2496. {
  2497. Cells[ startIndex ].Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  2498. start_tcPr = Cells[ startIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2499. }
  2500. /*
  2501. * Get the gridSpan element of this row,
  2502. * null will be returned if no such element exists.
  2503. */
  2504. XElement start_gridSpan = start_tcPr.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
  2505. if( start_gridSpan == null )
  2506. {
  2507. start_tcPr.SetElementValue( XName.Get( "gridSpan", DocX.w.NamespaceName ), string.Empty );
  2508. start_gridSpan = start_tcPr.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
  2509. }
  2510. /*
  2511. * Get the val attribute of this row,
  2512. * null will be returned if no such element exists.
  2513. */
  2514. XAttribute start_val = start_gridSpan.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  2515. int start_value = 0;
  2516. if( start_val != null )
  2517. if( int.TryParse( start_val.Value, out start_value ) )
  2518. gridSpanSum += start_value - 1;
  2519. // Set the val attribute to the number of merged cells.
  2520. start_gridSpan.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), ( gridSpanSum + ( endIndex - startIndex + 1 ) ).ToString() );
  2521. }
  2522. #endregion
  2523. }
  2524. public class Cell : Container
  2525. {
  2526. #region Internal Members
  2527. internal Row _row;
  2528. #endregion
  2529. #region Public Properties
  2530. public override ReadOnlyCollection<Paragraph> Paragraphs
  2531. {
  2532. get
  2533. {
  2534. var paragraphs = base.Paragraphs;
  2535. foreach( Paragraph p in paragraphs )
  2536. {
  2537. p.PackagePart = _row._table.PackagePart;
  2538. }
  2539. return paragraphs;
  2540. }
  2541. }
  2542. // <summary>
  2543. // Gets or Sets this Cells vertical alignment.
  2544. // </summary>
  2545. // <example>
  2546. // Creates a table with 3 cells and sets the vertical alignment of each to 1 of the 3 available options.
  2547. // <code>
  2548. // Create a new document.
  2549. //using(DocX document = DocX.Create("Test.docx"))
  2550. //{
  2551. // // Insert a Table into this document.
  2552. // Table t = document.InsertTable(3, 1);
  2553. //
  2554. // // Set the design of the Table such that we can easily identify cell boundaries.
  2555. // t.Design = TableDesign.TableGrid;
  2556. //
  2557. // // Set the height of the row bigger than default.
  2558. // // We need to be able to see the difference in vertical cell alignment options.
  2559. // t.Rows[0].Height = 100;
  2560. //
  2561. // // Set the vertical alignment of cell0 to top.
  2562. // Cell c0 = t.Rows[0].Cells[0];
  2563. // c0.InsertParagraph("VerticalAlignment.Top");
  2564. // c0.VerticalAlignment = VerticalAlignment.Top;
  2565. //
  2566. // // Set the vertical alignment of cell1 to center.
  2567. // Cell c1 = t.Rows[0].Cells[1];
  2568. // c1.InsertParagraph("VerticalAlignment.Center");
  2569. // c1.VerticalAlignment = VerticalAlignment.Center;
  2570. //
  2571. // // Set the vertical alignment of cell2 to bottom.
  2572. // Cell c2 = t.Rows[0].Cells[2];
  2573. // c2.InsertParagraph("VerticalAlignment.Bottom");
  2574. // c2.VerticalAlignment = VerticalAlignment.Bottom;
  2575. //
  2576. // // Save the document.
  2577. // document.Save();
  2578. //}
  2579. // </code>
  2580. // </example>
  2581. public VerticalAlignment VerticalAlignment
  2582. {
  2583. get
  2584. {
  2585. /*
  2586. * Get the tcPr (table cell properties) element for this Cell,
  2587. * null will be return if no such element exists.
  2588. */
  2589. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2590. // If tcPr is null, this cell contains no width information.
  2591. // Get the vAlign (table cell vertical alignment) element for this Cell,
  2592. // null will be return if no such element exists.
  2593. XElement vAlign = tcPr?.Element( XName.Get( "vAlign", DocX.w.NamespaceName ) );
  2594. // If vAlign is null, this cell contains no vertical alignment information.
  2595. // Get the val attribute of the vAlign element.
  2596. XAttribute val = vAlign?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  2597. // If val is null, this cell contains no vAlign information.
  2598. if( val == null )
  2599. return VerticalAlignment.Top;
  2600. // If val is not a VerticalAlign enum, something is wrong with this attributes value, so remove it and return VerticalAlignment.Center;
  2601. try
  2602. {
  2603. return ( VerticalAlignment )Enum.Parse( typeof( VerticalAlignment ), val.Value, true );
  2604. }
  2605. catch
  2606. {
  2607. val.Remove();
  2608. return VerticalAlignment.Top;
  2609. }
  2610. }
  2611. set
  2612. {
  2613. /*
  2614. * Get the tcPr (table cell properties) element for this Cell,
  2615. * null will be return if no such element exists.
  2616. */
  2617. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2618. if( tcPr == null )
  2619. {
  2620. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  2621. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2622. }
  2623. /*
  2624. * Get the vAlign (table cell vertical alignment) element for this Cell,
  2625. * null will be return if no such element exists.
  2626. */
  2627. XElement vAlign = tcPr.Element( XName.Get( "vAlign", DocX.w.NamespaceName ) );
  2628. if( vAlign == null )
  2629. {
  2630. tcPr.SetElementValue( XName.Get( "vAlign", DocX.w.NamespaceName ), string.Empty );
  2631. vAlign = tcPr.Element( XName.Get( "vAlign", DocX.w.NamespaceName ) );
  2632. }
  2633. // Set the VerticalAlignment in 'val'
  2634. vAlign.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), value.ToString().ToLower() );
  2635. }
  2636. }
  2637. public Color Shading
  2638. {
  2639. get
  2640. {
  2641. /*
  2642. * Get the tcPr (table cell properties) element for this Cell,
  2643. * null will be return if no such element exists.
  2644. */
  2645. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2646. // If tcPr is null, this cell contains no Color information.
  2647. // Get the shd (table shade) element for this Cell,
  2648. // null will be return if no such element exists.
  2649. XElement shd = tcPr?.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
  2650. // If shd is null, this cell contains no Color information.
  2651. // Get the w attribute of the tcW element.
  2652. XAttribute fill = shd?.Attribute( XName.Get( "fill", DocX.w.NamespaceName ) );
  2653. // If fill is null, this cell contains no Color information.
  2654. if( fill == null )
  2655. return Color.White;
  2656. return ColorTranslator.FromHtml( string.Format( "#{0}", fill.Value ) );
  2657. }
  2658. set
  2659. {
  2660. /*
  2661. * Get the tcPr (table cell properties) element for this Cell,
  2662. * null will be return if no such element exists.
  2663. */
  2664. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2665. if( tcPr == null )
  2666. {
  2667. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  2668. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2669. }
  2670. /*
  2671. * Get the shd (table shade) element for this Cell,
  2672. * null will be return if no such element exists.
  2673. */
  2674. XElement shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
  2675. if( shd == null )
  2676. {
  2677. tcPr.SetElementValue( XName.Get( "shd", DocX.w.NamespaceName ), string.Empty );
  2678. shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
  2679. }
  2680. // The val attribute needs to be set to clear
  2681. shd.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), "clear" );
  2682. // The color attribute needs to be set to auto
  2683. shd.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), "auto" );
  2684. // The fill attribute needs to be set to the hex for this Color.
  2685. shd.SetAttributeValue( XName.Get( "fill", DocX.w.NamespaceName ), value.ToHex() );
  2686. }
  2687. }
  2688. /// <summary>
  2689. /// Width in pixels.
  2690. /// </summary>
  2691. public double Width
  2692. {
  2693. get
  2694. {
  2695. /*
  2696. * Get the tcPr (table cell properties) element for this Cell,
  2697. * null will be return if no such element exists.
  2698. */
  2699. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2700. // If tcPr is null, this cell contains no width information.
  2701. // Get the tcW (table cell width) element for this Cell,
  2702. // null will be return if no such element exists.
  2703. XElement tcW = tcPr?.Element( XName.Get( "tcW", DocX.w.NamespaceName ) );
  2704. // If tcW is null, this cell contains no width information.
  2705. // Get the w attribute of the tcW element.
  2706. XAttribute w = tcW?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
  2707. XAttribute type = tcW?.Attribute( XName.Get( "type", DocX.w.NamespaceName ) );
  2708. // If w is null, this cell contains no width information.
  2709. if( w == null )
  2710. return double.NaN;
  2711. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2712. double widthInWordUnits;
  2713. if( !double.TryParse( w.Value, out widthInWordUnits ) )
  2714. {
  2715. w.Remove();
  2716. return double.NaN;
  2717. }
  2718. if( type != null )
  2719. {
  2720. if( type.Value == "pct" )
  2721. {
  2722. return ( ( widthInWordUnits / 5000d ) * this._row._table.ColumnWidths.Sum() ) / 20;
  2723. }
  2724. else if( type.Value == "auto" )
  2725. {
  2726. var cellIndex = this._row.Cells.FindIndex( x => x.Xml == this.Xml );
  2727. if( cellIndex >= 0 )
  2728. return this._row._table.ColumnWidths[ cellIndex ] / 20;
  2729. }
  2730. }
  2731. // Using 20 to match DocX._pageSizeMultiplier.
  2732. return ( widthInWordUnits / 20 );
  2733. }
  2734. set
  2735. {
  2736. /*
  2737. * Get the tcPr (table cell properties) element for this Cell,
  2738. * null will be return if no such element exists.
  2739. */
  2740. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2741. if( tcPr == null )
  2742. {
  2743. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  2744. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2745. }
  2746. /*
  2747. * Get the tcW (table cell width) element for this Cell,
  2748. * null will be return if no such element exists.
  2749. */
  2750. XElement tcW = tcPr.Element( XName.Get( "tcW", DocX.w.NamespaceName ) );
  2751. if( tcW == null )
  2752. {
  2753. tcPr.SetElementValue( XName.Get( "tcW", DocX.w.NamespaceName ), string.Empty );
  2754. tcW = tcPr.Element( XName.Get( "tcW", DocX.w.NamespaceName ) );
  2755. }
  2756. if( value == -1 )
  2757. {
  2758. // remove cell width; due to set on table prop.
  2759. tcW.Remove();
  2760. return;
  2761. }
  2762. // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
  2763. tcW.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
  2764. // Using 20 to match DocX._pageSizeMultiplier.
  2765. tcW.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
  2766. }
  2767. }
  2768. /// <summary>
  2769. /// LeftMargin in pixels.
  2770. /// </summary>
  2771. /// <example>
  2772. /// <code>
  2773. /// // Create a new document.
  2774. ///using (DocX document = DocX.Create("Test.docx"))
  2775. ///{
  2776. /// // Insert table into this document.
  2777. /// Table t = document.InsertTable(3, 3);
  2778. /// t.Design = TableDesign.TableGrid;
  2779. ///
  2780. /// // Get the center cell.
  2781. /// Cell center = t.Rows[1].Cells[1];
  2782. ///
  2783. /// // Insert some text so that we can see the effect of the Margins.
  2784. /// center.Paragraphs[0].Append("Center Cell");
  2785. ///
  2786. /// // Set the center cells Left, Margin to 10.
  2787. /// center.MarginLeft = 25;
  2788. ///
  2789. /// // Save the document.
  2790. /// document.Save();
  2791. ///}
  2792. /// </code>
  2793. /// </example>
  2794. public double MarginLeft
  2795. {
  2796. get
  2797. {
  2798. /*
  2799. * Get the tcPr (table cell properties) element for this Cell,
  2800. * null will be return if no such element exists.
  2801. */
  2802. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2803. // If tcPr is null, this cell contains no width information.
  2804. if( tcPr == null )
  2805. return double.NaN;
  2806. /*
  2807. * Get the tcMar
  2808. *
  2809. */
  2810. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  2811. // If tcMar is null, this cell contains no margin information.
  2812. // Get the left (LeftMargin) element
  2813. XElement tcMarLeft = tcMar?.Element( XName.Get( "left", DocX.w.NamespaceName ) );
  2814. // If tcMarLeft is null, this cell contains no left margin information.
  2815. // Get the w attribute of the tcMarLeft element.
  2816. XAttribute w = tcMarLeft?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
  2817. // If w is null, this cell contains no width information.
  2818. if( w == null )
  2819. return double.NaN;
  2820. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2821. double leftMarginInWordUnits;
  2822. if( !double.TryParse( w.Value, out leftMarginInWordUnits ) )
  2823. {
  2824. w.Remove();
  2825. return double.NaN;
  2826. }
  2827. // Using 20 to match DocX._pageSizeMultiplier.
  2828. return ( leftMarginInWordUnits / 20 );
  2829. }
  2830. set
  2831. {
  2832. /*
  2833. * Get the tcPr (table cell properties) element for this Cell,
  2834. * null will be return if no such element exists.
  2835. */
  2836. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2837. if( tcPr == null )
  2838. {
  2839. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  2840. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2841. }
  2842. /*
  2843. * Get the tcMar (table cell margin) element for this Cell,
  2844. * null will be return if no such element exists.
  2845. */
  2846. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  2847. if( tcMar == null )
  2848. {
  2849. tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
  2850. tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  2851. }
  2852. /*
  2853. * Get the left (table cell left margin) element for this Cell,
  2854. * null will be return if no such element exists.
  2855. */
  2856. XElement tcMarLeft = tcMar.Element( XName.Get( "left", DocX.w.NamespaceName ) );
  2857. if( tcMarLeft == null )
  2858. {
  2859. tcMar.SetElementValue( XName.Get( "left", DocX.w.NamespaceName ), string.Empty );
  2860. tcMarLeft = tcMar.Element( XName.Get( "left", DocX.w.NamespaceName ) );
  2861. }
  2862. // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
  2863. tcMarLeft.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
  2864. // Using 20 to match DocX._pageSizeMultiplier.
  2865. tcMarLeft.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
  2866. }
  2867. }
  2868. /// <summary>
  2869. /// RightMargin in pixels.
  2870. /// </summary>
  2871. /// <example>
  2872. /// <code>
  2873. /// // Create a new document.
  2874. ///using (DocX document = DocX.Create("Test.docx"))
  2875. ///{
  2876. /// // Insert table into this document.
  2877. /// Table t = document.InsertTable(3, 3);
  2878. /// t.Design = TableDesign.TableGrid;
  2879. ///
  2880. /// // Get the center cell.
  2881. /// Cell center = t.Rows[1].Cells[1];
  2882. ///
  2883. /// // Insert some text so that we can see the effect of the Margins.
  2884. /// center.Paragraphs[0].Append("Center Cell");
  2885. ///
  2886. /// // Set the center cells Right, Margin to 10.
  2887. /// center.MarginRight = 25;
  2888. ///
  2889. /// // Save the document.
  2890. /// document.Save();
  2891. ///}
  2892. /// </code>
  2893. /// </example>
  2894. public double MarginRight
  2895. {
  2896. get
  2897. {
  2898. /*
  2899. * Get the tcPr (table cell properties) element for this Cell,
  2900. * null will be return if no such element exists.
  2901. */
  2902. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2903. // If tcPr is null, this cell contains no width information.
  2904. if( tcPr == null )
  2905. return double.NaN;
  2906. /*
  2907. * Get the tcMar
  2908. *
  2909. */
  2910. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  2911. // If tcMar is null, this cell contains no margin information.
  2912. // Get the right (RightMargin) element
  2913. XElement tcMarRight = tcMar?.Element( XName.Get( "right", DocX.w.NamespaceName ) );
  2914. // If tcMarRight is null, this cell contains no right margin information.
  2915. // Get the w attribute of the tcMarRight element.
  2916. XAttribute w = tcMarRight?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
  2917. // If w is null, this cell contains no width information.
  2918. if( w == null )
  2919. return double.NaN;
  2920. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2921. double rightMarginInWordUnits;
  2922. if( !double.TryParse( w.Value, out rightMarginInWordUnits ) )
  2923. {
  2924. w.Remove();
  2925. return double.NaN;
  2926. }
  2927. // Using 20 to match DocX._pageSizeMultiplier.
  2928. return ( rightMarginInWordUnits / 20 );
  2929. }
  2930. set
  2931. {
  2932. /*
  2933. * Get the tcPr (table cell properties) element for this Cell,
  2934. * null will be return if no such element exists.
  2935. */
  2936. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2937. if( tcPr == null )
  2938. {
  2939. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  2940. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  2941. }
  2942. /*
  2943. * Get the tcMar (table cell margin) element for this Cell,
  2944. * null will be return if no such element exists.
  2945. */
  2946. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  2947. if( tcMar == null )
  2948. {
  2949. tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
  2950. tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  2951. }
  2952. /*
  2953. * Get the right (table cell right margin) element for this Cell,
  2954. * null will be return if no such element exists.
  2955. */
  2956. XElement tcMarRight = tcMar.Element( XName.Get( "right", DocX.w.NamespaceName ) );
  2957. if( tcMarRight == null )
  2958. {
  2959. tcMar.SetElementValue( XName.Get( "right", DocX.w.NamespaceName ), string.Empty );
  2960. tcMarRight = tcMar.Element( XName.Get( "right", DocX.w.NamespaceName ) );
  2961. }
  2962. // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
  2963. tcMarRight.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
  2964. // Using 20 to match DocX._pageSizeMultiplier.
  2965. tcMarRight.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
  2966. }
  2967. }
  2968. /// <summary>
  2969. /// TopMargin in pixels.
  2970. /// </summary>
  2971. /// <example>
  2972. /// <code>
  2973. /// // Create a new document.
  2974. ///using (DocX document = DocX.Create("Test.docx"))
  2975. ///{
  2976. /// // Insert table into this document.
  2977. /// Table t = document.InsertTable(3, 3);
  2978. /// t.Design = TableDesign.TableGrid;
  2979. ///
  2980. /// // Get the center cell.
  2981. /// Cell center = t.Rows[1].Cells[1];
  2982. ///
  2983. /// // Insert some text so that we can see the effect of the Margins.
  2984. /// center.Paragraphs[0].Append("Center Cell");
  2985. ///
  2986. /// // Set the center cells Top, Margin to 10.
  2987. /// center.MarginTop = 25;
  2988. ///
  2989. /// // Save the document.
  2990. /// document.Save();
  2991. ///}
  2992. /// </code>
  2993. /// </example>
  2994. public double MarginTop
  2995. {
  2996. get
  2997. {
  2998. /*
  2999. * Get the tcPr (table cell properties) element for this Cell,
  3000. * null will be return if no such element exists.
  3001. */
  3002. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3003. // If tcPr is null, this cell contains no width information.
  3004. if( tcPr == null )
  3005. return double.NaN;
  3006. /*
  3007. * Get the tcMar
  3008. *
  3009. */
  3010. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  3011. // If tcMar is null, this cell contains no margin information.
  3012. // Get the top (TopMargin) element
  3013. XElement tcMarTop = tcMar?.Element( XName.Get( "top", DocX.w.NamespaceName ) );
  3014. // If tcMarTop is null, this cell contains no top margin information.
  3015. // Get the w attribute of the tcMarTop element.
  3016. XAttribute w = tcMarTop?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
  3017. // If w is null, this cell contains no width information.
  3018. if( w == null )
  3019. return double.NaN;
  3020. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  3021. double topMarginInWordUnits;
  3022. if( !double.TryParse( w.Value, out topMarginInWordUnits ) )
  3023. {
  3024. w.Remove();
  3025. return double.NaN;
  3026. }
  3027. // Using 20 to match DocX._pageSizeMultiplier.
  3028. return ( topMarginInWordUnits / 20 );
  3029. }
  3030. set
  3031. {
  3032. /*
  3033. * Get the tcPr (table cell properties) element for this Cell,
  3034. * null will be return if no such element exists.
  3035. */
  3036. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3037. if( tcPr == null )
  3038. {
  3039. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  3040. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3041. }
  3042. /*
  3043. * Get the tcMar (table cell margin) element for this Cell,
  3044. * null will be return if no such element exists.
  3045. */
  3046. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  3047. if( tcMar == null )
  3048. {
  3049. tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
  3050. tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  3051. }
  3052. /*
  3053. * Get the top (table cell top margin) element for this Cell,
  3054. * null will be return if no such element exists.
  3055. */
  3056. XElement tcMarTop = tcMar.Element( XName.Get( "top", DocX.w.NamespaceName ) );
  3057. if( tcMarTop == null )
  3058. {
  3059. tcMar.SetElementValue( XName.Get( "top", DocX.w.NamespaceName ), string.Empty );
  3060. tcMarTop = tcMar.Element( XName.Get( "top", DocX.w.NamespaceName ) );
  3061. }
  3062. // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
  3063. tcMarTop.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
  3064. // Using 20 to match DocX._pageSizeMultiplier.
  3065. tcMarTop.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
  3066. }
  3067. }
  3068. /// <summary>
  3069. /// BottomMargin in pixels.
  3070. /// </summary>
  3071. /// <example>
  3072. /// <code>
  3073. /// // Create a new document.
  3074. ///using (DocX document = DocX.Create("Test.docx"))
  3075. ///{
  3076. /// // Insert table into this document.
  3077. /// Table t = document.InsertTable(3, 3);
  3078. /// t.Design = TableDesign.TableGrid;
  3079. ///
  3080. /// // Get the center cell.
  3081. /// Cell center = t.Rows[1].Cells[1];
  3082. ///
  3083. /// // Insert some text so that we can see the effect of the Margins.
  3084. /// center.Paragraphs[0].Append("Center Cell");
  3085. ///
  3086. /// // Set the center cells Top, Margin to 10.
  3087. /// center.MarginBottom = 25;
  3088. ///
  3089. /// // Save the document.
  3090. /// document.Save();
  3091. ///}
  3092. /// </code>
  3093. /// </example>
  3094. public double MarginBottom
  3095. {
  3096. get
  3097. {
  3098. /*
  3099. * Get the tcPr (table cell properties) element for this Cell,
  3100. * null will be return if no such element exists.
  3101. */
  3102. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3103. // If tcPr is null, this cell contains no width information.
  3104. if( tcPr == null )
  3105. return double.NaN;
  3106. /*
  3107. * Get the tcMar
  3108. *
  3109. */
  3110. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  3111. // If tcMar is null, this cell contains no margin information.
  3112. // Get the bottom (BottomMargin) element
  3113. XElement tcMarBottom = tcMar?.Element( XName.Get( "bottom", DocX.w.NamespaceName ) );
  3114. // If tcMarBottom is null, this cell contains no bottom margin information.
  3115. // Get the w attribute of the tcMarBottom element.
  3116. XAttribute w = tcMarBottom?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
  3117. // If w is null, this cell contains no width information.
  3118. if( w == null )
  3119. return double.NaN;
  3120. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  3121. double bottomMarginInWordUnits;
  3122. if( !double.TryParse( w.Value, out bottomMarginInWordUnits ) )
  3123. {
  3124. w.Remove();
  3125. return double.NaN;
  3126. }
  3127. // Using 20 to match DocX._pageSizeMultiplier.
  3128. return ( bottomMarginInWordUnits / 20 );
  3129. }
  3130. set
  3131. {
  3132. /*
  3133. * Get the tcPr (table cell properties) element for this Cell,
  3134. * null will be return if no such element exists.
  3135. */
  3136. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3137. if( tcPr == null )
  3138. {
  3139. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  3140. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3141. }
  3142. /*
  3143. * Get the tcMar (table cell margin) element for this Cell,
  3144. * null will be return if no such element exists.
  3145. */
  3146. XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  3147. if( tcMar == null )
  3148. {
  3149. tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
  3150. tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
  3151. }
  3152. /*
  3153. * Get the bottom (table cell bottom margin) element for this Cell,
  3154. * null will be return if no such element exists.
  3155. */
  3156. XElement tcMarBottom = tcMar.Element( XName.Get( "bottom", DocX.w.NamespaceName ) );
  3157. if( tcMarBottom == null )
  3158. {
  3159. tcMar.SetElementValue( XName.Get( "bottom", DocX.w.NamespaceName ), string.Empty );
  3160. tcMarBottom = tcMar.Element( XName.Get( "bottom", DocX.w.NamespaceName ) );
  3161. }
  3162. // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
  3163. tcMarBottom.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
  3164. // Using 20 to match DocX._pageSizeMultiplier.
  3165. tcMarBottom.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
  3166. }
  3167. }
  3168. public Color FillColor
  3169. {
  3170. get
  3171. {
  3172. /*
  3173. * Get the tcPr (table cell properties) element for this Cell,
  3174. * null will be return if no such element exists.
  3175. */
  3176. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3177. XElement shd = tcPr?.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
  3178. XAttribute fill = shd?.Attribute( XName.Get( "fill", DocX.w.NamespaceName ) );
  3179. if( fill == null )
  3180. return Color.Empty;
  3181. int argb = Int32.Parse( fill.Value.Replace( "#", "" ), NumberStyles.HexNumber );
  3182. return Color.FromArgb( argb );
  3183. }
  3184. set
  3185. {
  3186. /*
  3187. * Get the tcPr (table cell properties) element for this Cell,
  3188. * null will be return if no such element exists.
  3189. */
  3190. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3191. if( tcPr == null )
  3192. {
  3193. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  3194. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3195. }
  3196. /*
  3197. * Get the tcW (table cell width) element for this Cell,
  3198. * null will be return if no such element exists.
  3199. */
  3200. XElement shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
  3201. if( shd == null )
  3202. {
  3203. tcPr.SetElementValue( XName.Get( "shd", DocX.w.NamespaceName ), string.Empty );
  3204. shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
  3205. }
  3206. shd.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), "clear" );
  3207. shd.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), "auto" );
  3208. shd.SetAttributeValue( XName.Get( "fill", DocX.w.NamespaceName ), value.ToHex() );
  3209. }
  3210. }
  3211. public TextDirection TextDirection
  3212. {
  3213. get
  3214. {
  3215. var tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3216. var textDirection = tcPr?.Element( XName.Get( "textDirection", DocX.w.NamespaceName ) );
  3217. var val = textDirection?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  3218. if( val != null )
  3219. {
  3220. TextDirection textDirectionValue;
  3221. var success = Enum.TryParse( val.Value, out textDirectionValue );
  3222. if( success )
  3223. return textDirectionValue;
  3224. else
  3225. {
  3226. val.Remove();
  3227. }
  3228. }
  3229. return TextDirection.right;
  3230. }
  3231. set
  3232. {
  3233. var tcPrXName = XName.Get( "tcPr", DocX.w.NamespaceName );
  3234. var textDirectionXName = XName.Get( "textDirection", DocX.w.NamespaceName );
  3235. var tcPr = Xml.Element( tcPrXName );
  3236. if( tcPr == null )
  3237. {
  3238. Xml.SetElementValue( tcPrXName, string.Empty );
  3239. tcPr = Xml.Element( tcPrXName );
  3240. }
  3241. var textDirection = tcPr.Element( textDirectionXName );
  3242. if( textDirection == null )
  3243. {
  3244. tcPr.SetElementValue( textDirectionXName, string.Empty );
  3245. textDirection = tcPr.Element( textDirectionXName );
  3246. }
  3247. textDirection.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), value.ToString() );
  3248. }
  3249. }
  3250. /// <summary>
  3251. /// Returns the Cell.GridSpan => How many cells are merged.
  3252. /// </summary>
  3253. public int GridSpan
  3254. {
  3255. get
  3256. {
  3257. int gridSpanValue = 0;
  3258. var tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3259. var gridSpan = tcPr?.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
  3260. if( gridSpan != null )
  3261. {
  3262. var gridSpanAttrValue = gridSpan.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  3263. int value;
  3264. if( gridSpanAttrValue != null && int.TryParse( gridSpanAttrValue.Value, out value ) )
  3265. gridSpanValue = value;
  3266. }
  3267. return gridSpanValue;
  3268. }
  3269. }
  3270. #endregion
  3271. #region Constructors
  3272. internal Cell( Row row, DocX document, XElement xml )
  3273. : base( document, xml )
  3274. {
  3275. _row = row;
  3276. this.PackagePart = row.PackagePart;
  3277. }
  3278. #endregion
  3279. #region Public Methods
  3280. // <summary>
  3281. // Set the table cell border
  3282. // </summary>
  3283. // <example>
  3284. // <code>
  3285. // Create a new document.
  3286. //using (DocX document = DocX.Create("Test.docx"))
  3287. //{
  3288. // // Insert a table into this document.
  3289. // Table t = document.InsertTable(3, 3);
  3290. //
  3291. // // Get the center cell.
  3292. // Cell center = t.Rows[1].Cells[1];
  3293. //
  3294. // // Create a large blue border.
  3295. // Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
  3296. //
  3297. // // Set the center cells Top, Bottom, Left and Right Borders to b.
  3298. // center.SetBorder(TableCellBorderType.Top, b);
  3299. // center.SetBorder(TableCellBorderType.Bottom, b);
  3300. // center.SetBorder(TableCellBorderType.Left, b);
  3301. // center.SetBorder(TableCellBorderType.Right, b);
  3302. //
  3303. // // Save the document.
  3304. // document.Save();
  3305. //}
  3306. // </code>
  3307. // </example>
  3308. // <param name="borderType">Table Cell border to set</param>
  3309. // <param name="border">Border object to set the table cell border</param>
  3310. public void SetBorder( TableCellBorderType borderType, Border border )
  3311. {
  3312. /*
  3313. * Get the tcPr (table cell properties) element for this Cell,
  3314. * null will be return if no such element exists.
  3315. */
  3316. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3317. if( tcPr == null )
  3318. {
  3319. Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
  3320. tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3321. }
  3322. /*
  3323. * Get the tblBorders (table cell borders) element for this Cell,
  3324. * null will be return if no such element exists.
  3325. */
  3326. XElement tcBorders = tcPr.Element( XName.Get( "tcBorders", DocX.w.NamespaceName ) );
  3327. if( tcBorders == null )
  3328. {
  3329. tcPr.SetElementValue( XName.Get( "tcBorders", DocX.w.NamespaceName ), string.Empty );
  3330. tcBorders = tcPr.Element( XName.Get( "tcBorders", DocX.w.NamespaceName ) );
  3331. }
  3332. /*
  3333. * Get the 'borderType' (table cell border) element for this Cell,
  3334. * null will be return if no such element exists.
  3335. */
  3336. var tcbordertype = borderType.ToString();
  3337. switch( borderType )
  3338. {
  3339. case TableCellBorderType.TopLeftToBottomRight:
  3340. tcbordertype = "tl2br";
  3341. break;
  3342. case TableCellBorderType.TopRightToBottomLeft:
  3343. tcbordertype = "tr2bl";
  3344. break;
  3345. default:
  3346. // only lower the first char of string (because of insideH and insideV)
  3347. tcbordertype = tcbordertype.Substring( 0, 1 ).ToLower() + tcbordertype.Substring( 1 );
  3348. break;
  3349. }
  3350. XElement tcBorderType = tcBorders.Element( XName.Get( borderType.ToString(), DocX.w.NamespaceName ) );
  3351. if( tcBorderType == null )
  3352. {
  3353. tcBorders.SetElementValue( XName.Get( tcbordertype, DocX.w.NamespaceName ), string.Empty );
  3354. tcBorderType = tcBorders.Element( XName.Get( tcbordertype, DocX.w.NamespaceName ) );
  3355. }
  3356. // get string value of border style
  3357. string borderstyle = border.Tcbs.ToString().Substring( 5 );
  3358. borderstyle = borderstyle.Substring( 0, 1 ).ToLower() + borderstyle.Substring( 1 );
  3359. // The val attribute is used for the border style
  3360. tcBorderType.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), borderstyle );
  3361. int size;
  3362. switch( border.Size )
  3363. {
  3364. case BorderSize.one:
  3365. size = 2;
  3366. break;
  3367. case BorderSize.two:
  3368. size = 4;
  3369. break;
  3370. case BorderSize.three:
  3371. size = 6;
  3372. break;
  3373. case BorderSize.four:
  3374. size = 8;
  3375. break;
  3376. case BorderSize.five:
  3377. size = 12;
  3378. break;
  3379. case BorderSize.six:
  3380. size = 18;
  3381. break;
  3382. case BorderSize.seven:
  3383. size = 24;
  3384. break;
  3385. case BorderSize.eight:
  3386. size = 36;
  3387. break;
  3388. case BorderSize.nine:
  3389. size = 48;
  3390. break;
  3391. default:
  3392. size = 2;
  3393. break;
  3394. }
  3395. // The sz attribute is used for the border size
  3396. tcBorderType.SetAttributeValue( XName.Get( "sz", DocX.w.NamespaceName ), ( size ).ToString() );
  3397. // The space attribute is used for the cell spacing (probably '0')
  3398. tcBorderType.SetAttributeValue( XName.Get( "space", DocX.w.NamespaceName ), ( border.Space ).ToString() );
  3399. // The color attribute is used for the border color
  3400. tcBorderType.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), border.Color.ToHex() );
  3401. }
  3402. /// <summary>
  3403. /// Get a table cell border
  3404. /// </summary>
  3405. /// <param name="borderType">The table cell border to get</param>
  3406. public Border GetBorder( TableCellBorderType borderType )
  3407. {
  3408. // instance with default border values
  3409. var b = new Border();
  3410. /*
  3411. * Get the tcPr (table cell properties) element for this Cell,
  3412. * null will be return if no such element exists.
  3413. */
  3414. XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
  3415. if( tcPr == null )
  3416. {
  3417. // uses default border style
  3418. }
  3419. /*
  3420. * Get the tcBorders (table cell borders) element for this Cell,
  3421. * null will be return if no such element exists.
  3422. */
  3423. XElement tcBorders = tcPr.Element( XName.Get( "tcBorders", DocX.w.NamespaceName ) );
  3424. if( tcBorders == null )
  3425. {
  3426. // uses default border style
  3427. }
  3428. /*
  3429. * Get the 'borderType' (cell border) element for this Cell,
  3430. * null will be return if no such element exists.
  3431. */
  3432. var tcbordertype = borderType.ToString();
  3433. switch( tcbordertype )
  3434. {
  3435. case "TopLeftToBottomRight":
  3436. tcbordertype = "tl2br";
  3437. break;
  3438. case "TopRightToBottomLeft":
  3439. tcbordertype = "tr2bl";
  3440. break;
  3441. default:
  3442. // only lower the first char of string (because of insideH and insideV)
  3443. tcbordertype = tcbordertype.Substring( 0, 1 ).ToLower() + tcbordertype.Substring( 1 );
  3444. break;
  3445. }
  3446. XElement tcBorderType = tcBorders.Element( XName.Get( tcbordertype, DocX.w.NamespaceName ) );
  3447. if( tcBorderType == null )
  3448. {
  3449. // uses default border style
  3450. }
  3451. // The val attribute is used for the border style
  3452. XAttribute val = tcBorderType.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
  3453. // If val is null, this cell contains no border information.
  3454. if( val == null )
  3455. {
  3456. // uses default border style
  3457. }
  3458. else
  3459. {
  3460. try
  3461. {
  3462. string bordertype = "Tcbs_" + val.Value;
  3463. b.Tcbs = ( BorderStyle )Enum.Parse( typeof( BorderStyle ), bordertype );
  3464. }
  3465. catch
  3466. {
  3467. val.Remove();
  3468. // uses default border style
  3469. }
  3470. }
  3471. // The sz attribute is used for the border size
  3472. XAttribute sz = tcBorderType.Attribute( XName.Get( "sz", DocX.w.NamespaceName ) );
  3473. // If sz is null, this border contains no size information.
  3474. if( sz == null )
  3475. {
  3476. // uses default border style
  3477. }
  3478. else
  3479. {
  3480. // If sz is not an int, something is wrong with this attributes value, so remove it
  3481. int numerical_size;
  3482. if( !int.TryParse( sz.Value, out numerical_size ) )
  3483. sz.Remove();
  3484. else
  3485. {
  3486. switch( numerical_size )
  3487. {
  3488. case 2:
  3489. b.Size = BorderSize.one;
  3490. break;
  3491. case 4:
  3492. b.Size = BorderSize.two;
  3493. break;
  3494. case 6:
  3495. b.Size = BorderSize.three;
  3496. break;
  3497. case 8:
  3498. b.Size = BorderSize.four;
  3499. break;
  3500. case 12:
  3501. b.Size = BorderSize.five;
  3502. break;
  3503. case 18:
  3504. b.Size = BorderSize.six;
  3505. break;
  3506. case 24:
  3507. b.Size = BorderSize.seven;
  3508. break;
  3509. case 36:
  3510. b.Size = BorderSize.eight;
  3511. break;
  3512. case 48:
  3513. b.Size = BorderSize.nine;
  3514. break;
  3515. default:
  3516. b.Size = BorderSize.one;
  3517. break;
  3518. }
  3519. }
  3520. }
  3521. // The space attribute is used for the border spacing (probably '0')
  3522. XAttribute space = tcBorderType.Attribute( XName.Get( "space", DocX.w.NamespaceName ) );
  3523. // If space is null, this border contains no space information.
  3524. if( space == null )
  3525. {
  3526. // uses default border style
  3527. }
  3528. else
  3529. {
  3530. // If space is not an int, something is wrong with this attributes value, so remove it
  3531. int borderspace;
  3532. if( !int.TryParse( space.Value, out borderspace ) )
  3533. {
  3534. space.Remove();
  3535. // uses default border style
  3536. }
  3537. else
  3538. {
  3539. b.Space = borderspace;
  3540. }
  3541. }
  3542. // The color attribute is used for the border color
  3543. XAttribute color = tcBorderType.Attribute( XName.Get( "color", DocX.w.NamespaceName ) );
  3544. if( color == null )
  3545. {
  3546. // uses default border style
  3547. }
  3548. else
  3549. {
  3550. // If color is not a Color, something is wrong with this attributes value, so remove it
  3551. try
  3552. {
  3553. b.Color = ColorTranslator.FromHtml( string.Format( "#{0}", color.Value ) );
  3554. }
  3555. catch
  3556. {
  3557. color.Remove();
  3558. // uses default border style
  3559. }
  3560. }
  3561. return b;
  3562. }
  3563. public override Table InsertTable( int rowCount, int columnCount )
  3564. {
  3565. var table = base.InsertTable( rowCount, columnCount );
  3566. table.PackagePart = this.PackagePart;
  3567. this.InsertParagraph(); //It is necessary to put paragraph in the end of the cell, without it MS-Word will say that the document is corrupted
  3568. //IMPORTANT: It will be better to check all methods that work with adding anything to cells
  3569. return table;
  3570. }
  3571. #endregion
  3572. // <summary>
  3573. // Gets or Sets the fill color of this Cell.
  3574. // </summary>
  3575. // <example>
  3576. // <code>
  3577. // // Create a new document.
  3578. // using (DocX document = DocX.Create("Test.docx"))
  3579. // {
  3580. // // Insert a table into this document.
  3581. // Table t = document.InsertTable(3, 3);
  3582. //
  3583. // // Fill the first cell as Blue.
  3584. // t.Rows[0].Cells[0].FillColor = Color.Blue;
  3585. // // Fill the middle cell as Red.
  3586. // t.Rows[1].Cells[1].FillColor = Color.Red;
  3587. // // Fill the last cell as Green.
  3588. // t.Rows[2].Cells[2].FillColor = Color.Green;
  3589. //
  3590. // // Save the document.
  3591. // document.Save();
  3592. // }
  3593. // </code>
  3594. // </example>
  3595. }
  3596. public class TableLook
  3597. {
  3598. #region Public Properties
  3599. public bool FirstRow
  3600. {
  3601. get;
  3602. set;
  3603. }
  3604. public bool LastRow
  3605. {
  3606. get;
  3607. set;
  3608. }
  3609. public bool FirstColumn
  3610. {
  3611. get;
  3612. set;
  3613. }
  3614. public bool LastColumn
  3615. {
  3616. get;
  3617. set;
  3618. }
  3619. public bool NoHorizontalBanding
  3620. {
  3621. get;
  3622. set;
  3623. }
  3624. public bool NoVerticalBanding
  3625. {
  3626. get;
  3627. set;
  3628. }
  3629. #endregion
  3630. #region Constructors
  3631. public TableLook()
  3632. {
  3633. }
  3634. public TableLook( bool firstRow, bool lastRow, bool firstColumn, bool lastColumn, bool noHorizontalBanding, bool noVerticalBanding )
  3635. {
  3636. this.FirstRow = firstRow;
  3637. this.LastRow = lastRow;
  3638. this.FirstColumn = firstColumn;
  3639. this.LastColumn = lastColumn;
  3640. this.NoHorizontalBanding = noHorizontalBanding;
  3641. this.NoVerticalBanding = noVerticalBanding;
  3642. }
  3643. #endregion
  3644. }
  3645. }