You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Table.cs 159KB

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