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 125KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using Novacode;
  7. using System.IO.Packaging;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Drawing;
  11. using System.Globalization;
  12. namespace Novacode
  13. {
  14. /// <summary>
  15. /// Represents a Table in a document.
  16. /// </summary>
  17. public class Table : InsertBeforeOrAfter
  18. {
  19. private Alignment alignment;
  20. private AutoFit autofit;
  21. /// <summary>
  22. /// Merge cells in given column starting with startRow and ending with endRow.
  23. /// </summary>
  24. /// <remarks>
  25. /// Added by arudoy patch: 11608
  26. /// </remarks>
  27. public void MergeCellsInColumn(int columnIndex, int startRow, int endRow)
  28. {
  29. // Check for valid start and end indexes.
  30. if (columnIndex < 0 || columnIndex >= ColumnCount)
  31. throw new IndexOutOfRangeException();
  32. if (startRow < 0 || endRow <= startRow || endRow >= Rows.Count)
  33. throw new IndexOutOfRangeException();
  34. // Foreach each Cell between startIndex and endIndex inclusive.
  35. foreach (Row row in Rows.Where((z, i) => i > startRow && i <= endRow))
  36. {
  37. Cell c = row.Cells[columnIndex];
  38. XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  39. if (tcPr == null)
  40. {
  41. c.Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  42. tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  43. }
  44. XElement vMerge = tcPr.Element(XName.Get("vMerge", DocX.w.NamespaceName));
  45. if (vMerge == null)
  46. {
  47. tcPr.SetElementValue(XName.Get("vMerge", DocX.w.NamespaceName), string.Empty);
  48. vMerge = tcPr.Element(XName.Get("vMerge", DocX.w.NamespaceName));
  49. }
  50. }
  51. /*
  52. * Get the tcPr (table cell properties) element for the first cell in this merge,
  53. * null will be returned if no such element exists.
  54. */
  55. XElement start_tcPr = Rows[startRow].Cells[columnIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  56. if (start_tcPr == null)
  57. {
  58. Rows[startRow].Cells[columnIndex].Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  59. start_tcPr = Rows[startRow].Cells[columnIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  60. }
  61. /*
  62. * Get the gridSpan element of this row,
  63. * null will be returned if no such element exists.
  64. */
  65. XElement start_vMerge = start_tcPr.Element(XName.Get("vMerge", DocX.w.NamespaceName));
  66. if (start_vMerge == null)
  67. {
  68. start_tcPr.SetElementValue(XName.Get("vMerge", DocX.w.NamespaceName), string.Empty);
  69. start_vMerge = start_tcPr.Element(XName.Get("vMerge", DocX.w.NamespaceName));
  70. }
  71. start_vMerge.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), "restart");
  72. }
  73. /// <summary>
  74. /// Returns a list of all Paragraphs inside this container.
  75. /// </summary>
  76. ///
  77. public virtual List<Paragraph> Paragraphs
  78. {
  79. get
  80. {
  81. List<Paragraph> paragraphs = new List<Paragraph>();
  82. foreach (Row r in Rows)
  83. paragraphs.AddRange(r.Paragraphs);
  84. return paragraphs;
  85. }
  86. }
  87. /// <summary>
  88. /// Returns a list of all Pictures in a Table.
  89. /// </summary>
  90. /// <example>
  91. /// Returns a list of all Pictures in a Table.
  92. /// <code>
  93. /// // Create a document.
  94. /// using (DocX document = DocX.Load(@"Test.docx"))
  95. /// {
  96. /// // Get the first Table in a document.
  97. /// Table t = document.Tables[0];
  98. ///
  99. /// // Get all of the Pictures in this Table.
  100. /// List<Picture> pictures = t.Pictures;
  101. ///
  102. /// // Save this document.
  103. /// document.Save();
  104. /// }
  105. /// </code>
  106. /// </example>
  107. public List<Picture> Pictures
  108. {
  109. get
  110. {
  111. List<Picture> pictures = new List<Picture>();
  112. foreach (Row r in Rows)
  113. pictures.AddRange(r.Pictures);
  114. return pictures;
  115. }
  116. }
  117. /// <summary>
  118. /// Set the direction of all content in this Table.
  119. /// </summary>
  120. /// <param name="direction">(Left to Right) or (Right to Left)</param>
  121. /// <example>
  122. /// Set the content direction for all content in a table to RightToLeft.
  123. /// <code>
  124. /// // Load a document.
  125. /// using (DocX document = DocX.Load(@"Test.docx"))
  126. /// {
  127. /// // Get the first table in a document.
  128. /// Table table = document.Tables[0];
  129. ///
  130. /// // Set the content direction for all content in this table to RightToLeft.
  131. /// table.SetDirection(Direction.RightToLeft);
  132. ///
  133. /// // Save all changes made to this document.
  134. /// document.Save();
  135. /// }
  136. /// </code>
  137. /// </example>
  138. public void SetDirection(Direction direction)
  139. {
  140. XElement tblPr = GetOrCreate_tblPr();
  141. tblPr.Add(new XElement(DocX.w + "bidiVisual"));
  142. foreach (Row r in Rows)
  143. r.SetDirection(direction);
  144. }
  145. /// <summary>
  146. /// Get all of the Hyperlinks in this Table.
  147. /// </summary>
  148. /// <example>
  149. /// Get all of the Hyperlinks in this Table.
  150. /// <code>
  151. /// // Create a document.
  152. /// using (DocX document = DocX.Load(@"Test.docx"))
  153. /// {
  154. /// // Get the first Table in this document.
  155. /// Table t = document.Tables[0];
  156. ///
  157. /// // Get a list of all Hyperlinks in this Table.
  158. /// List<Hyperlink> hyperlinks = t.Hyperlinks;
  159. ///
  160. /// // Save this document.
  161. /// document.Save();
  162. /// }
  163. /// </code>
  164. /// </example>
  165. public List<Hyperlink> Hyperlinks
  166. {
  167. get
  168. {
  169. List<Hyperlink> hyperlinks = new List<Hyperlink>();
  170. foreach (Row r in Rows)
  171. hyperlinks.AddRange(r.Hyperlinks);
  172. return hyperlinks;
  173. }
  174. }
  175. /// <summary>
  176. /// If the tblPr element doesent exist it is created, either way it is returned by this function.
  177. /// </summary>
  178. /// <returns>The tblPr element for this Table.</returns>
  179. internal XElement GetOrCreate_tblPr()
  180. {
  181. // Get the element.
  182. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  183. // If it dosen't exist, create it.
  184. if (tblPr == null)
  185. {
  186. Xml.AddFirst(new XElement(XName.Get("tblPr", DocX.w.NamespaceName)));
  187. tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  188. }
  189. // Return the pPr element for this Paragraph.
  190. return tblPr;
  191. }
  192. /// <summary>
  193. /// Returns the number of rows in this table.
  194. /// </summary>
  195. public Int32 RowCount
  196. {
  197. get
  198. {
  199. return Xml.Elements(XName.Get("tr", DocX.w.NamespaceName)).Count();
  200. }
  201. }
  202. /// <summary>
  203. /// Returns the number of columns in this table.
  204. /// </summary>
  205. public Int32 ColumnCount
  206. {
  207. get
  208. {
  209. if (RowCount == 0)
  210. return 0;
  211. return Rows.First().ColumnCount;
  212. }
  213. }
  214. /// <summary>
  215. /// Returns a list of rows in this table.
  216. /// </summary>
  217. public List<Row> Rows
  218. {
  219. get
  220. {
  221. List<Row> rows =
  222. (
  223. from r in Xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
  224. select new Row(this, Document, r)
  225. ).ToList();
  226. return rows;
  227. }
  228. }
  229. private TableDesign design;
  230. internal PackagePart mainPart;
  231. internal Table(DocX document, XElement xml)
  232. : base(document, xml)
  233. {
  234. autofit = AutoFit.ColumnWidth;
  235. this.Xml = xml;
  236. XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  237. XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  238. if (style != null)
  239. {
  240. XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  241. if (val != null)
  242. {
  243. try
  244. {
  245. design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));
  246. }
  247. catch (Exception)
  248. {
  249. design = TableDesign.Custom;
  250. }
  251. }
  252. else
  253. design = TableDesign.None;
  254. }
  255. else
  256. design = TableDesign.None;
  257. XElement tableLook = properties.Element(XName.Get("tblLook", DocX.w.NamespaceName));
  258. if (tableLook != null)
  259. {
  260. TableLook = new TableLook();
  261. TableLook.FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1";
  262. TableLook.LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1";
  263. TableLook.FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1";
  264. TableLook.LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1";
  265. TableLook.NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1";
  266. TableLook.NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1";
  267. }
  268. }
  269. public TableLook TableLook { get; set; }
  270. public Alignment Alignment
  271. {
  272. get { return alignment; }
  273. set
  274. {
  275. string alignmentString = string.Empty;
  276. switch (value)
  277. {
  278. case Alignment.left:
  279. {
  280. alignmentString = "left";
  281. break;
  282. }
  283. case Alignment.both:
  284. {
  285. alignmentString = "both";
  286. break;
  287. }
  288. case Alignment.right:
  289. {
  290. alignmentString = "right";
  291. break;
  292. }
  293. case Alignment.center:
  294. {
  295. alignmentString = "center";
  296. break;
  297. }
  298. }
  299. XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First();
  300. XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault();
  301. if (jc != null)
  302. jc.Remove();
  303. jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString));
  304. tblPr.Add(jc);
  305. alignment = value;
  306. }
  307. }
  308. /// <summary>
  309. /// Auto size this table according to some rule.
  310. /// </summary>
  311. /// <remarks>Added by Roger Saele, April 2012. Thank you for your contribution Roger.</remarks>
  312. public AutoFit AutoFit
  313. {
  314. get { return autofit; }
  315. set
  316. {
  317. string tableAttributeValue = string.Empty;
  318. string columnAttributeValue = string.Empty;
  319. switch (value)
  320. {
  321. case AutoFit.ColumnWidth:
  322. {
  323. tableAttributeValue = "auto";
  324. columnAttributeValue = "dxa";
  325. // Disable "Automatically resize to fit contents" option
  326. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  327. if (tblPr != null)
  328. {
  329. XElement layout = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName));
  330. if (layout == null)
  331. {
  332. tblPr.Add(new XElement(XName.Get("tblLayout", DocX.w.NamespaceName)));
  333. layout = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName));
  334. }
  335. XAttribute type = layout.Attribute(XName.Get("type", DocX.w.NamespaceName));
  336. if (type == null)
  337. {
  338. layout.Add(new XAttribute(XName.Get("type", DocX.w.NamespaceName), String.Empty));
  339. type = layout.Attribute(XName.Get("type", DocX.w.NamespaceName));
  340. }
  341. type.Value = "fixed";
  342. }
  343. break;
  344. }
  345. case AutoFit.Contents:
  346. {
  347. tableAttributeValue = columnAttributeValue = "auto";
  348. break;
  349. }
  350. case AutoFit.Window:
  351. {
  352. tableAttributeValue = columnAttributeValue = "pct";
  353. break;
  354. }
  355. }
  356. // Set table attributes
  357. var query = from d in Xml.Descendants()
  358. let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
  359. where (d.Name.LocalName == "tblW") && type != null
  360. select type;
  361. foreach (XAttribute type in query)
  362. type.Value = tableAttributeValue;
  363. // Set column attributes
  364. query = from d in Xml.Descendants()
  365. let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
  366. where (d.Name.LocalName == "tcW") && type != null
  367. select type;
  368. foreach (XAttribute type in query)
  369. type.Value = columnAttributeValue;
  370. autofit = value;
  371. }
  372. }
  373. /// <summary>
  374. /// The design\style to apply to this table.
  375. /// </summary>
  376. public TableDesign Design
  377. {
  378. get { return design; }
  379. set
  380. {
  381. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  382. XElement style = tblPr.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  383. if (style == null)
  384. {
  385. tblPr.Add(new XElement(XName.Get("tblStyle", DocX.w.NamespaceName)));
  386. style = tblPr.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  387. }
  388. XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  389. if (val == null)
  390. {
  391. style.Add(new XAttribute(XName.Get("val", DocX.w.NamespaceName), ""));
  392. val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  393. }
  394. design = value;
  395. if (design == TableDesign.None)
  396. {
  397. if (style != null)
  398. style.Remove();
  399. }
  400. switch (design)
  401. {
  402. case TableDesign.TableNormal: val.Value = "TableNormal"; break;
  403. case TableDesign.TableGrid: val.Value = "TableGrid"; break;
  404. case TableDesign.LightShading: val.Value = "LightShading"; break;
  405. case TableDesign.LightShadingAccent1: val.Value = "LightShading-Accent1"; break;
  406. case TableDesign.LightShadingAccent2: val.Value = "LightShading-Accent2"; break;
  407. case TableDesign.LightShadingAccent3: val.Value = "LightShading-Accent3"; break;
  408. case TableDesign.LightShadingAccent4: val.Value = "LightShading-Accent4"; break;
  409. case TableDesign.LightShadingAccent5: val.Value = "LightShading-Accent5"; break;
  410. case TableDesign.LightShadingAccent6: val.Value = "LightShading-Accent6"; break;
  411. case TableDesign.LightList: val.Value = "LightList"; break;
  412. case TableDesign.LightListAccent1: val.Value = "LightList-Accent1"; break;
  413. case TableDesign.LightListAccent2: val.Value = "LightList-Accent2"; break;
  414. case TableDesign.LightListAccent3: val.Value = "LightList-Accent3"; break;
  415. case TableDesign.LightListAccent4: val.Value = "LightList-Accent4"; break;
  416. case TableDesign.LightListAccent5: val.Value = "LightList-Accent5"; break;
  417. case TableDesign.LightListAccent6: val.Value = "LightList-Accent6"; break;
  418. case TableDesign.LightGrid: val.Value = "LightGrid"; break;
  419. case TableDesign.LightGridAccent1: val.Value = "LightGrid-Accent1"; break;
  420. case TableDesign.LightGridAccent2: val.Value = "LightGrid-Accent2"; break;
  421. case TableDesign.LightGridAccent3: val.Value = "LightGrid-Accent3"; break;
  422. case TableDesign.LightGridAccent4: val.Value = "LightGrid-Accent4"; break;
  423. case TableDesign.LightGridAccent5: val.Value = "LightGrid-Accent5"; break;
  424. case TableDesign.LightGridAccent6: val.Value = "LightGrid-Accent6"; break;
  425. case TableDesign.MediumShading1: val.Value = "MediumShading1"; break;
  426. case TableDesign.MediumShading1Accent1: val.Value = "MediumShading1-Accent1"; break;
  427. case TableDesign.MediumShading1Accent2: val.Value = "MediumShading1-Accent2"; break;
  428. case TableDesign.MediumShading1Accent3: val.Value = "MediumShading1-Accent3"; break;
  429. case TableDesign.MediumShading1Accent4: val.Value = "MediumShading1-Accent4"; break;
  430. case TableDesign.MediumShading1Accent5: val.Value = "MediumShading1-Accent5"; break;
  431. case TableDesign.MediumShading1Accent6: val.Value = "MediumShading1-Accent6"; break;
  432. case TableDesign.MediumShading2: val.Value = "MediumShading2"; break;
  433. case TableDesign.MediumShading2Accent1: val.Value = "MediumShading2-Accent1"; break;
  434. case TableDesign.MediumShading2Accent2: val.Value = "MediumShading2-Accent2"; break;
  435. case TableDesign.MediumShading2Accent3: val.Value = "MediumShading2-Accent3"; break;
  436. case TableDesign.MediumShading2Accent4: val.Value = "MediumShading2-Accent4"; break;
  437. case TableDesign.MediumShading2Accent5: val.Value = "MediumShading2-Accent5"; break;
  438. case TableDesign.MediumShading2Accent6: val.Value = "MediumShading2-Accent6"; break;
  439. case TableDesign.MediumList1: val.Value = "MediumList1"; break;
  440. case TableDesign.MediumList1Accent1: val.Value = "MediumList1-Accent1"; break;
  441. case TableDesign.MediumList1Accent2: val.Value = "MediumList1-Accent2"; break;
  442. case TableDesign.MediumList1Accent3: val.Value = "MediumList1-Accent3"; break;
  443. case TableDesign.MediumList1Accent4: val.Value = "MediumList1-Accent4"; break;
  444. case TableDesign.MediumList1Accent5: val.Value = "MediumList1-Accent5"; break;
  445. case TableDesign.MediumList1Accent6: val.Value = "MediumList1-Accent6"; break;
  446. case TableDesign.MediumList2: val.Value = "MediumList2"; break;
  447. case TableDesign.MediumList2Accent1: val.Value = "MediumList2-Accent1"; break;
  448. case TableDesign.MediumList2Accent2: val.Value = "MediumList2-Accent2"; break;
  449. case TableDesign.MediumList2Accent3: val.Value = "MediumList2-Accent3"; break;
  450. case TableDesign.MediumList2Accent4: val.Value = "MediumList2-Accent4"; break;
  451. case TableDesign.MediumList2Accent5: val.Value = "MediumList2-Accent5"; break;
  452. case TableDesign.MediumList2Accent6: val.Value = "MediumList2-Accent6"; break;
  453. case TableDesign.MediumGrid1: val.Value = "MediumGrid1"; break;
  454. case TableDesign.MediumGrid1Accent1: val.Value = "MediumGrid1-Accent1"; break;
  455. case TableDesign.MediumGrid1Accent2: val.Value = "MediumGrid1-Accent2"; break;
  456. case TableDesign.MediumGrid1Accent3: val.Value = "MediumGrid1-Accent3"; break;
  457. case TableDesign.MediumGrid1Accent4: val.Value = "MediumGrid1-Accent4"; break;
  458. case TableDesign.MediumGrid1Accent5: val.Value = "MediumGrid1-Accent5"; break;
  459. case TableDesign.MediumGrid1Accent6: val.Value = "MediumGrid1-Accent6"; break;
  460. case TableDesign.MediumGrid2: val.Value = "MediumGrid2"; break;
  461. case TableDesign.MediumGrid2Accent1: val.Value = "MediumGrid2-Accent1"; break;
  462. case TableDesign.MediumGrid2Accent2: val.Value = "MediumGrid2-Accent2"; break;
  463. case TableDesign.MediumGrid2Accent3: val.Value = "MediumGrid2-Accent3"; break;
  464. case TableDesign.MediumGrid2Accent4: val.Value = "MediumGrid2-Accent4"; break;
  465. case TableDesign.MediumGrid2Accent5: val.Value = "MediumGrid2-Accent5"; break;
  466. case TableDesign.MediumGrid2Accent6: val.Value = "MediumGrid2-Accent6"; break;
  467. case TableDesign.MediumGrid3: val.Value = "MediumGrid3"; break;
  468. case TableDesign.MediumGrid3Accent1: val.Value = "MediumGrid3-Accent1"; break;
  469. case TableDesign.MediumGrid3Accent2: val.Value = "MediumGrid3-Accent2"; break;
  470. case TableDesign.MediumGrid3Accent3: val.Value = "MediumGrid3-Accent3"; break;
  471. case TableDesign.MediumGrid3Accent4: val.Value = "MediumGrid3-Accent4"; break;
  472. case TableDesign.MediumGrid3Accent5: val.Value = "MediumGrid3-Accent5"; break;
  473. case TableDesign.MediumGrid3Accent6: val.Value = "MediumGrid3-Accent6"; break;
  474. case TableDesign.DarkList: val.Value = "DarkList"; break;
  475. case TableDesign.DarkListAccent1: val.Value = "DarkList-Accent1"; break;
  476. case TableDesign.DarkListAccent2: val.Value = "DarkList-Accent2"; break;
  477. case TableDesign.DarkListAccent3: val.Value = "DarkList-Accent3"; break;
  478. case TableDesign.DarkListAccent4: val.Value = "DarkList-Accent4"; break;
  479. case TableDesign.DarkListAccent5: val.Value = "DarkList-Accent5"; break;
  480. case TableDesign.DarkListAccent6: val.Value = "DarkList-Accent6"; break;
  481. case TableDesign.ColorfulShading: val.Value = "ColorfulShading"; break;
  482. case TableDesign.ColorfulShadingAccent1: val.Value = "ColorfulShading-Accent1"; break;
  483. case TableDesign.ColorfulShadingAccent2: val.Value = "ColorfulShading-Accent2"; break;
  484. case TableDesign.ColorfulShadingAccent3: val.Value = "ColorfulShading-Accent3"; break;
  485. case TableDesign.ColorfulShadingAccent4: val.Value = "ColorfulShading-Accent4"; break;
  486. case TableDesign.ColorfulShadingAccent5: val.Value = "ColorfulShading-Accent5"; break;
  487. case TableDesign.ColorfulShadingAccent6: val.Value = "ColorfulShading-Accent6"; break;
  488. case TableDesign.ColorfulList: val.Value = "ColorfulList"; break;
  489. case TableDesign.ColorfulListAccent1: val.Value = "ColorfulList-Accent1"; break;
  490. case TableDesign.ColorfulListAccent2: val.Value = "ColorfulList-Accent2"; break;
  491. case TableDesign.ColorfulListAccent3: val.Value = "ColorfulList-Accent3"; break;
  492. case TableDesign.ColorfulListAccent4: val.Value = "ColorfulList-Accent4"; break;
  493. case TableDesign.ColorfulListAccent5: val.Value = "ColorfulList-Accent5"; break;
  494. case TableDesign.ColorfulListAccent6: val.Value = "ColorfulList-Accent6"; break;
  495. case TableDesign.ColorfulGrid: val.Value = "ColorfulGrid"; break;
  496. case TableDesign.ColorfulGridAccent1: val.Value = "ColorfulGrid-Accent1"; break;
  497. case TableDesign.ColorfulGridAccent2: val.Value = "ColorfulGrid-Accent2"; break;
  498. case TableDesign.ColorfulGridAccent3: val.Value = "ColorfulGrid-Accent3"; break;
  499. case TableDesign.ColorfulGridAccent4: val.Value = "ColorfulGrid-Accent4"; break;
  500. case TableDesign.ColorfulGridAccent5: val.Value = "ColorfulGrid-Accent5"; break;
  501. case TableDesign.ColorfulGridAccent6: val.Value = "ColorfulGrid-Accent6"; break;
  502. default: break;
  503. }
  504. if (Document.styles == null)
  505. {
  506. PackagePart word_styles = Document.package.GetPart(new Uri("/word/styles.xml", UriKind.Relative));
  507. using (TextReader tr = new StreamReader(word_styles.GetStream()))
  508. Document.styles = XDocument.Load(tr);
  509. }
  510. var tableStyle =
  511. (
  512. from e in Document.styles.Descendants()
  513. let styleId = e.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  514. where (styleId != null && styleId.Value == val.Value)
  515. select e
  516. ).FirstOrDefault();
  517. if (tableStyle == null)
  518. {
  519. XDocument external_style_doc = HelperFunctions.DecompressXMLResource("Novacode.Resources.styles.xml.gz");
  520. var styleElement =
  521. (
  522. from e in external_style_doc.Descendants()
  523. let styleId = e.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  524. where (styleId != null && styleId.Value == val.Value)
  525. select e
  526. ).First();
  527. Document.styles.Element(XName.Get("styles", DocX.w.NamespaceName)).Add(styleElement);
  528. }
  529. }
  530. }
  531. /// <summary>
  532. /// Returns the index of this Table.
  533. /// </summary>
  534. /// <example>
  535. /// Replace the first table in this document with a new Table.
  536. /// <code>
  537. /// // Load a document into memory.
  538. /// using (DocX document = DocX.Load(@"Test.docx"))
  539. /// {
  540. /// // Get the first Table in this document.
  541. /// Table t = document.Tables[0];
  542. ///
  543. /// // Get the character index of Table t in this document.
  544. /// int index = t.Index;
  545. ///
  546. /// // Remove Table t.
  547. /// t.Remove();
  548. ///
  549. /// // Insert a new Table at the original index of Table t.
  550. /// Table newTable = document.InsertTable(index, 4, 4);
  551. ///
  552. /// // Set the design of this new Table, so that we can see it.
  553. /// newTable.Design = TableDesign.LightShadingAccent1;
  554. ///
  555. /// // Save all changes made to the document.
  556. /// document.Save();
  557. /// } // Release this document from memory.
  558. /// </code>
  559. /// </example>
  560. public int Index
  561. {
  562. get
  563. {
  564. int index = 0;
  565. IEnumerable<XElement> previous = Xml.ElementsBeforeSelf();
  566. foreach (XElement e in previous)
  567. index += Paragraph.GetElementTextLength(e);
  568. return index;
  569. }
  570. }
  571. /// <summary>
  572. /// Remove this Table from this document.
  573. /// </summary>
  574. /// <example>
  575. /// Remove the first Table from this document.
  576. /// <code>
  577. /// // Load a document into memory.
  578. /// using (DocX document = DocX.Load(@"Test.docx"))
  579. /// {
  580. /// // Get the first Table in this document.
  581. /// Table t = d.Tables[0];
  582. ///
  583. /// // Remove this Table.
  584. /// t.Remove();
  585. ///
  586. /// // Save all changes made to the document.
  587. /// document.Save();
  588. /// } // Release this document from memory.
  589. /// </code>
  590. /// </example>
  591. public void Remove()
  592. {
  593. Xml.Remove();
  594. }
  595. /// <summary>
  596. /// Insert a row at the end of this table.
  597. /// </summary>
  598. /// <example>
  599. /// <code>
  600. /// // Load a document.
  601. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  602. /// {
  603. /// // Get the first table in this document.
  604. /// Table table = document.Tables[0];
  605. ///
  606. /// // Insert a new row at the end of this table.
  607. /// Row row = table.InsertRow();
  608. ///
  609. /// // Loop through each cell in this new row.
  610. /// foreach (Cell c in row.Cells)
  611. /// {
  612. /// // Set the text of each new cell to "Hello".
  613. /// c.Paragraphs[0].InsertText("Hello", false);
  614. /// }
  615. ///
  616. /// // Save the document to a new file.
  617. /// document.SaveAs(@"C:\Example\Test2.docx");
  618. /// }// Release this document from memory.
  619. /// </code>
  620. /// </example>
  621. /// <returns>A new row.</returns>
  622. public Row InsertRow()
  623. {
  624. return InsertRow(RowCount);
  625. }
  626. /// <summary>
  627. /// Insert a copy of a row at the end of this table.
  628. /// </summary>
  629. /// <returns>A new row.</returns>
  630. public Row InsertRow(Row row)
  631. {
  632. return InsertRow(row, RowCount);
  633. }
  634. /// <summary>
  635. /// Insert a column to the right of a Table.
  636. /// </summary>
  637. /// <example>
  638. /// <code>
  639. /// // Load a document.
  640. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  641. /// {
  642. /// // Get the first Table in this document.
  643. /// Table table = document.Tables[0];
  644. ///
  645. /// // Insert a new column to this right of this table.
  646. /// table.InsertColumn();
  647. ///
  648. /// // Set the new columns text to "Row no."
  649. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  650. ///
  651. /// // Loop through each row in the table.
  652. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  653. /// {
  654. /// // The current row.
  655. /// Row row = table.Rows[i];
  656. ///
  657. /// // The cell in this row that belongs to the new column.
  658. /// Cell cell = row.Cells[table.ColumnCount - 1];
  659. ///
  660. /// // The first Paragraph that this cell houses.
  661. /// Paragraph p = cell.Paragraphs[0];
  662. ///
  663. /// // Insert this rows index.
  664. /// p.InsertText(i.ToString(), false);
  665. /// }
  666. ///
  667. /// document.Save();
  668. /// }// Release this document from memory.
  669. /// </code>
  670. /// </example>
  671. public void InsertColumn()
  672. {
  673. InsertColumn(ColumnCount);
  674. }
  675. /// <summary>
  676. /// Remove the last row from this Table.
  677. /// </summary>
  678. /// <example>
  679. /// Remove the last row from a Table.
  680. /// <code>
  681. /// // Load a document.
  682. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  683. /// {
  684. /// // Get the first table in this document.
  685. /// Table table = document.Tables[0];
  686. ///
  687. /// // Remove the last row from this table.
  688. /// table.RemoveRow();
  689. ///
  690. /// // Save the document.
  691. /// document.Save();
  692. /// }// Release this document from memory.
  693. /// </code>
  694. /// </example>
  695. public void RemoveRow()
  696. {
  697. RemoveRow(RowCount - 1);
  698. }
  699. /// <summary>
  700. /// Remove a row from this Table.
  701. /// </summary>
  702. /// <param name="index">The row to remove.</param>
  703. /// <example>
  704. /// Remove the first row from a Table.
  705. /// <code>
  706. /// // Load a document.
  707. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  708. /// {
  709. /// // Get the first table in this document.
  710. /// Table table = document.Tables[0];
  711. ///
  712. /// // Remove the first row from this table.
  713. /// table.RemoveRow(0);
  714. ///
  715. /// // Save the document.
  716. /// document.Save();
  717. /// }// Release this document from memory.
  718. /// </code>
  719. /// </example>
  720. public void RemoveRow(int index)
  721. {
  722. if (index < 0 || index > RowCount - 1)
  723. throw new IndexOutOfRangeException();
  724. Rows[index].Xml.Remove();
  725. if (Rows.Count == 0)
  726. Remove();
  727. }
  728. /// <summary>
  729. /// Remove the last column for this Table.
  730. /// </summary>
  731. /// <example>
  732. /// Remove the last column from a Table.
  733. /// <code>
  734. /// // Load a document.
  735. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  736. /// {
  737. /// // Get the first table in this document.
  738. /// Table table = document.Tables[0];
  739. ///
  740. /// // Remove the last column from this table.
  741. /// table.RemoveColumn();
  742. ///
  743. /// // Save the document.
  744. /// document.Save();
  745. /// }// Release this document from memory.
  746. /// </code>
  747. /// </example>
  748. public void RemoveColumn()
  749. {
  750. RemoveColumn(ColumnCount - 1);
  751. }
  752. /// <summary>
  753. /// Remove a column from this Table.
  754. /// </summary>
  755. /// <param name="index">The column to remove.</param>
  756. /// <example>
  757. /// Remove the first column from a Table.
  758. /// <code>
  759. /// // Load a document.
  760. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  761. /// {
  762. /// // Get the first table in this document.
  763. /// Table table = document.Tables[0];
  764. ///
  765. /// // Remove the first column from this table.
  766. /// table.RemoveColumn(0);
  767. ///
  768. /// // Save the document.
  769. /// document.Save();
  770. /// }// Release this document from memory.
  771. /// </code>
  772. /// </example>
  773. public void RemoveColumn(int index)
  774. {
  775. if (index < 0 || index > ColumnCount - 1)
  776. throw new IndexOutOfRangeException();
  777. foreach (Row r in Rows)
  778. r.Cells[index].Xml.Remove();
  779. }
  780. /// <summary>
  781. /// Insert a row into this table.
  782. /// </summary>
  783. /// <example>
  784. /// <code>
  785. /// // Load a document.
  786. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  787. /// {
  788. /// // Get the first table in this document.
  789. /// Table table = document.Tables[0];
  790. ///
  791. /// // Insert a new row at index 1 in this table.
  792. /// Row row = table.InsertRow(1);
  793. ///
  794. /// // Loop through each cell in this new row.
  795. /// foreach (Cell c in row.Cells)
  796. /// {
  797. /// // Set the text of each new cell to "Hello".
  798. /// c.Paragraphs[0].InsertText("Hello", false);
  799. /// }
  800. ///
  801. /// // Save the document to a new file.
  802. /// document.SaveAs(@"C:\Example\Test2.docx");
  803. /// }// Release this document from memory.
  804. /// </code>
  805. /// </example>
  806. /// <param name="index">Index to insert row at.</param>
  807. /// <returns>A new Row</returns>
  808. public Row InsertRow(int index)
  809. {
  810. if (index < 0 || index > RowCount)
  811. throw new IndexOutOfRangeException();
  812. List<XElement> content = new List<XElement>();
  813. for (int i = 0; i < ColumnCount; i++)
  814. {
  815. XElement cell = HelperFunctions.CreateTableCell();
  816. content.Add(cell);
  817. }
  818. return InsertRow(content, index);
  819. }
  820. /// <summary>
  821. /// Insert a copy of a row into this table.
  822. /// </summary>
  823. /// <param name="row">Row to copy and insert.</param>
  824. /// <param name="index">Index to insert row at.</param>
  825. /// <returns>A new Row</returns>
  826. public Row InsertRow(Row row, int index)
  827. {
  828. if (row == null)
  829. throw new ArgumentNullException("row");
  830. if (index < 0 || index > RowCount)
  831. throw new IndexOutOfRangeException();
  832. List<XElement> content = row.Xml.Elements(XName.Get("tc", DocX.w.NamespaceName)).Select(element => HelperFunctions.CloneElement(element)).ToList();
  833. return InsertRow(content, index);
  834. }
  835. private Row InsertRow(List<XElement> content, Int32 index)
  836. {
  837. Row newRow = new Row(this, Document, new XElement(XName.Get("tr", DocX.w.NamespaceName), content));
  838. XElement rowXml;
  839. if (index == Rows.Count)
  840. {
  841. rowXml = Rows.Last().Xml;
  842. rowXml.AddAfterSelf(newRow.Xml);
  843. }
  844. else
  845. {
  846. rowXml = Rows[index].Xml;
  847. rowXml.AddBeforeSelf(newRow.Xml);
  848. }
  849. return newRow;
  850. }
  851. /// <summary>
  852. /// Insert a column into a table.
  853. /// </summary>
  854. /// <param name="index">The index to insert the column at.</param>
  855. /// <example>
  856. /// Insert a column to the left of a table.
  857. /// <code>
  858. /// // Load a document.
  859. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  860. /// {
  861. /// // Get the first Table in this document.
  862. /// Table table = document.Tables[0];
  863. ///
  864. /// // Insert a new column to this left of this table.
  865. /// table.InsertColumn(0);
  866. ///
  867. /// // Set the new columns text to "Row no."
  868. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  869. ///
  870. /// // Loop through each row in the table.
  871. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  872. /// {
  873. /// // The current row.
  874. /// Row row = table.Rows[i];
  875. ///
  876. /// // The cell in this row that belongs to the new column.
  877. /// Cell cell = row.Cells[table.ColumnCount - 1];
  878. ///
  879. /// // The first Paragraph that this cell houses.
  880. /// Paragraph p = cell.Paragraphs[0];
  881. ///
  882. /// // Insert this rows index.
  883. /// p.InsertText(i.ToString(), false);
  884. /// }
  885. ///
  886. /// document.Save();
  887. /// }// Release this document from memory.
  888. /// </code>
  889. /// </example>
  890. public void InsertColumn(int index)
  891. {
  892. if (RowCount > 0)
  893. {
  894. foreach (Row r in Rows)
  895. {
  896. // create cell
  897. XElement cell = HelperFunctions.CreateTableCell();
  898. // insert cell
  899. if (r.Cells.Count == index)
  900. r.Cells[index - 1].Xml.AddAfterSelf(cell);
  901. else
  902. r.Cells[index].Xml.AddBeforeSelf(cell);
  903. }
  904. }
  905. }
  906. /// <summary>
  907. /// Insert a page break before a Table.
  908. /// </summary>
  909. /// <example>
  910. /// Insert a Table and a Paragraph into a document with a page break between them.
  911. /// <code>
  912. /// // Create a new document.
  913. /// using (DocX document = DocX.Create(@"Test.docx"))
  914. /// {
  915. /// // Insert a new Paragraph.
  916. /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
  917. ///
  918. /// // Insert a new Table.
  919. /// Table t1 = document.InsertTable(2, 2);
  920. /// t1.Design = TableDesign.LightShadingAccent1;
  921. ///
  922. /// // Insert a page break before this Table.
  923. /// t1.InsertPageBreakBeforeSelf();
  924. ///
  925. /// // Save this document.
  926. /// document.Save();
  927. /// }// Release this document from memory.
  928. /// </code>
  929. /// </example>
  930. public override void InsertPageBreakBeforeSelf()
  931. {
  932. base.InsertPageBreakBeforeSelf();
  933. }
  934. /// <summary>
  935. /// Insert a page break after a Table.
  936. /// </summary>
  937. /// <example>
  938. /// Insert a Table and a Paragraph into a document with a page break between them.
  939. /// <code>
  940. /// // Create a new document.
  941. /// using (DocX document = DocX.Create(@"Test.docx"))
  942. /// {
  943. /// // Insert a new Table.
  944. /// Table t1 = document.InsertTable(2, 2);
  945. /// t1.Design = TableDesign.LightShadingAccent1;
  946. ///
  947. /// // Insert a page break after this Table.
  948. /// t1.InsertPageBreakAfterSelf();
  949. ///
  950. /// // Insert a new Paragraph.
  951. /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
  952. ///
  953. /// // Save this document.
  954. /// document.Save();
  955. /// }// Release this document from memory.
  956. /// </code>
  957. /// </example>
  958. public override void InsertPageBreakAfterSelf()
  959. {
  960. base.InsertPageBreakAfterSelf();
  961. }
  962. /// <summary>
  963. /// Insert a new Table before this Table, this Table can be from this document or another document.
  964. /// </summary>
  965. /// <param name="t">The Table t to be inserted</param>
  966. /// <returns>A new Table inserted before this Table.</returns>
  967. /// <example>
  968. /// Insert a new Table before this Table.
  969. /// <code>
  970. /// // Place holder for a Table.
  971. /// Table t;
  972. ///
  973. /// // Load document a.
  974. /// using (DocX documentA = DocX.Load(@"a.docx"))
  975. /// {
  976. /// // Get the first Table from this document.
  977. /// t = documentA.Tables[0];
  978. /// }
  979. ///
  980. /// // Load document b.
  981. /// using (DocX documentB = DocX.Load(@"b.docx"))
  982. /// {
  983. /// // Get the first Table in document b.
  984. /// Table t2 = documentB.Tables[0];
  985. ///
  986. /// // Insert the Table from document a before this Table.
  987. /// Table newTable = t2.InsertTableBeforeSelf(t);
  988. ///
  989. /// // Save all changes made to document b.
  990. /// documentB.Save();
  991. /// }// Release this document from memory.
  992. /// </code>
  993. /// </example>
  994. public override Table InsertTableBeforeSelf(Table t)
  995. {
  996. return base.InsertTableBeforeSelf(t);
  997. }
  998. /// <summary>
  999. /// Insert a new Table into this document before this Table.
  1000. /// </summary>
  1001. /// <param name="rowCount">The number of rows this Table should have.</param>
  1002. /// <param name="columnCount">The number of columns this Table should have.</param>
  1003. /// <returns>A new Table inserted before this Table.</returns>
  1004. /// <example>
  1005. /// <code>
  1006. /// // Create a new document.
  1007. /// using (DocX document = DocX.Create(@"Test.docx"))
  1008. /// {
  1009. /// //Insert a Table into this document.
  1010. /// Table t = document.InsertTable(2, 2);
  1011. /// t.Design = TableDesign.LightShadingAccent1;
  1012. /// t.Alignment = Alignment.center;
  1013. ///
  1014. /// // Insert a new Table before this Table.
  1015. /// Table newTable = t.InsertTableBeforeSelf(2, 2);
  1016. /// newTable.Design = TableDesign.LightShadingAccent2;
  1017. /// newTable.Alignment = Alignment.center;
  1018. ///
  1019. /// // Save all changes made to this document.
  1020. /// document.Save();
  1021. /// }// Release this document from memory.
  1022. /// </code>
  1023. /// </example>
  1024. public override Table InsertTableBeforeSelf(int rowCount, int columnCount)
  1025. {
  1026. return base.InsertTableBeforeSelf(rowCount, columnCount);
  1027. }
  1028. /// <summary>
  1029. /// Insert a new Table after this Table, this Table can be from this document or another document.
  1030. /// </summary>
  1031. /// <param name="t">The Table t to be inserted</param>
  1032. /// <returns>A new Table inserted after this Table.</returns>
  1033. /// <example>
  1034. /// Insert a new Table after this Table.
  1035. /// <code>
  1036. /// // Place holder for a Table.
  1037. /// Table t;
  1038. ///
  1039. /// // Load document a.
  1040. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1041. /// {
  1042. /// // Get the first Table from this document.
  1043. /// t = documentA.Tables[0];
  1044. /// }
  1045. ///
  1046. /// // Load document b.
  1047. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1048. /// {
  1049. /// // Get the first Table in document b.
  1050. /// Table t2 = documentB.Tables[0];
  1051. ///
  1052. /// // Insert the Table from document a after this Table.
  1053. /// Table newTable = t2.InsertTableAfterSelf(t);
  1054. ///
  1055. /// // Save all changes made to document b.
  1056. /// documentB.Save();
  1057. /// }// Release this document from memory.
  1058. /// </code>
  1059. /// </example>
  1060. public override Table InsertTableAfterSelf(Table t)
  1061. {
  1062. return base.InsertTableAfterSelf(t);
  1063. }
  1064. /// <summary>
  1065. /// Insert a new Table into this document after this Table.
  1066. /// </summary>
  1067. /// <param name="rowCount">The number of rows this Table should have.</param>
  1068. /// <param name="columnCount">The number of columns this Table should have.</param>
  1069. /// <returns>A new Table inserted before this Table.</returns>
  1070. /// <example>
  1071. /// <code>
  1072. /// // Create a new document.
  1073. /// using (DocX document = DocX.Create(@"Test.docx"))
  1074. /// {
  1075. /// //Insert a Table into this document.
  1076. /// Table t = document.InsertTable(2, 2);
  1077. /// t.Design = TableDesign.LightShadingAccent1;
  1078. /// t.Alignment = Alignment.center;
  1079. ///
  1080. /// // Insert a new Table after this Table.
  1081. /// Table newTable = t.InsertTableAfterSelf(2, 2);
  1082. /// newTable.Design = TableDesign.LightShadingAccent2;
  1083. /// newTable.Alignment = Alignment.center;
  1084. ///
  1085. /// // Save all changes made to this document.
  1086. /// document.Save();
  1087. /// }// Release this document from memory.
  1088. /// </code>
  1089. /// </example>
  1090. public override Table InsertTableAfterSelf(int rowCount, int columnCount)
  1091. {
  1092. return base.InsertTableAfterSelf(rowCount, columnCount);
  1093. }
  1094. /// <summary>
  1095. /// Insert a Paragraph before this Table, this Paragraph may have come from the same or another document.
  1096. /// </summary>
  1097. /// <param name="p">The Paragraph to insert.</param>
  1098. /// <returns>The Paragraph now associated with this document.</returns>
  1099. /// <example>
  1100. /// Take a Paragraph from document a, and insert it into document b before this Table.
  1101. /// <code>
  1102. /// // Place holder for a Paragraph.
  1103. /// Paragraph p;
  1104. ///
  1105. /// // Load document a.
  1106. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1107. /// {
  1108. /// // Get the first paragraph from this document.
  1109. /// p = documentA.Paragraphs[0];
  1110. /// }
  1111. ///
  1112. /// // Load document b.
  1113. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1114. /// {
  1115. /// // Get the first Table in document b.
  1116. /// Table t = documentB.Tables[0];
  1117. ///
  1118. /// // Insert the Paragraph from document a before this Table.
  1119. /// Paragraph newParagraph = t.InsertParagraphBeforeSelf(p);
  1120. ///
  1121. /// // Save all changes made to document b.
  1122. /// documentB.Save();
  1123. /// }// Release this document from memory.
  1124. /// </code>
  1125. /// </example>
  1126. public override Paragraph InsertParagraphBeforeSelf(Paragraph p)
  1127. {
  1128. return base.InsertParagraphBeforeSelf(p);
  1129. }
  1130. /// <summary>
  1131. /// Insert a new Paragraph before this Table.
  1132. /// </summary>
  1133. /// <param name="text">The initial text for this new Paragraph.</param>
  1134. /// <returns>A new Paragraph inserted before this Table.</returns>
  1135. /// <example>
  1136. /// Insert a new Paragraph before the first Table in this document.
  1137. /// <code>
  1138. /// // Create a new document.
  1139. /// using (DocX document = DocX.Create(@"Test.docx"))
  1140. /// {
  1141. /// // Insert a Table into this document.
  1142. /// Table t = document.InsertTable(2, 2);
  1143. ///
  1144. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.");
  1145. ///
  1146. /// // Save all changes made to this new document.
  1147. /// document.Save();
  1148. /// }// Release this new document form memory.
  1149. /// </code>
  1150. /// </example>
  1151. public override Paragraph InsertParagraphBeforeSelf(string text)
  1152. {
  1153. return base.InsertParagraphBeforeSelf(text);
  1154. }
  1155. /// <summary>
  1156. /// Insert a new Paragraph before this Table.
  1157. /// </summary>
  1158. /// <param name="text">The initial text for this new Paragraph.</param>
  1159. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1160. /// <returns>A new Paragraph inserted before this Table.</returns>
  1161. /// <example>
  1162. /// Insert a new paragraph before the first Table in this document.
  1163. /// <code>
  1164. /// // Create a new document.
  1165. /// using (DocX document = DocX.Create(@"Test.docx"))
  1166. /// {
  1167. /// // Insert a Table into this document.
  1168. /// Table t = document.InsertTable(2, 2);
  1169. ///
  1170. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false);
  1171. ///
  1172. /// // Save all changes made to this new document.
  1173. /// document.Save();
  1174. /// }// Release this new document form memory.
  1175. /// </code>
  1176. /// </example>
  1177. public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
  1178. {
  1179. return base.InsertParagraphBeforeSelf(text, trackChanges);
  1180. }
  1181. /// <summary>
  1182. /// Insert a new Paragraph before this Table.
  1183. /// </summary>
  1184. /// <param name="text">The initial text for this new Paragraph.</param>
  1185. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1186. /// <param name="formatting">The formatting to apply to this insertion.</param>
  1187. /// <returns>A new Paragraph inserted before this Table.</returns>
  1188. /// <example>
  1189. /// Insert a new paragraph before the first Table in this document.
  1190. /// <code>
  1191. /// // Create a new document.
  1192. /// using (DocX document = DocX.Create(@"Test.docx"))
  1193. /// {
  1194. /// // Insert a Table into this document.
  1195. /// Table t = document.InsertTable(2, 2);
  1196. ///
  1197. /// Formatting boldFormatting = new Formatting();
  1198. /// boldFormatting.Bold = true;
  1199. ///
  1200. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting);
  1201. ///
  1202. /// // Save all changes made to this new document.
  1203. /// document.Save();
  1204. /// }// Release this new document form memory.
  1205. /// </code>
  1206. /// </example>
  1207. public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
  1208. {
  1209. return base.InsertParagraphBeforeSelf(text, trackChanges, formatting);
  1210. }
  1211. /// <summary>
  1212. /// Insert a Paragraph after this Table, this Paragraph may have come from the same or another document.
  1213. /// </summary>
  1214. /// <param name="p">The Paragraph to insert.</param>
  1215. /// <returns>The Paragraph now associated with this document.</returns>
  1216. /// <example>
  1217. /// Take a Paragraph from document a, and insert it into document b after this Table.
  1218. /// <code>
  1219. /// // Place holder for a Paragraph.
  1220. /// Paragraph p;
  1221. ///
  1222. /// // Load document a.
  1223. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1224. /// {
  1225. /// // Get the first paragraph from this document.
  1226. /// p = documentA.Paragraphs[0];
  1227. /// }
  1228. ///
  1229. /// // Load document b.
  1230. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1231. /// {
  1232. /// // Get the first Table in document b.
  1233. /// Table t = documentB.Tables[0];
  1234. ///
  1235. /// // Insert the Paragraph from document a after this Table.
  1236. /// Paragraph newParagraph = t.InsertParagraphAfterSelf(p);
  1237. ///
  1238. /// // Save all changes made to document b.
  1239. /// documentB.Save();
  1240. /// }// Release this document from memory.
  1241. /// </code>
  1242. /// </example>
  1243. public override Paragraph InsertParagraphAfterSelf(Paragraph p)
  1244. {
  1245. return base.InsertParagraphAfterSelf(p);
  1246. }
  1247. /// <summary>
  1248. /// Insert a new Paragraph after this Table.
  1249. /// </summary>
  1250. /// <param name="text">The initial text for this new Paragraph.</param>
  1251. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1252. /// <param name="formatting">The formatting to apply to this insertion.</param>
  1253. /// <returns>A new Paragraph inserted after this Table.</returns>
  1254. /// <example>
  1255. /// Insert a new paragraph after the first Table in this document.
  1256. /// <code>
  1257. /// // Create a new document.
  1258. /// using (DocX document = DocX.Create(@"Test.docx"))
  1259. /// {
  1260. /// // Insert a Table into this document.
  1261. /// Table t = document.InsertTable(2, 2);
  1262. ///
  1263. /// Formatting boldFormatting = new Formatting();
  1264. /// boldFormatting.Bold = true;
  1265. ///
  1266. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting);
  1267. ///
  1268. /// // Save all changes made to this new document.
  1269. /// document.Save();
  1270. /// }// Release this new document form memory.
  1271. /// </code>
  1272. /// </example>
  1273. public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
  1274. {
  1275. return base.InsertParagraphAfterSelf(text, trackChanges, formatting);
  1276. }
  1277. /// <summary>
  1278. /// Insert a new Paragraph after this Table.
  1279. /// </summary>
  1280. /// <param name="text">The initial text for this new Paragraph.</param>
  1281. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1282. /// <returns>A new Paragraph inserted after this Table.</returns>
  1283. /// <example>
  1284. /// Insert a new paragraph after the first Table in this document.
  1285. /// <code>
  1286. /// // Create a new document.
  1287. /// using (DocX document = DocX.Create(@"Test.docx"))
  1288. /// {
  1289. /// // Insert a Table into this document.
  1290. /// Table t = document.InsertTable(2, 2);
  1291. ///
  1292. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false);
  1293. ///
  1294. /// // Save all changes made to this new document.
  1295. /// document.Save();
  1296. /// }// Release this new document form memory.
  1297. /// </code>
  1298. /// </example>
  1299. public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
  1300. {
  1301. return base.InsertParagraphAfterSelf(text, trackChanges);
  1302. }
  1303. /// <summary>
  1304. /// Insert a new Paragraph after this Table.
  1305. /// </summary>
  1306. /// <param name="text">The initial text for this new Paragraph.</param>
  1307. /// <returns>A new Paragraph inserted after this Table.</returns>
  1308. /// <example>
  1309. /// Insert a new Paragraph after the first Table in this document.
  1310. /// <code>
  1311. /// // Create a new document.
  1312. /// using (DocX document = DocX.Create(@"Test.docx"))
  1313. /// {
  1314. /// // Insert a Table into this document.
  1315. /// Table t = document.InsertTable(2, 2);
  1316. ///
  1317. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.");
  1318. ///
  1319. /// // Save all changes made to this new document.
  1320. /// document.Save();
  1321. /// }// Release this new document form memory.
  1322. /// </code>
  1323. /// </example>
  1324. public override Paragraph InsertParagraphAfterSelf(string text)
  1325. {
  1326. return base.InsertParagraphAfterSelf(text);
  1327. }
  1328. /// <summary>
  1329. /// Set a table border
  1330. /// Added by lckuiper @ 20101117
  1331. /// </summary>
  1332. /// <example>
  1333. /// <code>
  1334. /// // Create a new document.
  1335. ///using (DocX document = DocX.Create("Test.docx"))
  1336. ///{
  1337. /// // Insert a table into this document.
  1338. /// Table t = document.InsertTable(3, 3);
  1339. ///
  1340. /// // Create a large blue border.
  1341. /// Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
  1342. ///
  1343. /// // Set the tables Top, Bottom, Left and Right Borders to b.
  1344. /// t.SetBorder(TableBorderType.Top, b);
  1345. /// t.SetBorder(TableBorderType.Bottom, b);
  1346. /// t.SetBorder(TableBorderType.Left, b);
  1347. /// t.SetBorder(TableBorderType.Right, b);
  1348. ///
  1349. /// // Save the document.
  1350. /// document.Save();
  1351. ///}
  1352. /// </code>
  1353. /// </example>
  1354. /// <param name="borderType">The table border to set</param>
  1355. /// <param name="border">Border object to set the table border</param>
  1356. public void SetBorder(TableBorderType borderType, Border border)
  1357. {
  1358. /*
  1359. * Get the tblPr (table properties) element for this Table,
  1360. * null will be return if no such element exists.
  1361. */
  1362. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  1363. if (tblPr == null)
  1364. {
  1365. Xml.SetElementValue(XName.Get("tblPr", DocX.w.NamespaceName), string.Empty);
  1366. tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  1367. }
  1368. /*
  1369. * Get the tblBorders (table borders) element for this Table,
  1370. * null will be return if no such element exists.
  1371. */
  1372. XElement tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
  1373. if (tblBorders == null)
  1374. {
  1375. tblPr.SetElementValue(XName.Get("tblBorders", DocX.w.NamespaceName), string.Empty);
  1376. tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
  1377. }
  1378. /*
  1379. * Get the 'borderType' (table border) element for this Table,
  1380. * null will be return if no such element exists.
  1381. */
  1382. string tbordertype;
  1383. tbordertype = borderType.ToString();
  1384. // only lower the first char of string (because of insideH and insideV)
  1385. tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);
  1386. XElement tblBorderType = tblBorders.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));
  1387. if (tblBorderType == null)
  1388. {
  1389. tblBorders.SetElementValue(XName.Get(tbordertype, DocX.w.NamespaceName), string.Empty);
  1390. tblBorderType = tblBorders.Element(XName.Get(tbordertype, DocX.w.NamespaceName));
  1391. }
  1392. // get string value of border style
  1393. string borderstyle = border.Tcbs.ToString().Substring(5);
  1394. borderstyle = borderstyle.Substring(0, 1).ToLower() + borderstyle.Substring(1);
  1395. // The val attribute is used for the border style
  1396. tblBorderType.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), borderstyle);
  1397. int size;
  1398. switch (border.Size)
  1399. {
  1400. case BorderSize.one: size = 2; break;
  1401. case BorderSize.two: size = 4; break;
  1402. case BorderSize.three: size = 6; break;
  1403. case BorderSize.four: size = 8; break;
  1404. case BorderSize.five: size = 12; break;
  1405. case BorderSize.six: size = 18; break;
  1406. case BorderSize.seven: size = 24; break;
  1407. case BorderSize.eight: size = 36; break;
  1408. case BorderSize.nine: size = 48; break;
  1409. default: size = 2; break;
  1410. }
  1411. // The sz attribute is used for the border size
  1412. tblBorderType.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), (size).ToString());
  1413. // The space attribute is used for the cell spacing (probably '0')
  1414. tblBorderType.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), (border.Space).ToString());
  1415. // The color attribute is used for the border color
  1416. tblBorderType.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), ColorTranslator.ToHtml(border.Color));
  1417. }
  1418. /// <summary>
  1419. /// Get a table border
  1420. /// Added by lckuiper @ 20101117
  1421. /// </summary>
  1422. /// <param name="borderType">The table border to get</param>
  1423. public Border GetBorder(TableBorderType borderType)
  1424. {
  1425. // instance with default border values
  1426. Border b = new Border();
  1427. /*
  1428. * Get the tblPr (table properties) element for this Table,
  1429. * null will be return if no such element exists.
  1430. */
  1431. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  1432. if (tblPr == null)
  1433. {
  1434. // uses default border style
  1435. }
  1436. /*
  1437. * Get the tblBorders (table borders) element for this Table,
  1438. * null will be return if no such element exists.
  1439. */
  1440. XElement tblBorders = tblPr.Element(XName.Get("tblBorders", DocX.w.NamespaceName));
  1441. if (tblBorders == null)
  1442. {
  1443. // uses default border style
  1444. }
  1445. /*
  1446. * Get the 'borderType' (table border) element for this Table,
  1447. * null will be return if no such element exists.
  1448. */
  1449. string tbordertype;
  1450. tbordertype = borderType.ToString();
  1451. // only lower the first char of string (because of insideH and insideV)
  1452. tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);
  1453. XElement tblBorderType = tblBorders.Element(XName.Get(tbordertype, DocX.w.NamespaceName));
  1454. if (tblBorderType == null)
  1455. {
  1456. // uses default border style
  1457. }
  1458. // The val attribute is used for the border style
  1459. XAttribute val = tblBorderType.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1460. // If val is null, this table contains no border information.
  1461. if (val == null)
  1462. {
  1463. // uses default border style
  1464. }
  1465. else
  1466. {
  1467. try
  1468. {
  1469. string bordertype = "Tcbs_" + val.Value;
  1470. b.Tcbs = (BorderStyle)Enum.Parse(typeof(BorderStyle), bordertype);
  1471. }
  1472. catch
  1473. {
  1474. val.Remove();
  1475. // uses default border style
  1476. }
  1477. }
  1478. // The sz attribute is used for the border size
  1479. XAttribute sz = tblBorderType.Attribute(XName.Get("sz", DocX.w.NamespaceName));
  1480. // If sz is null, this border contains no size information.
  1481. if (sz == null)
  1482. {
  1483. // uses default border style
  1484. }
  1485. else
  1486. {
  1487. // If sz is not an int, something is wrong with this attributes value, so remove it
  1488. int numerical_size;
  1489. if (!int.TryParse(sz.Value, out numerical_size))
  1490. sz.Remove();
  1491. else
  1492. {
  1493. switch (numerical_size)
  1494. {
  1495. case 2: b.Size = BorderSize.one; break;
  1496. case 4: b.Size = BorderSize.two; break;
  1497. case 6: b.Size = BorderSize.three; break;
  1498. case 8: b.Size = BorderSize.four; break;
  1499. case 12: b.Size = BorderSize.five; break;
  1500. case 18: b.Size = BorderSize.six; break;
  1501. case 24: b.Size = BorderSize.seven; break;
  1502. case 36: b.Size = BorderSize.eight; break;
  1503. case 48: b.Size = BorderSize.nine; break;
  1504. default: b.Size = BorderSize.one; break;
  1505. }
  1506. }
  1507. }
  1508. // The space attribute is used for the border spacing (probably '0')
  1509. XAttribute space = tblBorderType.Attribute(XName.Get("space", DocX.w.NamespaceName));
  1510. // If space is null, this border contains no space information.
  1511. if (space == null)
  1512. {
  1513. // uses default border style
  1514. }
  1515. else
  1516. {
  1517. // If space is not an int, something is wrong with this attributes value, so remove it
  1518. int borderspace;
  1519. if (!int.TryParse(space.Value, out borderspace))
  1520. {
  1521. space.Remove();
  1522. // uses default border style
  1523. }
  1524. else
  1525. {
  1526. b.Space = borderspace;
  1527. }
  1528. }
  1529. // The color attribute is used for the border color
  1530. XAttribute color = tblBorderType.Attribute(XName.Get("color", DocX.w.NamespaceName));
  1531. if (color == null)
  1532. {
  1533. // uses default border style
  1534. }
  1535. else
  1536. {
  1537. // If color is not a Color, something is wrong with this attributes value, so remove it
  1538. try
  1539. {
  1540. b.Color = ColorTranslator.FromHtml(color.Value);
  1541. }
  1542. catch
  1543. {
  1544. color.Remove();
  1545. // uses default border style
  1546. }
  1547. }
  1548. return b;
  1549. }
  1550. }
  1551. /// <summary>
  1552. /// Represents a single row in a Table.
  1553. /// </summary>
  1554. public class Row : Container
  1555. {
  1556. /// <summary>
  1557. /// Calculates columns count in the row, taking spanned cells into account
  1558. /// </summary>
  1559. public Int32 ColumnCount
  1560. {
  1561. get
  1562. {
  1563. int gridSpanSum = 0;
  1564. // Foreach each Cell between startIndex and endIndex inclusive.
  1565. foreach (Cell c in Cells)
  1566. {
  1567. XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1568. if (tcPr != null)
  1569. {
  1570. XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1571. if (gridSpan != null)
  1572. {
  1573. XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1574. int value = 0;
  1575. if (val != null)
  1576. if (int.TryParse(val.Value, out value))
  1577. gridSpanSum += value - 1;
  1578. }
  1579. }
  1580. }
  1581. // return cells count + count of spanned cells
  1582. return Cells.Count + gridSpanSum;
  1583. }
  1584. }
  1585. /// <summary>
  1586. /// A list of Cells in this Row.
  1587. /// </summary>
  1588. public List<Cell> Cells
  1589. {
  1590. get
  1591. {
  1592. List<Cell> cells =
  1593. (
  1594. from c in Xml.Elements(XName.Get("tc", DocX.w.NamespaceName))
  1595. select new Cell(this, Document, c)
  1596. ).ToList();
  1597. return cells;
  1598. }
  1599. }
  1600. public void Remove()
  1601. {
  1602. XElement table = Xml.Parent;
  1603. Xml.Remove();
  1604. if (table.Elements(XName.Get("tr", DocX.w.NamespaceName)).Count() == 0)
  1605. table.Remove();
  1606. }
  1607. public override List<Paragraph> Paragraphs
  1608. {
  1609. get
  1610. {
  1611. List<Paragraph> paragraphs =
  1612. (
  1613. from p in Xml.Descendants(DocX.w + "p")
  1614. select new Paragraph(Document, p, 0)
  1615. ).ToList();
  1616. foreach (Paragraph p in paragraphs)
  1617. p.PackagePart = table.mainPart;
  1618. return paragraphs;
  1619. }
  1620. }
  1621. internal Table table;
  1622. internal PackagePart mainPart;
  1623. internal Row(Table table, DocX document, XElement xml)
  1624. : base(document, xml)
  1625. {
  1626. this.table = table;
  1627. this.mainPart = table.mainPart;
  1628. }
  1629. /// <summary>
  1630. /// Height in pixels. // Added by Joel, refactored by Cathal.
  1631. /// </summary>
  1632. public double Height
  1633. {
  1634. get
  1635. {
  1636. /*
  1637. * Get the trPr (table row properties) element for this Row,
  1638. * null will be return if no such element exists.
  1639. */
  1640. XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
  1641. // If trPr is null, this row contains no height information.
  1642. if (trPr == null)
  1643. return double.NaN;
  1644. /*
  1645. * Get the trHeight element for this Row,
  1646. * null will be return if no such element exists.
  1647. */
  1648. XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
  1649. // If trHeight is null, this row contains no height information.
  1650. if (trHeight == null)
  1651. return double.NaN;
  1652. // Get the val attribute for this trHeight element.
  1653. XAttribute val = trHeight.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1654. // If w is null, this cell contains no width information.
  1655. if (val == null)
  1656. return double.NaN;
  1657. // If val is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  1658. double heightInWordUnits;
  1659. if (!double.TryParse(val.Value, out heightInWordUnits))
  1660. {
  1661. val.Remove();
  1662. return double.NaN;
  1663. }
  1664. // 15 "word units" in one pixel
  1665. return (heightInWordUnits / 15);
  1666. }
  1667. set
  1668. {
  1669. /*
  1670. * Get the trPr (table row properties) element for this Row,
  1671. * null will be return if no such element exists.
  1672. */
  1673. XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
  1674. if (trPr == null)
  1675. {
  1676. Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
  1677. trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
  1678. }
  1679. /*
  1680. * Get the trHeight element for this Row,
  1681. * null will be return if no such element exists.
  1682. */
  1683. XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
  1684. if (trHeight == null)
  1685. {
  1686. trPr.SetElementValue(XName.Get("trHeight", DocX.w.NamespaceName), string.Empty);
  1687. trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
  1688. }
  1689. // The hRule attribute needs to be set to exact.
  1690. trHeight.SetAttributeValue(XName.Get("hRule", DocX.w.NamespaceName), "exact");
  1691. // 15 "word units" is equal to one pixel.
  1692. trHeight.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), (value * 15).ToString());
  1693. }
  1694. }
  1695. /// <summary>
  1696. /// Merge cells starting with startIndex and ending with endIndex.
  1697. /// </summary>
  1698. public void MergeCells(int startIndex, int endIndex)
  1699. {
  1700. // Check for valid start and end indexes.
  1701. if (startIndex < 0 || endIndex <= startIndex || endIndex > Cells.Count + 1)
  1702. throw new IndexOutOfRangeException();
  1703. // The sum of all merged gridSpans.
  1704. int gridSpanSum = 0;
  1705. // Foreach each Cell between startIndex and endIndex inclusive.
  1706. foreach (Cell c in Cells.Where((z, i) => i > startIndex && i <= endIndex))
  1707. {
  1708. XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1709. if (tcPr != null)
  1710. {
  1711. XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1712. if (gridSpan != null)
  1713. {
  1714. XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1715. int value = 0;
  1716. if (val != null)
  1717. if (int.TryParse(val.Value, out value))
  1718. gridSpanSum += value - 1;
  1719. }
  1720. }
  1721. // Add this cells Pragraph to the merge start Cell.
  1722. Cells[startIndex].Xml.Add(c.Xml.Elements(XName.Get("p", DocX.w.NamespaceName)));
  1723. // Remove this Cell.
  1724. c.Xml.Remove();
  1725. }
  1726. /*
  1727. * Get the tcPr (table cell properties) element for the first cell in this merge,
  1728. * null will be returned if no such element exists.
  1729. */
  1730. XElement start_tcPr = Cells[startIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1731. if (start_tcPr == null)
  1732. {
  1733. Cells[startIndex].Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1734. start_tcPr = Cells[startIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1735. }
  1736. /*
  1737. * Get the gridSpan element of this row,
  1738. * null will be returned if no such element exists.
  1739. */
  1740. XElement start_gridSpan = start_tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1741. if (start_gridSpan == null)
  1742. {
  1743. start_tcPr.SetElementValue(XName.Get("gridSpan", DocX.w.NamespaceName), string.Empty);
  1744. start_gridSpan = start_tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1745. }
  1746. /*
  1747. * Get the val attribute of this row,
  1748. * null will be returned if no such element exists.
  1749. */
  1750. XAttribute start_val = start_gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1751. int start_value = 0;
  1752. if (start_val != null)
  1753. if (int.TryParse(start_val.Value, out start_value))
  1754. gridSpanSum += start_value - 1;
  1755. // Set the val attribute to the number of merged cells.
  1756. start_gridSpan.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), (gridSpanSum + (endIndex - startIndex + 1)).ToString());
  1757. }
  1758. }
  1759. public class Cell : Container
  1760. {
  1761. internal Row row;
  1762. internal PackagePart mainPart;
  1763. internal Cell(Row row, DocX document, XElement xml)
  1764. : base(document, xml)
  1765. {
  1766. this.row = row;
  1767. this.mainPart = row.mainPart;
  1768. }
  1769. public override List<Paragraph> Paragraphs
  1770. {
  1771. get
  1772. {
  1773. List<Paragraph> paragraphs = base.Paragraphs;
  1774. foreach (Paragraph p in paragraphs)
  1775. p.PackagePart = row.table.mainPart;
  1776. return paragraphs;
  1777. }
  1778. }
  1779. /// <summary>
  1780. /// Gets or Sets this Cells vertical alignment.
  1781. /// </summary>
  1782. /// <!--Patch 7398 added by lckuiper on Nov 16th 2010 @ 2:23 PM-->
  1783. /// <example>
  1784. /// Creates a table with 3 cells and sets the vertical alignment of each to 1 of the 3 available options.
  1785. /// <code>
  1786. ///// Create a new document.
  1787. ///using(DocX document = DocX.Create("Test.docx"))
  1788. ///{
  1789. /// // Insert a Table into this document.
  1790. /// Table t = document.InsertTable(3, 1);
  1791. ///
  1792. /// // Set the design of the Table such that we can easily identify cell boundaries.
  1793. /// t.Design = TableDesign.TableGrid;
  1794. ///
  1795. /// // Set the height of the row bigger than default.
  1796. /// // We need to be able to see the difference in vertical cell alignment options.
  1797. /// t.Rows[0].Height = 100;
  1798. ///
  1799. /// // Set the vertical alignment of cell0 to top.
  1800. /// Cell c0 = t.Rows[0].Cells[0];
  1801. /// c0.InsertParagraph("VerticalAlignment.Top");
  1802. /// c0.VerticalAlignment = VerticalAlignment.Top;
  1803. ///
  1804. /// // Set the vertical alignment of cell1 to center.
  1805. /// Cell c1 = t.Rows[0].Cells[1];
  1806. /// c1.InsertParagraph("VerticalAlignment.Center");
  1807. /// c1.VerticalAlignment = VerticalAlignment.Center;
  1808. ///
  1809. /// // Set the vertical alignment of cell2 to bottom.
  1810. /// Cell c2 = t.Rows[0].Cells[2];
  1811. /// c2.InsertParagraph("VerticalAlignment.Bottom");
  1812. /// c2.VerticalAlignment = VerticalAlignment.Bottom;
  1813. ///
  1814. /// // Save the document.
  1815. /// document.Save();
  1816. ///}
  1817. /// </code>
  1818. /// </example>
  1819. public VerticalAlignment VerticalAlignment
  1820. {
  1821. get
  1822. {
  1823. /*
  1824. * Get the tcPr (table cell properties) element for this Cell,
  1825. * null will be return if no such element exists.
  1826. */
  1827. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1828. // If tcPr is null, this cell contains no width information.
  1829. if (tcPr == null)
  1830. return VerticalAlignment.Center;
  1831. /*
  1832. * Get the vAlign (table cell vertical alignment) element for this Cell,
  1833. * null will be return if no such element exists.
  1834. */
  1835. XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
  1836. // If vAlign is null, this cell contains no vertical alignment information.
  1837. if (vAlign == null)
  1838. return VerticalAlignment.Center;
  1839. // Get the val attribute of the vAlign element.
  1840. XAttribute val = vAlign.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1841. // If val is null, this cell contains no vAlign information.
  1842. if (val == null)
  1843. return VerticalAlignment.Center;
  1844. // If val is not a VerticalAlign enum, something is wrong with this attributes value, so remove it and return VerticalAlignment.Center;
  1845. try
  1846. {
  1847. return (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), val.Value, true);
  1848. }
  1849. catch
  1850. {
  1851. val.Remove();
  1852. return VerticalAlignment.Center;
  1853. }
  1854. }
  1855. set
  1856. {
  1857. /*
  1858. * Get the tcPr (table cell properties) element for this Cell,
  1859. * null will be return if no such element exists.
  1860. */
  1861. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1862. if (tcPr == null)
  1863. {
  1864. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1865. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1866. }
  1867. /*
  1868. * Get the vAlign (table cell vertical alignment) element for this Cell,
  1869. * null will be return if no such element exists.
  1870. */
  1871. XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
  1872. if (vAlign == null)
  1873. {
  1874. tcPr.SetElementValue(XName.Get("vAlign", DocX.w.NamespaceName), string.Empty);
  1875. vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
  1876. }
  1877. // Set the VerticalAlignment in 'val'
  1878. vAlign.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), value.ToString().ToLower());
  1879. }
  1880. }
  1881. public Color Shading
  1882. {
  1883. get
  1884. {
  1885. /*
  1886. * Get the tcPr (table cell properties) element for this Cell,
  1887. * null will be return if no such element exists.
  1888. */
  1889. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1890. // If tcPr is null, this cell contains no Color information.
  1891. if (tcPr == null)
  1892. return Color.White;
  1893. /*
  1894. * Get the shd (table shade) element for this Cell,
  1895. * null will be return if no such element exists.
  1896. */
  1897. XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  1898. // If shd is null, this cell contains no Color information.
  1899. if (shd == null)
  1900. return Color.White;
  1901. // Get the w attribute of the tcW element.
  1902. XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName));
  1903. // If fill is null, this cell contains no Color information.
  1904. if (fill == null)
  1905. return Color.White;
  1906. return ColorTranslator.FromHtml(string.Format("#{0}", fill.Value));
  1907. }
  1908. set
  1909. {
  1910. /*
  1911. * Get the tcPr (table cell properties) element for this Cell,
  1912. * null will be return if no such element exists.
  1913. */
  1914. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1915. if (tcPr == null)
  1916. {
  1917. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1918. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1919. }
  1920. /*
  1921. * Get the shd (table shade) element for this Cell,
  1922. * null will be return if no such element exists.
  1923. */
  1924. XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  1925. if (shd == null)
  1926. {
  1927. tcPr.SetElementValue(XName.Get("shd", DocX.w.NamespaceName), string.Empty);
  1928. shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  1929. }
  1930. // The val attribute needs to be set to clear
  1931. shd.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), "clear");
  1932. // The color attribute needs to be set to auto
  1933. shd.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), "auto");
  1934. // The fill attribute needs to be set to the hex for this Color.
  1935. shd.SetAttributeValue(XName.Get("fill", DocX.w.NamespaceName), value.ToHex());
  1936. }
  1937. }
  1938. /// <summary>
  1939. /// Width in pixels. // Added by Joel, refactored by Cathal
  1940. /// </summary>
  1941. public double Width
  1942. {
  1943. get
  1944. {
  1945. /*
  1946. * Get the tcPr (table cell properties) element for this Cell,
  1947. * null will be return if no such element exists.
  1948. */
  1949. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1950. // If tcPr is null, this cell contains no width information.
  1951. if (tcPr == null)
  1952. return double.NaN;
  1953. /*
  1954. * Get the tcW (table cell width) element for this Cell,
  1955. * null will be return if no such element exists.
  1956. */
  1957. XElement tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
  1958. // If tcW is null, this cell contains no width information.
  1959. if (tcW == null)
  1960. return double.NaN;
  1961. // Get the w attribute of the tcW element.
  1962. XAttribute w = tcW.Attribute(XName.Get("w", DocX.w.NamespaceName));
  1963. // If w is null, this cell contains no width information.
  1964. if (w == null)
  1965. return double.NaN;
  1966. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  1967. double widthInWordUnits;
  1968. if (!double.TryParse(w.Value, out widthInWordUnits))
  1969. {
  1970. w.Remove();
  1971. return double.NaN;
  1972. }
  1973. // 15 "word units" is equal to one pixel.
  1974. return (widthInWordUnits / 15);
  1975. }
  1976. set
  1977. {
  1978. /*
  1979. * Get the tcPr (table cell properties) element for this Cell,
  1980. * null will be return if no such element exists.
  1981. */
  1982. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1983. if (tcPr == null)
  1984. {
  1985. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1986. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1987. }
  1988. /*
  1989. * Get the tcW (table cell width) element for this Cell,
  1990. * null will be return if no such element exists.
  1991. */
  1992. XElement tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
  1993. if (tcW == null)
  1994. {
  1995. tcPr.SetElementValue(XName.Get("tcW", DocX.w.NamespaceName), string.Empty);
  1996. tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
  1997. }
  1998. // 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.
  1999. tcW.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
  2000. // 15 "word units" is equal to one pixel.
  2001. tcW.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), (value * 15).ToString());
  2002. }
  2003. }
  2004. /// <summary>
  2005. /// LeftMargin in pixels. // Added by lckuiper
  2006. /// </summary>
  2007. /// <example>
  2008. /// <code>
  2009. /// // Create a new document.
  2010. ///using (DocX document = DocX.Create("Test.docx"))
  2011. ///{
  2012. /// // Insert table into this document.
  2013. /// Table t = document.InsertTable(3, 3);
  2014. /// t.Design = TableDesign.TableGrid;
  2015. ///
  2016. /// // Get the center cell.
  2017. /// Cell center = t.Rows[1].Cells[1];
  2018. ///
  2019. /// // Insert some text so that we can see the effect of the Margins.
  2020. /// center.Paragraphs[0].Append("Center Cell");
  2021. ///
  2022. /// // Set the center cells Left, Margin to 10.
  2023. /// center.MarginLeft = 25;
  2024. ///
  2025. /// // Save the document.
  2026. /// document.Save();
  2027. ///}
  2028. /// </code>
  2029. /// </example>
  2030. public double MarginLeft
  2031. {
  2032. get
  2033. {
  2034. /*
  2035. * Get the tcPr (table cell properties) element for this Cell,
  2036. * null will be return if no such element exists.
  2037. */
  2038. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2039. // If tcPr is null, this cell contains no width information.
  2040. if (tcPr == null)
  2041. return double.NaN;
  2042. /*
  2043. * Get the tcMar
  2044. *
  2045. */
  2046. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2047. // If tcMar is null, this cell contains no margin information.
  2048. if (tcMar == null)
  2049. return double.NaN;
  2050. // Get the left (LeftMargin) element
  2051. XElement tcMarLeft = tcMar.Element(XName.Get("left", DocX.w.NamespaceName));
  2052. // If tcMarLeft is null, this cell contains no left margin information.
  2053. if (tcMarLeft == null)
  2054. return double.NaN;
  2055. // Get the w attribute of the tcMarLeft element.
  2056. XAttribute w = tcMarLeft.Attribute(XName.Get("w", DocX.w.NamespaceName));
  2057. // If w is null, this cell contains no width information.
  2058. if (w == null)
  2059. return double.NaN;
  2060. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2061. double leftMarginInWordUnits;
  2062. if (!double.TryParse(w.Value, out leftMarginInWordUnits))
  2063. {
  2064. w.Remove();
  2065. return double.NaN;
  2066. }
  2067. // 15 "word units" is equal to one pixel.
  2068. return (leftMarginInWordUnits / 15);
  2069. }
  2070. set
  2071. {
  2072. /*
  2073. * Get the tcPr (table cell properties) element for this Cell,
  2074. * null will be return if no such element exists.
  2075. */
  2076. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2077. if (tcPr == null)
  2078. {
  2079. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  2080. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2081. }
  2082. /*
  2083. * Get the tcMar (table cell margin) element for this Cell,
  2084. * null will be return if no such element exists.
  2085. */
  2086. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2087. if (tcMar == null)
  2088. {
  2089. tcPr.SetElementValue(XName.Get("tcMar", DocX.w.NamespaceName), string.Empty);
  2090. tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2091. }
  2092. /*
  2093. * Get the left (table cell left margin) element for this Cell,
  2094. * null will be return if no such element exists.
  2095. */
  2096. XElement tcMarLeft = tcMar.Element(XName.Get("left", DocX.w.NamespaceName));
  2097. if (tcMarLeft == null)
  2098. {
  2099. tcMar.SetElementValue(XName.Get("left", DocX.w.NamespaceName), string.Empty);
  2100. tcMarLeft = tcMar.Element(XName.Get("left", DocX.w.NamespaceName));
  2101. }
  2102. // 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.
  2103. tcMarLeft.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
  2104. // 15 "word units" is equal to one pixel.
  2105. tcMarLeft.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), (value * 15).ToString());
  2106. }
  2107. }
  2108. /// <summary>
  2109. /// RightMargin in pixels. // Added by lckuiper
  2110. /// </summary>
  2111. /// <example>
  2112. /// <code>
  2113. /// // Create a new document.
  2114. ///using (DocX document = DocX.Create("Test.docx"))
  2115. ///{
  2116. /// // Insert table into this document.
  2117. /// Table t = document.InsertTable(3, 3);
  2118. /// t.Design = TableDesign.TableGrid;
  2119. ///
  2120. /// // Get the center cell.
  2121. /// Cell center = t.Rows[1].Cells[1];
  2122. ///
  2123. /// // Insert some text so that we can see the effect of the Margins.
  2124. /// center.Paragraphs[0].Append("Center Cell");
  2125. ///
  2126. /// // Set the center cells Right, Margin to 10.
  2127. /// center.MarginRight = 25;
  2128. ///
  2129. /// // Save the document.
  2130. /// document.Save();
  2131. ///}
  2132. /// </code>
  2133. /// </example>
  2134. public double MarginRight
  2135. {
  2136. get
  2137. {
  2138. /*
  2139. * Get the tcPr (table cell properties) element for this Cell,
  2140. * null will be return if no such element exists.
  2141. */
  2142. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2143. // If tcPr is null, this cell contains no width information.
  2144. if (tcPr == null)
  2145. return double.NaN;
  2146. /*
  2147. * Get the tcMar
  2148. *
  2149. */
  2150. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2151. // If tcMar is null, this cell contains no margin information.
  2152. if (tcMar == null)
  2153. return double.NaN;
  2154. // Get the right (RightMargin) element
  2155. XElement tcMarRight = tcMar.Element(XName.Get("right", DocX.w.NamespaceName));
  2156. // If tcMarRight is null, this cell contains no right margin information.
  2157. if (tcMarRight == null)
  2158. return double.NaN;
  2159. // Get the w attribute of the tcMarRight element.
  2160. XAttribute w = tcMarRight.Attribute(XName.Get("w", DocX.w.NamespaceName));
  2161. // If w is null, this cell contains no width information.
  2162. if (w == null)
  2163. return double.NaN;
  2164. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2165. double rightMarginInWordUnits;
  2166. if (!double.TryParse(w.Value, out rightMarginInWordUnits))
  2167. {
  2168. w.Remove();
  2169. return double.NaN;
  2170. }
  2171. // 15 "word units" is equal to one pixel.
  2172. return (rightMarginInWordUnits / 15);
  2173. }
  2174. set
  2175. {
  2176. /*
  2177. * Get the tcPr (table cell properties) element for this Cell,
  2178. * null will be return if no such element exists.
  2179. */
  2180. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2181. if (tcPr == null)
  2182. {
  2183. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  2184. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2185. }
  2186. /*
  2187. * Get the tcMar (table cell margin) element for this Cell,
  2188. * null will be return if no such element exists.
  2189. */
  2190. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2191. if (tcMar == null)
  2192. {
  2193. tcPr.SetElementValue(XName.Get("tcMar", DocX.w.NamespaceName), string.Empty);
  2194. tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2195. }
  2196. /*
  2197. * Get the right (table cell right margin) element for this Cell,
  2198. * null will be return if no such element exists.
  2199. */
  2200. XElement tcMarRight = tcMar.Element(XName.Get("right", DocX.w.NamespaceName));
  2201. if (tcMarRight == null)
  2202. {
  2203. tcMar.SetElementValue(XName.Get("right", DocX.w.NamespaceName), string.Empty);
  2204. tcMarRight = tcMar.Element(XName.Get("right", DocX.w.NamespaceName));
  2205. }
  2206. // 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.
  2207. tcMarRight.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
  2208. // 15 "word units" is equal to one pixel.
  2209. tcMarRight.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), (value * 15).ToString());
  2210. }
  2211. }
  2212. /// <summary>
  2213. /// TopMargin in pixels. // Added by lckuiper
  2214. /// </summary>
  2215. /// <example>
  2216. /// <code>
  2217. /// // Create a new document.
  2218. ///using (DocX document = DocX.Create("Test.docx"))
  2219. ///{
  2220. /// // Insert table into this document.
  2221. /// Table t = document.InsertTable(3, 3);
  2222. /// t.Design = TableDesign.TableGrid;
  2223. ///
  2224. /// // Get the center cell.
  2225. /// Cell center = t.Rows[1].Cells[1];
  2226. ///
  2227. /// // Insert some text so that we can see the effect of the Margins.
  2228. /// center.Paragraphs[0].Append("Center Cell");
  2229. ///
  2230. /// // Set the center cells Top, Margin to 10.
  2231. /// center.MarginTop = 25;
  2232. ///
  2233. /// // Save the document.
  2234. /// document.Save();
  2235. ///}
  2236. /// </code>
  2237. /// </example>
  2238. public double MarginTop
  2239. {
  2240. get
  2241. {
  2242. /*
  2243. * Get the tcPr (table cell properties) element for this Cell,
  2244. * null will be return if no such element exists.
  2245. */
  2246. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2247. // If tcPr is null, this cell contains no width information.
  2248. if (tcPr == null)
  2249. return double.NaN;
  2250. /*
  2251. * Get the tcMar
  2252. *
  2253. */
  2254. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2255. // If tcMar is null, this cell contains no margin information.
  2256. if (tcMar == null)
  2257. return double.NaN;
  2258. // Get the top (TopMargin) element
  2259. XElement tcMarTop = tcMar.Element(XName.Get("top", DocX.w.NamespaceName));
  2260. // If tcMarTop is null, this cell contains no top margin information.
  2261. if (tcMarTop == null)
  2262. return double.NaN;
  2263. // Get the w attribute of the tcMarTop element.
  2264. XAttribute w = tcMarTop.Attribute(XName.Get("w", DocX.w.NamespaceName));
  2265. // If w is null, this cell contains no width information.
  2266. if (w == null)
  2267. return double.NaN;
  2268. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2269. double topMarginInWordUnits;
  2270. if (!double.TryParse(w.Value, out topMarginInWordUnits))
  2271. {
  2272. w.Remove();
  2273. return double.NaN;
  2274. }
  2275. // 15 "word units" is equal to one pixel.
  2276. return (topMarginInWordUnits / 15);
  2277. }
  2278. set
  2279. {
  2280. /*
  2281. * Get the tcPr (table cell properties) element for this Cell,
  2282. * null will be return if no such element exists.
  2283. */
  2284. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2285. if (tcPr == null)
  2286. {
  2287. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  2288. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2289. }
  2290. /*
  2291. * Get the tcMar (table cell margin) element for this Cell,
  2292. * null will be return if no such element exists.
  2293. */
  2294. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2295. if (tcMar == null)
  2296. {
  2297. tcPr.SetElementValue(XName.Get("tcMar", DocX.w.NamespaceName), string.Empty);
  2298. tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2299. }
  2300. /*
  2301. * Get the top (table cell top margin) element for this Cell,
  2302. * null will be return if no such element exists.
  2303. */
  2304. XElement tcMarTop = tcMar.Element(XName.Get("top", DocX.w.NamespaceName));
  2305. if (tcMarTop == null)
  2306. {
  2307. tcMar.SetElementValue(XName.Get("top", DocX.w.NamespaceName), string.Empty);
  2308. tcMarTop = tcMar.Element(XName.Get("top", DocX.w.NamespaceName));
  2309. }
  2310. // 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.
  2311. tcMarTop.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
  2312. // 15 "word units" is equal to one pixel.
  2313. tcMarTop.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), (value * 15).ToString());
  2314. }
  2315. }
  2316. /// <summary>
  2317. /// BottomMargin in pixels. // Added by lckuiper
  2318. /// </summary>
  2319. /// <example>
  2320. /// <code>
  2321. /// // Create a new document.
  2322. ///using (DocX document = DocX.Create("Test.docx"))
  2323. ///{
  2324. /// // Insert table into this document.
  2325. /// Table t = document.InsertTable(3, 3);
  2326. /// t.Design = TableDesign.TableGrid;
  2327. ///
  2328. /// // Get the center cell.
  2329. /// Cell center = t.Rows[1].Cells[1];
  2330. ///
  2331. /// // Insert some text so that we can see the effect of the Margins.
  2332. /// center.Paragraphs[0].Append("Center Cell");
  2333. ///
  2334. /// // Set the center cells Top, Margin to 10.
  2335. /// center.MarginBottom = 25;
  2336. ///
  2337. /// // Save the document.
  2338. /// document.Save();
  2339. ///}
  2340. /// </code>
  2341. /// </example>
  2342. public double MarginBottom
  2343. {
  2344. get
  2345. {
  2346. /*
  2347. * Get the tcPr (table cell properties) element for this Cell,
  2348. * null will be return if no such element exists.
  2349. */
  2350. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2351. // If tcPr is null, this cell contains no width information.
  2352. if (tcPr == null)
  2353. return double.NaN;
  2354. /*
  2355. * Get the tcMar
  2356. *
  2357. */
  2358. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2359. // If tcMar is null, this cell contains no margin information.
  2360. if (tcMar == null)
  2361. return double.NaN;
  2362. // Get the bottom (BottomMargin) element
  2363. XElement tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName));
  2364. // If tcMarBottom is null, this cell contains no bottom margin information.
  2365. if (tcMarBottom == null)
  2366. return double.NaN;
  2367. // Get the w attribute of the tcMarBottom element.
  2368. XAttribute w = tcMarBottom.Attribute(XName.Get("w", DocX.w.NamespaceName));
  2369. // If w is null, this cell contains no width information.
  2370. if (w == null)
  2371. return double.NaN;
  2372. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  2373. double bottomMarginInWordUnits;
  2374. if (!double.TryParse(w.Value, out bottomMarginInWordUnits))
  2375. {
  2376. w.Remove();
  2377. return double.NaN;
  2378. }
  2379. // 15 "word units" is equal to one pixel.
  2380. return (bottomMarginInWordUnits / 15);
  2381. }
  2382. set
  2383. {
  2384. /*
  2385. * Get the tcPr (table cell properties) element for this Cell,
  2386. * null will be return if no such element exists.
  2387. */
  2388. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2389. if (tcPr == null)
  2390. {
  2391. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  2392. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2393. }
  2394. /*
  2395. * Get the tcMar (table cell margin) element for this Cell,
  2396. * null will be return if no such element exists.
  2397. */
  2398. XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2399. if (tcMar == null)
  2400. {
  2401. tcPr.SetElementValue(XName.Get("tcMar", DocX.w.NamespaceName), string.Empty);
  2402. tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
  2403. }
  2404. /*
  2405. * Get the bottom (table cell bottom margin) element for this Cell,
  2406. * null will be return if no such element exists.
  2407. */
  2408. XElement tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName));
  2409. if (tcMarBottom == null)
  2410. {
  2411. tcMar.SetElementValue(XName.Get("bottom", DocX.w.NamespaceName), string.Empty);
  2412. tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName));
  2413. }
  2414. // 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.
  2415. tcMarBottom.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
  2416. // 15 "word units" is equal to one pixel.
  2417. tcMarBottom.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), (value * 15).ToString());
  2418. }
  2419. }
  2420. /// <summary>
  2421. /// Set the table cell border
  2422. /// Added by lckuiper @ 20101117
  2423. /// </summary>
  2424. /// <example>
  2425. /// <code>
  2426. ///// Create a new document.
  2427. ///using (DocX document = DocX.Create("Test.docx"))
  2428. ///{
  2429. /// // Insert a table into this document.
  2430. /// Table t = document.InsertTable(3, 3);
  2431. ///
  2432. /// // Get the center cell.
  2433. /// Cell center = t.Rows[1].Cells[1];
  2434. ///
  2435. /// // Create a large blue border.
  2436. /// Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
  2437. ///
  2438. /// // Set the center cells Top, Bottom, Left and Right Borders to b.
  2439. /// center.SetBorder(TableCellBorderType.Top, b);
  2440. /// center.SetBorder(TableCellBorderType.Bottom, b);
  2441. /// center.SetBorder(TableCellBorderType.Left, b);
  2442. /// center.SetBorder(TableCellBorderType.Right, b);
  2443. ///
  2444. /// // Save the document.
  2445. /// document.Save();
  2446. ///}
  2447. /// </code>
  2448. /// </example>
  2449. /// <param name="borderType">Table Cell border to set</param>
  2450. /// <param name="border">Border object to set the table cell border</param>
  2451. public void SetBorder(TableCellBorderType borderType, Border border)
  2452. {
  2453. /*
  2454. * Get the tcPr (table cell properties) element for this Cell,
  2455. * null will be return if no such element exists.
  2456. */
  2457. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2458. if (tcPr == null)
  2459. {
  2460. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  2461. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2462. }
  2463. /*
  2464. * Get the tblBorders (table cell borders) element for this Cell,
  2465. * null will be return if no such element exists.
  2466. */
  2467. XElement tcBorders = tcPr.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
  2468. if (tcBorders == null)
  2469. {
  2470. tcPr.SetElementValue(XName.Get("tcBorders", DocX.w.NamespaceName), string.Empty);
  2471. tcBorders = tcPr.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
  2472. }
  2473. /*
  2474. * Get the 'borderType' (table cell border) element for this Cell,
  2475. * null will be return if no such element exists.
  2476. */
  2477. string tcbordertype;
  2478. switch (borderType)
  2479. {
  2480. case TableCellBorderType.TopLeftToBottomRight:
  2481. tcbordertype = "tl2br";
  2482. break;
  2483. case TableCellBorderType.TopRightToBottomLeft:
  2484. tcbordertype = "tr2bl";
  2485. break;
  2486. default:
  2487. // enum to string
  2488. tcbordertype = borderType.ToString();
  2489. // only lower the first char of string (because of insideH and insideV)
  2490. tcbordertype = tcbordertype.Substring(0, 1).ToLower() + tcbordertype.Substring(1);
  2491. break;
  2492. }
  2493. XElement tcBorderType = tcBorders.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));
  2494. if (tcBorderType == null)
  2495. {
  2496. tcBorders.SetElementValue(XName.Get(tcbordertype, DocX.w.NamespaceName), string.Empty);
  2497. tcBorderType = tcBorders.Element(XName.Get(tcbordertype, DocX.w.NamespaceName));
  2498. }
  2499. // get string value of border style
  2500. string borderstyle = border.Tcbs.ToString().Substring(5);
  2501. borderstyle = borderstyle.Substring(0, 1).ToLower() + borderstyle.Substring(1);
  2502. // The val attribute is used for the border style
  2503. tcBorderType.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), borderstyle);
  2504. int size;
  2505. switch (border.Size)
  2506. {
  2507. case BorderSize.one: size = 2; break;
  2508. case BorderSize.two: size = 4; break;
  2509. case BorderSize.three: size = 6; break;
  2510. case BorderSize.four: size = 8; break;
  2511. case BorderSize.five: size = 12; break;
  2512. case BorderSize.six: size = 18; break;
  2513. case BorderSize.seven: size = 24; break;
  2514. case BorderSize.eight: size = 36; break;
  2515. case BorderSize.nine: size = 48; break;
  2516. default: size = 2; break;
  2517. }
  2518. // The sz attribute is used for the border size
  2519. tcBorderType.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), (size).ToString());
  2520. // The space attribute is used for the cell spacing (probably '0')
  2521. tcBorderType.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), (border.Space).ToString());
  2522. // The color attribute is used for the border color
  2523. tcBorderType.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), ColorTranslator.ToHtml(border.Color));
  2524. }
  2525. /// <summary>
  2526. /// Get a table cell border
  2527. /// Added by lckuiper @ 20101117
  2528. /// </summary>
  2529. /// <param name="borderType">The table cell border to get</param>
  2530. public Border GetBorder(TableCellBorderType borderType)
  2531. {
  2532. // instance with default border values
  2533. Border b = new Border();
  2534. /*
  2535. * Get the tcPr (table cell properties) element for this Cell,
  2536. * null will be return if no such element exists.
  2537. */
  2538. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2539. if (tcPr == null)
  2540. {
  2541. // uses default border style
  2542. }
  2543. /*
  2544. * Get the tcBorders (table cell borders) element for this Cell,
  2545. * null will be return if no such element exists.
  2546. */
  2547. XElement tcBorders = tcPr.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
  2548. if (tcBorders == null)
  2549. {
  2550. // uses default border style
  2551. }
  2552. /*
  2553. * Get the 'borderType' (cell border) element for this Cell,
  2554. * null will be return if no such element exists.
  2555. */
  2556. string tcbordertype;
  2557. tcbordertype = borderType.ToString();
  2558. switch (tcbordertype)
  2559. {
  2560. case "TopLeftToBottomRight":
  2561. tcbordertype = "tl2br";
  2562. break;
  2563. case "TopRightToBottomLeft":
  2564. tcbordertype = "tr2bl";
  2565. break;
  2566. default:
  2567. // only lower the first char of string (because of insideH and insideV)
  2568. tcbordertype = tcbordertype.Substring(0, 1).ToLower() + tcbordertype.Substring(1);
  2569. break;
  2570. }
  2571. XElement tcBorderType = tcBorders.Element(XName.Get(tcbordertype, DocX.w.NamespaceName));
  2572. if (tcBorderType == null)
  2573. {
  2574. // uses default border style
  2575. }
  2576. // The val attribute is used for the border style
  2577. XAttribute val = tcBorderType.Attribute(XName.Get("val", DocX.w.NamespaceName));
  2578. // If val is null, this cell contains no border information.
  2579. if (val == null)
  2580. {
  2581. // uses default border style
  2582. }
  2583. else
  2584. {
  2585. try
  2586. {
  2587. string bordertype = "Tcbs_" + val.Value;
  2588. b.Tcbs = (BorderStyle)Enum.Parse(typeof(BorderStyle), bordertype);
  2589. }
  2590. catch
  2591. {
  2592. val.Remove();
  2593. // uses default border style
  2594. }
  2595. }
  2596. // The sz attribute is used for the border size
  2597. XAttribute sz = tcBorderType.Attribute(XName.Get("sz", DocX.w.NamespaceName));
  2598. // If sz is null, this border contains no size information.
  2599. if (sz == null)
  2600. {
  2601. // uses default border style
  2602. }
  2603. else
  2604. {
  2605. // If sz is not an int, something is wrong with this attributes value, so remove it
  2606. int numerical_size;
  2607. if (!int.TryParse(sz.Value, out numerical_size))
  2608. sz.Remove();
  2609. else
  2610. {
  2611. switch (numerical_size)
  2612. {
  2613. case 2: b.Size = BorderSize.one; break;
  2614. case 4: b.Size = BorderSize.two; break;
  2615. case 6: b.Size = BorderSize.three; break;
  2616. case 8: b.Size = BorderSize.four; break;
  2617. case 12: b.Size = BorderSize.five; break;
  2618. case 18: b.Size = BorderSize.six; break;
  2619. case 24: b.Size = BorderSize.seven; break;
  2620. case 36: b.Size = BorderSize.eight; break;
  2621. case 48: b.Size = BorderSize.nine; break;
  2622. default: b.Size = BorderSize.one; break;
  2623. }
  2624. }
  2625. }
  2626. // The space attribute is used for the border spacing (probably '0')
  2627. XAttribute space = tcBorderType.Attribute(XName.Get("space", DocX.w.NamespaceName));
  2628. // If space is null, this border contains no space information.
  2629. if (space == null)
  2630. {
  2631. // uses default border style
  2632. }
  2633. else
  2634. {
  2635. // If space is not an int, something is wrong with this attributes value, so remove it
  2636. int borderspace;
  2637. if (!int.TryParse(space.Value, out borderspace))
  2638. {
  2639. space.Remove();
  2640. // uses default border style
  2641. }
  2642. else
  2643. {
  2644. b.Space = borderspace;
  2645. }
  2646. }
  2647. // The color attribute is used for the border color
  2648. XAttribute color = tcBorderType.Attribute(XName.Get("color", DocX.w.NamespaceName));
  2649. if (color == null)
  2650. {
  2651. // uses default border style
  2652. }
  2653. else
  2654. {
  2655. // If color is not a Color, something is wrong with this attributes value, so remove it
  2656. try
  2657. {
  2658. b.Color = ColorTranslator.FromHtml(color.Value);
  2659. }
  2660. catch
  2661. {
  2662. color.Remove();
  2663. // uses default border style
  2664. }
  2665. }
  2666. return b;
  2667. }
  2668. /// <summary>
  2669. /// Gets or Sets the fill color of this Cell.
  2670. /// </summary>
  2671. /// <example>
  2672. /// <code>
  2673. /// // Create a new document.
  2674. /// using (DocX document = DocX.Create("Test.docx"))
  2675. /// {
  2676. /// // Insert a table into this document.
  2677. /// Table t = document.InsertTable(3, 3);
  2678. ///
  2679. /// // Fill the first cell as Blue.
  2680. /// t.Rows[0].Cells[0].FillColor = Color.Blue;
  2681. /// // Fill the middle cell as Red.
  2682. /// t.Rows[1].Cells[1].FillColor = Color.Red;
  2683. /// // Fill the last cell as Green.
  2684. /// t.Rows[2].Cells[2].FillColor = Color.Green;
  2685. ///
  2686. /// // Save the document.
  2687. /// document.Save();
  2688. /// }
  2689. /// </code>
  2690. /// </example>
  2691. public Color FillColor
  2692. {
  2693. get
  2694. {
  2695. /*
  2696. * Get the tcPr (table cell properties) element for this Cell,
  2697. * null will be return if no such element exists.
  2698. */
  2699. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2700. if (tcPr == null)
  2701. return Color.Empty;
  2702. else
  2703. {
  2704. XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  2705. if (shd == null)
  2706. return Color.Empty;
  2707. else
  2708. {
  2709. XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName));
  2710. if (fill == null)
  2711. return Color.Empty;
  2712. else
  2713. {
  2714. int argb = Int32.Parse(fill.Value.Replace("#", ""), NumberStyles.HexNumber);
  2715. return Color.FromArgb(argb);
  2716. }
  2717. }
  2718. }
  2719. }
  2720. set
  2721. {
  2722. /*
  2723. * Get the tcPr (table cell properties) element for this Cell,
  2724. * null will be return if no such element exists.
  2725. */
  2726. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2727. if (tcPr == null)
  2728. {
  2729. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  2730. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  2731. }
  2732. /*
  2733. * Get the tcW (table cell width) element for this Cell,
  2734. * null will be return if no such element exists.
  2735. */
  2736. XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  2737. if (shd == null)
  2738. {
  2739. tcPr.SetElementValue(XName.Get("shd", DocX.w.NamespaceName), string.Empty);
  2740. shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  2741. }
  2742. shd.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), "clear");
  2743. shd.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), "auto");
  2744. shd.SetAttributeValue(XName.Get("fill", DocX.w.NamespaceName), value.ToHex());
  2745. }
  2746. }
  2747. public override Table InsertTable(int rowCount, int columnCount)
  2748. {
  2749. Table table = base.InsertTable(rowCount, columnCount);
  2750. 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
  2751. //IMPORTANT: It will be better to check all methods that work with adding anything to cells
  2752. return table;
  2753. }
  2754. }
  2755. public class TableLook
  2756. {
  2757. public bool FirstRow { get; set; }
  2758. public bool LastRow { get; set; }
  2759. public bool FirstColumn { get; set; }
  2760. public bool LastColumn { get; set; }
  2761. public bool NoHorizontalBanding { get; set; }
  2762. public bool NoVerticalBanding { get; set; }
  2763. }
  2764. }