Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Table.cs 158KB

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