Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Table.cs 162KB

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