Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Table.cs 73KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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. namespace Novacode
  12. {
  13. /// <summary>
  14. /// Represents a Table in a document.
  15. /// </summary>
  16. public class Table : InsertBeforeOrAfter
  17. {
  18. private Alignment alignment;
  19. private AutoFit autofit;
  20. private List<Row> rows;
  21. private int rowCount, columnCount;
  22. /// <summary>
  23. /// Returns a list of all Pictures in a Table.
  24. /// </summary>
  25. /// <example>
  26. /// Returns a list of all Pictures in a Table.
  27. /// <code>
  28. /// // Create a document.
  29. /// using (DocX document = DocX.Load(@"Test.docx"))
  30. /// {
  31. /// // Get the first Table in a document.
  32. /// Table t = document.Tables[0];
  33. ///
  34. /// // Get all of the Pictures in this Table.
  35. /// List<Picture> pictures = t.Pictures;
  36. ///
  37. /// // Save this document.
  38. /// document.Save();
  39. /// }
  40. /// </code>
  41. /// </example>
  42. public List<Picture> Pictures
  43. {
  44. get
  45. {
  46. List<Picture> pictures = new List<Picture>();
  47. foreach (Row r in Rows)
  48. pictures.AddRange(r.Pictures);
  49. return pictures;
  50. }
  51. }
  52. /// <summary>
  53. /// Set the direction of all content in this Table.
  54. /// </summary>
  55. /// <param name="direction">(Left to Right) or (Right to Left)</param>
  56. /// <example>
  57. /// Set the content direction for all content in a table to RightToLeft.
  58. /// <code>
  59. /// // Load a document.
  60. /// using (DocX document = DocX.Load(@"Test.docx"))
  61. /// {
  62. /// // Get the first table in a document.
  63. /// Table table = document.Tables[0];
  64. ///
  65. /// // Set the content direction for all content in this table to RightToLeft.
  66. /// table.SetDirection(Direction.RightToLeft);
  67. ///
  68. /// // Save all changes made to this document.
  69. /// document.Save();
  70. /// }
  71. /// </code>
  72. /// </example>
  73. public void SetDirection(Direction direction)
  74. {
  75. XElement tblPr = GetOrCreate_tblPr();
  76. tblPr.Add(new XElement(DocX.w + "bidiVisual"));
  77. foreach (Row r in Rows)
  78. r.SetDirection(direction);
  79. }
  80. /// <summary>
  81. /// Get all of the Hyperlinks in this Table.
  82. /// </summary>
  83. /// <example>
  84. /// Get all of the Hyperlinks in this Table.
  85. /// <code>
  86. /// // Create a document.
  87. /// using (DocX document = DocX.Load(@"Test.docx"))
  88. /// {
  89. /// // Get the first Table in this document.
  90. /// Table t = document.Tables[0];
  91. ///
  92. /// // Get a list of all Hyperlinks in this Table.
  93. /// List<Hyperlink> hyperlinks = t.Hyperlinks;
  94. ///
  95. /// // Save this document.
  96. /// document.Save();
  97. /// }
  98. /// </code>
  99. /// </example>
  100. public List<Hyperlink> Hyperlinks
  101. {
  102. get
  103. {
  104. List<Hyperlink> hyperlinks = new List<Hyperlink>();
  105. foreach (Row r in Rows)
  106. hyperlinks.AddRange(r.Hyperlinks);
  107. return hyperlinks;
  108. }
  109. }
  110. /// <summary>
  111. /// If the tblPr element doesent exist it is created, either way it is returned by this function.
  112. /// </summary>
  113. /// <returns>The tblPr element for this Table.</returns>
  114. internal XElement GetOrCreate_tblPr()
  115. {
  116. // Get the element.
  117. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  118. // If it dosen't exist, create it.
  119. if (tblPr == null)
  120. {
  121. Xml.AddFirst(new XElement(XName.Get("tblPr", DocX.w.NamespaceName)));
  122. tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  123. }
  124. // Return the pPr element for this Paragraph.
  125. return tblPr;
  126. }
  127. /// <summary>
  128. /// Returns the number of rows in this table.
  129. /// </summary>
  130. public int RowCount { get { return rowCount; } }
  131. /// <summary>
  132. /// Returns the number of coloumns in this table.
  133. /// </summary>
  134. public int ColumnCount { get { return columnCount; } }
  135. /// <summary>
  136. /// Returns a list of rows in this table.
  137. /// </summary>
  138. public List<Row> Rows { get { return rows; } }
  139. private TableDesign design;
  140. internal Table(DocX document, XElement xml):base(document, xml)
  141. {
  142. autofit = AutoFit.ColoumnWidth;
  143. this.Xml = xml;
  144. XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  145. rows = (from r in xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
  146. select new Row(document, r)).ToList();
  147. rowCount = rows.Count;
  148. if (rows.Count > 0)
  149. if (rows[0].Cells.Count > 0)
  150. columnCount = rows[0].Cells.Count;
  151. XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  152. if (style != null)
  153. {
  154. XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  155. if (val != null)
  156. {
  157. try
  158. {
  159. design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));
  160. }
  161. catch (Exception e)
  162. {
  163. design = TableDesign.Custom;
  164. }
  165. }
  166. else
  167. design = TableDesign.None;
  168. }
  169. else
  170. design = TableDesign.None;
  171. }
  172. public Alignment Alignment
  173. {
  174. get { return alignment; }
  175. set
  176. {
  177. string alignmentString = string.Empty;
  178. switch (value)
  179. {
  180. case Alignment.left:
  181. {
  182. alignmentString = "left";
  183. break;
  184. }
  185. case Alignment.both:
  186. {
  187. alignmentString = "both";
  188. break;
  189. }
  190. case Alignment.right:
  191. {
  192. alignmentString = "right";
  193. break;
  194. }
  195. case Alignment.center:
  196. {
  197. alignmentString = "center";
  198. break;
  199. }
  200. }
  201. XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First();
  202. XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault();
  203. if(jc != null)
  204. jc.Remove();
  205. jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString));
  206. tblPr.Add(jc);
  207. alignment = value;
  208. }
  209. }
  210. /// <summary>
  211. /// Auto size this table according to some rule.
  212. /// </summary>
  213. public AutoFit AutoFit
  214. {
  215. get{return autofit;}
  216. set
  217. {
  218. string attributeValue = string.Empty;
  219. switch(value)
  220. {
  221. case AutoFit.ColoumnWidth:
  222. {
  223. attributeValue = "dxa";
  224. break;
  225. }
  226. case AutoFit.Contents:
  227. {
  228. attributeValue = "auto";
  229. break;
  230. }
  231. case AutoFit.Window:
  232. {
  233. attributeValue = "pct";
  234. break;
  235. }
  236. }
  237. var query = from d in Xml.Descendants()
  238. let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
  239. where (d.Name.LocalName == "tcW" || d.Name.LocalName == "tblW") && type != null
  240. select type;
  241. foreach (XAttribute type in query)
  242. type.Value = attributeValue;
  243. autofit = value;
  244. }
  245. }
  246. /// <summary>
  247. /// The design\style to apply to this table.
  248. /// </summary>
  249. public TableDesign Design
  250. {
  251. get { return design; }
  252. set
  253. {
  254. XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  255. XElement style = tblPr.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  256. if (style == null)
  257. {
  258. tblPr.Add(new XElement(XName.Get("tblStyle", DocX.w.NamespaceName)));
  259. style = tblPr.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  260. }
  261. XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  262. if(val == null)
  263. {
  264. style.Add(new XAttribute(XName.Get("val", DocX.w.NamespaceName), ""));
  265. val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  266. }
  267. design = value;
  268. if (design == TableDesign.None)
  269. {
  270. if (style != null)
  271. style.Remove();
  272. }
  273. switch (design)
  274. {
  275. case TableDesign.TableNormal: val.Value = "TableNormal"; break;
  276. case TableDesign.TableGrid: val.Value = "TableGrid"; break;
  277. case TableDesign.LightShading: val.Value = "LightShading"; break;
  278. case TableDesign.LightShadingAccent1: val.Value = "LightShading-Accent1"; break;
  279. case TableDesign.LightShadingAccent2: val.Value = "LightShading-Accent2"; break;
  280. case TableDesign.LightShadingAccent3: val.Value = "LightShading-Accent3"; break;
  281. case TableDesign.LightShadingAccent4: val.Value = "LightShading-Accent4"; break;
  282. case TableDesign.LightShadingAccent5: val.Value = "LightShading-Accent5"; break;
  283. case TableDesign.LightShadingAccent6: val.Value = "LightShading-Accent6"; break;
  284. case TableDesign.LightList: val.Value = "LightList"; break;
  285. case TableDesign.LightListAccent1: val.Value = "LightList-Accent1"; break;
  286. case TableDesign.LightListAccent2: val.Value = "LightList-Accent2"; break;
  287. case TableDesign.LightListAccent3: val.Value = "LightList-Accent3"; break;
  288. case TableDesign.LightListAccent4: val.Value = "LightList-Accent4"; break;
  289. case TableDesign.LightListAccent5: val.Value = "LightList-Accent5"; break;
  290. case TableDesign.LightListAccent6: val.Value = "LightList-Accent6"; break;
  291. case TableDesign.LightGrid: val.Value = "LightGrid"; break;
  292. case TableDesign.LightGridAccent1: val.Value = "LightGrid-Accent1"; break;
  293. case TableDesign.LightGridAccent2: val.Value = "LightGrid-Accent2"; break;
  294. case TableDesign.LightGridAccent3: val.Value = "LightGrid-Accent3"; break;
  295. case TableDesign.LightGridAccent4: val.Value = "LightGrid-Accent4"; break;
  296. case TableDesign.LightGridAccent5: val.Value = "LightGrid-Accent5"; break;
  297. case TableDesign.LightGridAccent6: val.Value = "LightGrid-Accent6"; break;
  298. case TableDesign.MediumShading1: val.Value = "MediumShading1"; break;
  299. case TableDesign.MediumShading1Accent1: val.Value = "MediumShading1-Accent1"; break;
  300. case TableDesign.MediumShading1Accent2: val.Value = "MediumShading1-Accent2"; break;
  301. case TableDesign.MediumShading1Accent3: val.Value = "MediumShading1-Accent3"; break;
  302. case TableDesign.MediumShading1Accent4: val.Value = "MediumShading1-Accent4"; break;
  303. case TableDesign.MediumShading1Accent5: val.Value = "MediumShading1-Accent5"; break;
  304. case TableDesign.MediumShading1Accent6: val.Value = "MediumShading1-Accent6"; break;
  305. case TableDesign.MediumShading2: val.Value = "MediumShading2"; break;
  306. case TableDesign.MediumShading2Accent1: val.Value = "MediumShading2-Accent1"; break;
  307. case TableDesign.MediumShading2Accent2: val.Value = "MediumShading2-Accent2"; break;
  308. case TableDesign.MediumShading2Accent3: val.Value = "MediumShading2-Accent3"; break;
  309. case TableDesign.MediumShading2Accent4: val.Value = "MediumShading2-Accent4"; break;
  310. case TableDesign.MediumShading2Accent5: val.Value = "MediumShading2-Accent5"; break;
  311. case TableDesign.MediumShading2Accent6: val.Value = "MediumShading2-Accent6"; break;
  312. case TableDesign.MediumList1: val.Value = "MediumList1"; break;
  313. case TableDesign.MediumList1Accent1: val.Value = "MediumList1-Accent1"; break;
  314. case TableDesign.MediumList1Accent2: val.Value = "MediumList1-Accent2"; break;
  315. case TableDesign.MediumList1Accent3: val.Value = "MediumList1-Accent3"; break;
  316. case TableDesign.MediumList1Accent4: val.Value = "MediumList1-Accent4"; break;
  317. case TableDesign.MediumList1Accent5: val.Value = "MediumList1-Accent5"; break;
  318. case TableDesign.MediumList1Accent6: val.Value = "MediumList1-Accent6"; break;
  319. case TableDesign.MediumList2: val.Value = "MediumList2"; break;
  320. case TableDesign.MediumList2Accent1: val.Value = "MediumList2-Accent1"; break;
  321. case TableDesign.MediumList2Accent2: val.Value = "MediumList2-Accent2"; break;
  322. case TableDesign.MediumList2Accent3: val.Value = "MediumList2-Accent3"; break;
  323. case TableDesign.MediumList2Accent4: val.Value = "MediumList2-Accent4"; break;
  324. case TableDesign.MediumList2Accent5: val.Value = "MediumList2-Accent5"; break;
  325. case TableDesign.MediumList2Accent6: val.Value = "MediumList2-Accent6"; break;
  326. case TableDesign.MediumGrid1: val.Value = "MediumGrid1"; break;
  327. case TableDesign.MediumGrid1Accent1: val.Value = "MediumGrid1-Accent1"; break;
  328. case TableDesign.MediumGrid1Accent2: val.Value = "MediumGrid1-Accent2"; break;
  329. case TableDesign.MediumGrid1Accent3: val.Value = "MediumGrid1-Accent3"; break;
  330. case TableDesign.MediumGrid1Accent4: val.Value = "MediumGrid1-Accent4"; break;
  331. case TableDesign.MediumGrid1Accent5: val.Value = "MediumGrid1-Accent5"; break;
  332. case TableDesign.MediumGrid1Accent6: val.Value = "MediumGrid1-Accent6"; break;
  333. case TableDesign.MediumGrid2: val.Value = "MediumGrid2"; break;
  334. case TableDesign.MediumGrid2Accent1: val.Value = "MediumGrid2-Accent1"; break;
  335. case TableDesign.MediumGrid2Accent2: val.Value = "MediumGrid2-Accent2"; break;
  336. case TableDesign.MediumGrid2Accent3: val.Value = "MediumGrid2-Accent3"; break;
  337. case TableDesign.MediumGrid2Accent4: val.Value = "MediumGrid2-Accent4"; break;
  338. case TableDesign.MediumGrid2Accent5: val.Value = "MediumGrid2-Accent5"; break;
  339. case TableDesign.MediumGrid2Accent6: val.Value = "MediumGrid2-Accent6"; break;
  340. case TableDesign.MediumGrid3: val.Value = "MediumGrid3"; break;
  341. case TableDesign.MediumGrid3Accent1: val.Value = "MediumGrid3-Accent1"; break;
  342. case TableDesign.MediumGrid3Accent2: val.Value = "MediumGrid3-Accent2"; break;
  343. case TableDesign.MediumGrid3Accent3: val.Value = "MediumGrid3-Accent3"; break;
  344. case TableDesign.MediumGrid3Accent4: val.Value = "MediumGrid3-Accent4"; break;
  345. case TableDesign.MediumGrid3Accent5: val.Value = "MediumGrid3-Accent5"; break;
  346. case TableDesign.MediumGrid3Accent6: val.Value = "MediumGrid3-Accent6"; break;
  347. case TableDesign.DarkList: val.Value = "DarkList"; break;
  348. case TableDesign.DarkListAccent1: val.Value = "DarkList-Accent1"; break;
  349. case TableDesign.DarkListAccent2: val.Value = "DarkList-Accent2"; break;
  350. case TableDesign.DarkListAccent3: val.Value = "DarkList-Accent3"; break;
  351. case TableDesign.DarkListAccent4: val.Value = "DarkList-Accent4"; break;
  352. case TableDesign.DarkListAccent5: val.Value = "DarkList-Accent5"; break;
  353. case TableDesign.DarkListAccent6: val.Value = "DarkList-Accent6"; break;
  354. case TableDesign.ColorfulShading: val.Value = "ColorfulShading"; break;
  355. case TableDesign.ColorfulShadingAccent1: val.Value = "ColorfulShading-Accent1"; break;
  356. case TableDesign.ColorfulShadingAccent2: val.Value = "ColorfulShading-Accent2"; break;
  357. case TableDesign.ColorfulShadingAccent3: val.Value = "ColorfulShading-Accent3"; break;
  358. case TableDesign.ColorfulShadingAccent4: val.Value = "ColorfulShading-Accent4"; break;
  359. case TableDesign.ColorfulShadingAccent5: val.Value = "ColorfulShading-Accent5"; break;
  360. case TableDesign.ColorfulShadingAccent6: val.Value = "ColorfulShading-Accent6"; break;
  361. case TableDesign.ColorfulList: val.Value = "ColorfulList"; break;
  362. case TableDesign.ColorfulListAccent1: val.Value = "ColorfulList-Accent1"; break;
  363. case TableDesign.ColorfulListAccent2: val.Value = "ColorfulList-Accent2"; break;
  364. case TableDesign.ColorfulListAccent3: val.Value = "ColorfulList-Accent3"; break;
  365. case TableDesign.ColorfulListAccent4: val.Value = "ColorfulList-Accent4"; break;
  366. case TableDesign.ColorfulListAccent5: val.Value = "ColorfulList-Accent5"; break;
  367. case TableDesign.ColorfulListAccent6: val.Value = "ColorfulList-Accent6"; break;
  368. case TableDesign.ColorfulGrid: val.Value = "ColorfulGrid"; break;
  369. case TableDesign.ColorfulGridAccent1: val.Value = "ColorfulGrid-Accent1"; break;
  370. case TableDesign.ColorfulGridAccent2: val.Value = "ColorfulGrid-Accent2"; break;
  371. case TableDesign.ColorfulGridAccent3: val.Value = "ColorfulGrid-Accent3"; break;
  372. case TableDesign.ColorfulGridAccent4: val.Value = "ColorfulGrid-Accent4"; break;
  373. case TableDesign.ColorfulGridAccent5: val.Value = "ColorfulGrid-Accent5"; break;
  374. case TableDesign.ColorfulGridAccent6: val.Value = "ColorfulGrid-Accent6"; break;
  375. default: break;
  376. }
  377. XDocument style_doc;
  378. PackagePart word_styles = Document.package.GetPart(new Uri("/word/styles.xml", UriKind.Relative));
  379. using (TextReader tr = new StreamReader(word_styles.GetStream()))
  380. style_doc = XDocument.Load(tr);
  381. var tableStyle =
  382. (
  383. from e in style_doc.Descendants()
  384. let styleId = e.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  385. where (styleId != null && styleId.Value == val.Value)
  386. select e
  387. ).FirstOrDefault();
  388. if (tableStyle == null)
  389. {
  390. XDocument external_style_doc = DocX.DecompressXMLResource("Novacode.Resources.styles.xml.gz");
  391. var styleElement =
  392. (
  393. from e in external_style_doc.Descendants()
  394. let styleId = e.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  395. where (styleId != null && styleId.Value == val.Value)
  396. select e
  397. ).First();
  398. style_doc.Element(XName.Get("styles", DocX.w.NamespaceName)).Add(styleElement);
  399. using (TextWriter tw = new StreamWriter(word_styles.GetStream(FileMode.Create)))
  400. style_doc.Save(tw, SaveOptions.None);
  401. }
  402. }
  403. }
  404. /// <summary>
  405. /// Insert a row at the end of this table.
  406. /// </summary>
  407. /// <example>
  408. /// <code>
  409. /// // Load a document.
  410. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  411. /// {
  412. /// // Get the first table in this document.
  413. /// Table table = document.Tables[0];
  414. ///
  415. /// // Insert a new row at the end of this table.
  416. /// Row row = table.InsertRow();
  417. ///
  418. /// // Loop through each cell in this new row.
  419. /// foreach (Cell c in row.Cells)
  420. /// {
  421. /// // Set the text of each new cell to "Hello".
  422. /// c.Paragraph.InsertText("Hello", false);
  423. /// }
  424. ///
  425. /// // Save the document to a new file.
  426. /// document.SaveAs(@"C:\Example\Test2.docx");
  427. /// }// Release this document from memory.
  428. /// </code>
  429. /// </example>
  430. /// <returns>A new row.</returns>
  431. public Row InsertRow()
  432. {
  433. return InsertRow(rows.Count);
  434. }
  435. /// <summary>
  436. /// Returns the index of this Table.
  437. /// </summary>
  438. /// <example>
  439. /// Replace the first table in this document with a new Table.
  440. /// <code>
  441. /// // Load a document into memory.
  442. /// using (DocX document = DocX.Load(@"Test.docx"))
  443. /// {
  444. /// // Get the first Table in this document.
  445. /// Table t = document.Tables[0];
  446. ///
  447. /// // Get the character index of Table t in this document.
  448. /// int index = t.Index;
  449. ///
  450. /// // Remove Table t.
  451. /// t.Remove();
  452. ///
  453. /// // Insert a new Table at the original index of Table t.
  454. /// Table newTable = document.InsertTable(index, 4, 4);
  455. ///
  456. /// // Set the design of this new Table, so that we can see it.
  457. /// newTable.Design = TableDesign.LightShadingAccent1;
  458. ///
  459. /// // Save all changes made to the document.
  460. /// document.Save();
  461. /// } // Release this document from memory.
  462. /// </code>
  463. /// </example>
  464. public int Index
  465. {
  466. get
  467. {
  468. int index = 0;
  469. IEnumerable<XElement> previous = Xml.ElementsBeforeSelf();
  470. foreach (XElement e in previous)
  471. index += Paragraph.GetElementTextLength(e);
  472. return index;
  473. }
  474. }
  475. /// <summary>
  476. /// Remove this Table from this document.
  477. /// </summary>
  478. /// <example>
  479. /// Remove the first Table from this document.
  480. /// <code>
  481. /// // Load a document into memory.
  482. /// using (DocX document = DocX.Load(@"Test.docx"))
  483. /// {
  484. /// // Get the first Table in this document.
  485. /// Table t = d.Tables[0];
  486. ///
  487. /// // Remove this Table.
  488. /// t.Remove();
  489. ///
  490. /// // Save all changes made to the document.
  491. /// document.Save();
  492. /// } // Release this document from memory.
  493. /// </code>
  494. /// </example>
  495. public void Remove()
  496. {
  497. Xml.Remove();
  498. }
  499. /// <summary>
  500. /// Insert a column to the right of a Table.
  501. /// </summary>
  502. /// <example>
  503. /// <code>
  504. /// // Load a document.
  505. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  506. /// {
  507. /// // Get the first Table in this document.
  508. /// Table table = document.Tables[0];
  509. ///
  510. /// // Insert a new column to this right of this table.
  511. /// table.InsertColumn();
  512. ///
  513. /// // Set the new coloumns text to "Row no."
  514. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  515. ///
  516. /// // Loop through each row in the table.
  517. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  518. /// {
  519. /// // The current row.
  520. /// Row row = table.Rows[i];
  521. ///
  522. /// // The cell in this row that belongs to the new coloumn.
  523. /// Cell cell = row.Cells[table.ColumnCount - 1];
  524. ///
  525. /// // The Paragraph that this cell houses.
  526. /// Paragraph p = cell.Paragraph;
  527. ///
  528. /// // Insert this rows index.
  529. /// p.InsertText(i.ToString(), false);
  530. /// }
  531. ///
  532. /// document.Save();
  533. /// }// Release this document from memory.
  534. /// </code>
  535. /// </example>
  536. public void InsertColumn()
  537. {
  538. InsertColumn(columnCount);
  539. }
  540. /// <summary>
  541. /// Remove the last row from this Table.
  542. /// </summary>
  543. /// <example>
  544. /// Remove the last row from a Table.
  545. /// <code>
  546. /// // Load a document.
  547. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  548. /// {
  549. /// // Get the first table in this document.
  550. /// Table table = document.Tables[0];
  551. ///
  552. /// // Remove the last row from this table.
  553. /// table.RemoveRow();
  554. ///
  555. /// // Save the document.
  556. /// document.Save();
  557. /// }// Release this document from memory.
  558. /// </code>
  559. /// </example>
  560. public void RemoveRow()
  561. {
  562. RemoveRow(rowCount - 1);
  563. }
  564. /// <summary>
  565. /// Remove a row from this Table.
  566. /// </summary>
  567. /// <param name="index">The row to remove.</param>
  568. /// <example>
  569. /// Remove the first row from a Table.
  570. /// <code>
  571. /// // Load a document.
  572. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  573. /// {
  574. /// // Get the first table in this document.
  575. /// Table table = document.Tables[0];
  576. ///
  577. /// // Remove the first row from this table.
  578. /// table.RemoveRow(0);
  579. ///
  580. /// // Save the document.
  581. /// document.Save();
  582. /// }// Release this document from memory.
  583. /// </code>
  584. /// </example>
  585. public void RemoveRow(int index)
  586. {
  587. if (index < 0 || index > rows.Count)
  588. throw new IndexOutOfRangeException();
  589. rows[index].Xml.Remove();
  590. }
  591. /// <summary>
  592. /// Remove the last column for this Table.
  593. /// </summary>
  594. /// <example>
  595. /// Remove the last column from a Table.
  596. /// <code>
  597. /// // Load a document.
  598. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  599. /// {
  600. /// // Get the first table in this document.
  601. /// Table table = document.Tables[0];
  602. ///
  603. /// // Remove the last column from this table.
  604. /// table.RemoveColumn();
  605. ///
  606. /// // Save the document.
  607. /// document.Save();
  608. /// }// Release this document from memory.
  609. /// </code>
  610. /// </example>
  611. public void RemoveColumn()
  612. {
  613. RemoveColumn(columnCount - 1);
  614. }
  615. /// <summary>
  616. /// Remove a coloumn from this Table.
  617. /// </summary>
  618. /// <param name="index">The coloumn to remove.</param>
  619. /// <example>
  620. /// Remove the first column from a Table.
  621. /// <code>
  622. /// // Load a document.
  623. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  624. /// {
  625. /// // Get the first table in this document.
  626. /// Table table = document.Tables[0];
  627. ///
  628. /// // Remove the first column from this table.
  629. /// table.RemoveColumn(0);
  630. ///
  631. /// // Save the document.
  632. /// document.Save();
  633. /// }// Release this document from memory.
  634. /// </code>
  635. /// </example>
  636. public void RemoveColumn(int index)
  637. {
  638. if (index < 0 || index > columnCount - 1)
  639. throw new IndexOutOfRangeException();
  640. foreach (Row r in rows)
  641. r.Cells[index].Xml.Remove();
  642. }
  643. /// <summary>
  644. /// Insert a row into this table.
  645. /// </summary>
  646. /// <example>
  647. /// <code>
  648. /// // Load a document.
  649. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  650. /// {
  651. /// // Get the first table in this document.
  652. /// Table table = document.Tables[0];
  653. ///
  654. /// // Insert a new row at index 1 in this table.
  655. /// Row row = table.InsertRow(1);
  656. ///
  657. /// // Loop through each cell in this new row.
  658. /// foreach (Cell c in row.Cells)
  659. /// {
  660. /// // Set the text of each new cell to "Hello".
  661. /// c.Paragraph.InsertText("Hello", false);
  662. /// }
  663. ///
  664. /// // Save the document to a new file.
  665. /// document.SaveAs(@"C:\Example\Test2.docx");
  666. /// }// Release this document from memory.
  667. /// </code>
  668. /// </example>
  669. /// <param name="index">Index to insert row at.</param>
  670. /// <returns>A new Row</returns>
  671. public Row InsertRow(int index)
  672. {
  673. if (index < 0 || index > rows.Count)
  674. throw new IndexOutOfRangeException();
  675. List<XElement> content = new List<XElement>();
  676. foreach (Cell c in rows[0].Cells)
  677. content.Add(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
  678. XElement e = new XElement(XName.Get("tr", DocX.w.NamespaceName), content);
  679. Row newRow = new Row(Document, e);
  680. XElement rowXml;
  681. if (index == rows.Count)
  682. {
  683. rowXml = rows.Last().Xml;
  684. rowXml.AddAfterSelf(newRow.Xml);
  685. }
  686. else
  687. {
  688. rowXml = rows[index].Xml;
  689. rowXml.AddBeforeSelf(newRow.Xml);
  690. }
  691. rows.Insert(index, newRow);
  692. rowCount = rows.Count;
  693. return newRow;
  694. }
  695. /// <summary>
  696. /// Insert a column into a table.
  697. /// </summary>
  698. /// <param name="index">The index to insert the column at.</param>
  699. /// <example>
  700. /// Insert a column to the left of a table.
  701. /// <code>
  702. /// // Load a document.
  703. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  704. /// {
  705. /// // Get the first Table in this document.
  706. /// Table table = document.Tables[0];
  707. ///
  708. /// // Insert a new column to this left of this table.
  709. /// table.InsertColumn(0);
  710. ///
  711. /// // Set the new coloumns text to "Row no."
  712. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  713. ///
  714. /// // Loop through each row in the table.
  715. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  716. /// {
  717. /// // The current row.
  718. /// Row row = table.Rows[i];
  719. ///
  720. /// // The cell in this row that belongs to the new coloumn.
  721. /// Cell cell = row.Cells[table.ColumnCount - 1];
  722. ///
  723. /// // The Paragraph that this cell houses.
  724. /// Paragraph p = cell.Paragraph;
  725. ///
  726. /// // Insert this rows index.
  727. /// p.InsertText(i.ToString(), false);
  728. /// }
  729. ///
  730. /// document.Save();
  731. /// }// Release this document from memory.
  732. /// </code>
  733. /// </example>
  734. public void InsertColumn(int index)
  735. {
  736. if (rows.Count > 0)
  737. {
  738. foreach (Row r in rows)
  739. {
  740. if(columnCount == index)
  741. r.Cells[index - 1].Xml.AddAfterSelf(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
  742. else
  743. r.Cells[index].Xml.AddBeforeSelf(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
  744. }
  745. rows = (from r in Xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
  746. select new Row(Document, r)).ToList();
  747. rowCount = rows.Count;
  748. if (rows.Count > 0)
  749. if (rows[0].Cells.Count > 0)
  750. columnCount = rows[0].Cells.Count;
  751. }
  752. }
  753. /// <summary>
  754. /// Insert a page break before a Table.
  755. /// </summary>
  756. /// <example>
  757. /// Insert a Table and a Paragraph into a document with a page break between them.
  758. /// <code>
  759. /// // Create a new document.
  760. /// using (DocX document = DocX.Create(@"Test.docx"))
  761. /// {
  762. /// // Insert a new Paragraph.
  763. /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
  764. ///
  765. /// // Insert a new Table.
  766. /// Table t1 = document.InsertTable(2, 2);
  767. /// t1.Design = TableDesign.LightShadingAccent1;
  768. ///
  769. /// // Insert a page break before this Table.
  770. /// t1.InsertPageBreakBeforeSelf();
  771. ///
  772. /// // Save this document.
  773. /// document.Save();
  774. /// }// Release this document from memory.
  775. /// </code>
  776. /// </example>
  777. public override void InsertPageBreakBeforeSelf()
  778. {
  779. base.InsertPageBreakBeforeSelf();
  780. }
  781. /// <summary>
  782. /// Insert a page break after a Table.
  783. /// </summary>
  784. /// <example>
  785. /// Insert a Table and a Paragraph into a document with a page break between them.
  786. /// <code>
  787. /// // Create a new document.
  788. /// using (DocX document = DocX.Create(@"Test.docx"))
  789. /// {
  790. /// // Insert a new Table.
  791. /// Table t1 = document.InsertTable(2, 2);
  792. /// t1.Design = TableDesign.LightShadingAccent1;
  793. ///
  794. /// // Insert a page break after this Table.
  795. /// t1.InsertPageBreakAfterSelf();
  796. ///
  797. /// // Insert a new Paragraph.
  798. /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
  799. ///
  800. /// // Save this document.
  801. /// document.Save();
  802. /// }// Release this document from memory.
  803. /// </code>
  804. /// </example>
  805. public override void InsertPageBreakAfterSelf()
  806. {
  807. base.InsertPageBreakAfterSelf();
  808. }
  809. /// <summary>
  810. /// Insert a new Table before this Table, this Table can be from this document or another document.
  811. /// </summary>
  812. /// <param name="t">The Table t to be inserted</param>
  813. /// <returns>A new Table inserted before this Table.</returns>
  814. /// <example>
  815. /// Insert a new Table before this Table.
  816. /// <code>
  817. /// // Place holder for a Table.
  818. /// Table t;
  819. ///
  820. /// // Load document a.
  821. /// using (DocX documentA = DocX.Load(@"a.docx"))
  822. /// {
  823. /// // Get the first Table from this document.
  824. /// t = documentA.Tables[0];
  825. /// }
  826. ///
  827. /// // Load document b.
  828. /// using (DocX documentB = DocX.Load(@"b.docx"))
  829. /// {
  830. /// // Get the first Table in document b.
  831. /// Table t2 = documentB.Tables[0];
  832. ///
  833. /// // Insert the Table from document a before this Table.
  834. /// Table newTable = t2.InsertTableBeforeSelf(t);
  835. ///
  836. /// // Save all changes made to document b.
  837. /// documentB.Save();
  838. /// }// Release this document from memory.
  839. /// </code>
  840. /// </example>
  841. public override Table InsertTableBeforeSelf(Table t)
  842. {
  843. return base.InsertTableBeforeSelf(t);
  844. }
  845. /// <summary>
  846. /// Insert a new Table into this document before this Table.
  847. /// </summary>
  848. /// <param name="rowCount">The number of rows this Table should have.</param>
  849. /// <param name="coloumnCount">The number of coloumns this Table should have.</param>
  850. /// <returns>A new Table inserted before this Table.</returns>
  851. /// <example>
  852. /// <code>
  853. /// // Create a new document.
  854. /// using (DocX document = DocX.Create(@"Test.docx"))
  855. /// {
  856. /// //Insert a Table into this document.
  857. /// Table t = document.InsertTable(2, 2);
  858. /// t.Design = TableDesign.LightShadingAccent1;
  859. /// t.Alignment = Alignment.center;
  860. ///
  861. /// // Insert a new Table before this Table.
  862. /// Table newTable = t.InsertTableBeforeSelf(2, 2);
  863. /// newTable.Design = TableDesign.LightShadingAccent2;
  864. /// newTable.Alignment = Alignment.center;
  865. ///
  866. /// // Save all changes made to this document.
  867. /// document.Save();
  868. /// }// Release this document from memory.
  869. /// </code>
  870. /// </example>
  871. public override Table InsertTableBeforeSelf(int rowCount, int coloumnCount)
  872. {
  873. return base.InsertTableBeforeSelf(rowCount, coloumnCount);
  874. }
  875. /// <summary>
  876. /// Insert a new Table after this Table, this Table can be from this document or another document.
  877. /// </summary>
  878. /// <param name="t">The Table t to be inserted</param>
  879. /// <returns>A new Table inserted after this Table.</returns>
  880. /// <example>
  881. /// Insert a new Table after this Table.
  882. /// <code>
  883. /// // Place holder for a Table.
  884. /// Table t;
  885. ///
  886. /// // Load document a.
  887. /// using (DocX documentA = DocX.Load(@"a.docx"))
  888. /// {
  889. /// // Get the first Table from this document.
  890. /// t = documentA.Tables[0];
  891. /// }
  892. ///
  893. /// // Load document b.
  894. /// using (DocX documentB = DocX.Load(@"b.docx"))
  895. /// {
  896. /// // Get the first Table in document b.
  897. /// Table t2 = documentB.Tables[0];
  898. ///
  899. /// // Insert the Table from document a after this Table.
  900. /// Table newTable = t2.InsertTableAfterSelf(t);
  901. ///
  902. /// // Save all changes made to document b.
  903. /// documentB.Save();
  904. /// }// Release this document from memory.
  905. /// </code>
  906. /// </example>
  907. public override Table InsertTableAfterSelf(Table t)
  908. {
  909. return base.InsertTableAfterSelf(t);
  910. }
  911. /// <summary>
  912. /// Insert a new Table into this document after this Table.
  913. /// </summary>
  914. /// <param name="rowCount">The number of rows this Table should have.</param>
  915. /// <param name="coloumnCount">The number of coloumns this Table should have.</param>
  916. /// <returns>A new Table inserted before this Table.</returns>
  917. /// <example>
  918. /// <code>
  919. /// // Create a new document.
  920. /// using (DocX document = DocX.Create(@"Test.docx"))
  921. /// {
  922. /// //Insert a Table into this document.
  923. /// Table t = document.InsertTable(2, 2);
  924. /// t.Design = TableDesign.LightShadingAccent1;
  925. /// t.Alignment = Alignment.center;
  926. ///
  927. /// // Insert a new Table after this Table.
  928. /// Table newTable = t.InsertTableAfterSelf(2, 2);
  929. /// newTable.Design = TableDesign.LightShadingAccent2;
  930. /// newTable.Alignment = Alignment.center;
  931. ///
  932. /// // Save all changes made to this document.
  933. /// document.Save();
  934. /// }// Release this document from memory.
  935. /// </code>
  936. /// </example>
  937. public override Table InsertTableAfterSelf(int rowCount, int coloumnCount)
  938. {
  939. return base.InsertTableAfterSelf(rowCount, coloumnCount);
  940. }
  941. /// <summary>
  942. /// Insert a Paragraph before this Table, this Paragraph may have come from the same or another document.
  943. /// </summary>
  944. /// <param name="p">The Paragraph to insert.</param>
  945. /// <returns>The Paragraph now associated with this document.</returns>
  946. /// <example>
  947. /// Take a Paragraph from document a, and insert it into document b before this Table.
  948. /// <code>
  949. /// // Place holder for a Paragraph.
  950. /// Paragraph p;
  951. ///
  952. /// // Load document a.
  953. /// using (DocX documentA = DocX.Load(@"a.docx"))
  954. /// {
  955. /// // Get the first paragraph from this document.
  956. /// p = documentA.Paragraphs[0];
  957. /// }
  958. ///
  959. /// // Load document b.
  960. /// using (DocX documentB = DocX.Load(@"b.docx"))
  961. /// {
  962. /// // Get the first Table in document b.
  963. /// Table t = documentB.Tables[0];
  964. ///
  965. /// // Insert the Paragraph from document a before this Table.
  966. /// Paragraph newParagraph = t.InsertParagraphBeforeSelf(p);
  967. ///
  968. /// // Save all changes made to document b.
  969. /// documentB.Save();
  970. /// }// Release this document from memory.
  971. /// </code>
  972. /// </example>
  973. public override Paragraph InsertParagraphBeforeSelf(Paragraph p)
  974. {
  975. return base.InsertParagraphBeforeSelf(p);
  976. }
  977. /// <summary>
  978. /// Insert a new Paragraph before this Table.
  979. /// </summary>
  980. /// <param name="text">The initial text for this new Paragraph.</param>
  981. /// <returns>A new Paragraph inserted before this Table.</returns>
  982. /// <example>
  983. /// Insert a new Paragraph before the first Table in this document.
  984. /// <code>
  985. /// // Create a new document.
  986. /// using (DocX document = DocX.Create(@"Test.docx"))
  987. /// {
  988. /// // Insert a Table into this document.
  989. /// Table t = document.InsertTable(2, 2);
  990. ///
  991. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.");
  992. ///
  993. /// // Save all changes made to this new document.
  994. /// document.Save();
  995. /// }// Release this new document form memory.
  996. /// </code>
  997. /// </example>
  998. public override Paragraph InsertParagraphBeforeSelf(string text)
  999. {
  1000. return base.InsertParagraphBeforeSelf(text);
  1001. }
  1002. /// <summary>
  1003. /// Insert a new Paragraph before this Table.
  1004. /// </summary>
  1005. /// <param name="text">The initial text for this new Paragraph.</param>
  1006. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1007. /// <returns>A new Paragraph inserted before this Table.</returns>
  1008. /// <example>
  1009. /// Insert a new paragraph before the first Table in this document.
  1010. /// <code>
  1011. /// // Create a new document.
  1012. /// using (DocX document = DocX.Create(@"Test.docx"))
  1013. /// {
  1014. /// // Insert a Table into this document.
  1015. /// Table t = document.InsertTable(2, 2);
  1016. ///
  1017. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false);
  1018. ///
  1019. /// // Save all changes made to this new document.
  1020. /// document.Save();
  1021. /// }// Release this new document form memory.
  1022. /// </code>
  1023. /// </example>
  1024. public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
  1025. {
  1026. return base.InsertParagraphBeforeSelf(text, trackChanges);
  1027. }
  1028. /// <summary>
  1029. /// Insert a new Paragraph before this Table.
  1030. /// </summary>
  1031. /// <param name="text">The initial text for this new Paragraph.</param>
  1032. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1033. /// <param name="formatting">The formatting to apply to this insertion.</param>
  1034. /// <returns>A new Paragraph inserted before this Table.</returns>
  1035. /// <example>
  1036. /// Insert a new paragraph before the first Table in this document.
  1037. /// <code>
  1038. /// // Create a new document.
  1039. /// using (DocX document = DocX.Create(@"Test.docx"))
  1040. /// {
  1041. /// // Insert a Table into this document.
  1042. /// Table t = document.InsertTable(2, 2);
  1043. ///
  1044. /// Formatting boldFormatting = new Formatting();
  1045. /// boldFormatting.Bold = true;
  1046. ///
  1047. /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting);
  1048. ///
  1049. /// // Save all changes made to this new document.
  1050. /// document.Save();
  1051. /// }// Release this new document form memory.
  1052. /// </code>
  1053. /// </example>
  1054. public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
  1055. {
  1056. return base.InsertParagraphBeforeSelf(text, trackChanges, formatting);
  1057. }
  1058. /// <summary>
  1059. /// Insert a Paragraph after this Table, this Paragraph may have come from the same or another document.
  1060. /// </summary>
  1061. /// <param name="p">The Paragraph to insert.</param>
  1062. /// <returns>The Paragraph now associated with this document.</returns>
  1063. /// <example>
  1064. /// Take a Paragraph from document a, and insert it into document b after this Table.
  1065. /// <code>
  1066. /// // Place holder for a Paragraph.
  1067. /// Paragraph p;
  1068. ///
  1069. /// // Load document a.
  1070. /// using (DocX documentA = DocX.Load(@"a.docx"))
  1071. /// {
  1072. /// // Get the first paragraph from this document.
  1073. /// p = documentA.Paragraphs[0];
  1074. /// }
  1075. ///
  1076. /// // Load document b.
  1077. /// using (DocX documentB = DocX.Load(@"b.docx"))
  1078. /// {
  1079. /// // Get the first Table in document b.
  1080. /// Table t = documentB.Tables[0];
  1081. ///
  1082. /// // Insert the Paragraph from document a after this Table.
  1083. /// Paragraph newParagraph = t.InsertParagraphAfterSelf(p);
  1084. ///
  1085. /// // Save all changes made to document b.
  1086. /// documentB.Save();
  1087. /// }// Release this document from memory.
  1088. /// </code>
  1089. /// </example>
  1090. public override Paragraph InsertParagraphAfterSelf(Paragraph p)
  1091. {
  1092. return base.InsertParagraphAfterSelf(p);
  1093. }
  1094. /// <summary>
  1095. /// Insert a new Paragraph after this Table.
  1096. /// </summary>
  1097. /// <param name="text">The initial text for this new Paragraph.</param>
  1098. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1099. /// <param name="formatting">The formatting to apply to this insertion.</param>
  1100. /// <returns>A new Paragraph inserted after this Table.</returns>
  1101. /// <example>
  1102. /// Insert a new paragraph after the first Table in this document.
  1103. /// <code>
  1104. /// // Create a new document.
  1105. /// using (DocX document = DocX.Create(@"Test.docx"))
  1106. /// {
  1107. /// // Insert a Table into this document.
  1108. /// Table t = document.InsertTable(2, 2);
  1109. ///
  1110. /// Formatting boldFormatting = new Formatting();
  1111. /// boldFormatting.Bold = true;
  1112. ///
  1113. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting);
  1114. ///
  1115. /// // Save all changes made to this new document.
  1116. /// document.Save();
  1117. /// }// Release this new document form memory.
  1118. /// </code>
  1119. /// </example>
  1120. public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
  1121. {
  1122. return base.InsertParagraphAfterSelf(text, trackChanges, formatting);
  1123. }
  1124. /// <summary>
  1125. /// Insert a new Paragraph after this Table.
  1126. /// </summary>
  1127. /// <param name="text">The initial text for this new Paragraph.</param>
  1128. /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
  1129. /// <returns>A new Paragraph inserted after this Table.</returns>
  1130. /// <example>
  1131. /// Insert a new paragraph after the first Table in this document.
  1132. /// <code>
  1133. /// // Create a new document.
  1134. /// using (DocX document = DocX.Create(@"Test.docx"))
  1135. /// {
  1136. /// // Insert a Table into this document.
  1137. /// Table t = document.InsertTable(2, 2);
  1138. ///
  1139. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false);
  1140. ///
  1141. /// // Save all changes made to this new document.
  1142. /// document.Save();
  1143. /// }// Release this new document form memory.
  1144. /// </code>
  1145. /// </example>
  1146. public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
  1147. {
  1148. return base.InsertParagraphAfterSelf(text, trackChanges);
  1149. }
  1150. /// <summary>
  1151. /// Insert a new Paragraph after this Table.
  1152. /// </summary>
  1153. /// <param name="text">The initial text for this new Paragraph.</param>
  1154. /// <returns>A new Paragraph inserted after this Table.</returns>
  1155. /// <example>
  1156. /// Insert a new Paragraph after the first Table in this document.
  1157. /// <code>
  1158. /// // Create a new document.
  1159. /// using (DocX document = DocX.Create(@"Test.docx"))
  1160. /// {
  1161. /// // Insert a Table into this document.
  1162. /// Table t = document.InsertTable(2, 2);
  1163. ///
  1164. /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.");
  1165. ///
  1166. /// // Save all changes made to this new document.
  1167. /// document.Save();
  1168. /// }// Release this new document form memory.
  1169. /// </code>
  1170. /// </example>
  1171. public override Paragraph InsertParagraphAfterSelf(string text)
  1172. {
  1173. return base.InsertParagraphAfterSelf(text);
  1174. }
  1175. }
  1176. /// <summary>
  1177. /// Represents a single row in a Table.
  1178. /// </summary>
  1179. public class Row:DocXElement
  1180. {
  1181. private List<Cell> cells;
  1182. /// <summary>
  1183. /// A list of Cells in this Row.
  1184. /// </summary>
  1185. public List<Cell> Cells { get { return cells; } }
  1186. internal Row(DocX document, XElement xml):base(document, xml)
  1187. {
  1188. cells = (from c in xml.Elements(XName.Get("tc", DocX.w.NamespaceName))
  1189. select new Cell(document, c)).ToList();
  1190. }
  1191. /// <summary>
  1192. /// Returns a list of all Pictures in a Row.
  1193. /// </summary>
  1194. /// <example>
  1195. /// Returns a list of all Pictures in a Row.
  1196. /// <code>
  1197. /// // Create a document.
  1198. /// using (DocX document = DocX.Load(@"Test.docx"))
  1199. /// {
  1200. /// // Get the first Table in a document.
  1201. /// Table t = document.Tables[0];
  1202. ///
  1203. /// // Get the first Row in a Table.
  1204. /// Row r = t.Rows[0];
  1205. ///
  1206. /// // Get all of the Pictures in this Row.
  1207. /// List<Picture> pictures = r.Pictures;
  1208. ///
  1209. /// // Save this document.
  1210. /// document.Save();
  1211. /// }
  1212. /// </code>
  1213. /// </example>
  1214. public List<Picture> Pictures
  1215. {
  1216. get
  1217. {
  1218. List<Picture> pictures = new List<Picture>();
  1219. foreach (Cell c in Cells)
  1220. pictures.AddRange(c.Pictures);
  1221. return pictures;
  1222. }
  1223. }
  1224. /// <summary>
  1225. /// Get all of the Hyperlinks in this Row.
  1226. /// </summary>
  1227. /// <example>
  1228. /// Get all of the Hyperlinks in this Row.
  1229. /// <code>
  1230. /// // Create a document.
  1231. /// using (DocX document = DocX.Load(@"Test.docx"))
  1232. /// {
  1233. /// // Get the first Table in this document.
  1234. /// Table t = document.Tables[0];
  1235. ///
  1236. /// // Get the first Row in this Table.
  1237. /// Row r = t.Rows[0];
  1238. ///
  1239. /// // Get a list of all Hyperlinks in this Row.
  1240. /// List<Hyperlink> hyperlinks = r.Hyperlinks;
  1241. ///
  1242. /// // Save this document.
  1243. /// document.Save();
  1244. /// }
  1245. /// </code>
  1246. /// </example>
  1247. public List<Hyperlink> Hyperlinks
  1248. {
  1249. get
  1250. {
  1251. List<Hyperlink> hyperlinks = new List<Hyperlink>();
  1252. foreach (Cell c in Cells)
  1253. hyperlinks.AddRange(c.Hyperlinks);
  1254. return hyperlinks;
  1255. }
  1256. }
  1257. /// <summary>
  1258. /// Set the content direction of a single Row in a Table.
  1259. /// </summary>
  1260. /// <param name="direction">The direction either (LeftToRight or RightToLeft).</param>
  1261. /// <example>
  1262. /// Set the content direction of a single Row in a Table.
  1263. /// <code>
  1264. /// // Load a document.
  1265. /// using (DocX document = DocX.Load(@"Test.docx"))
  1266. /// {
  1267. /// // Get the first Table from a document.
  1268. /// Table t = document.Tables[0];
  1269. ///
  1270. /// // Get the first row from this Table.
  1271. /// Row r = t.Rows[0];
  1272. ///
  1273. /// // Set the content direction of this Row to RightToLeft.
  1274. /// r.SetDirection(Direction.RightToLeft);
  1275. ///
  1276. /// // Save all changes made to this document.
  1277. /// document.Save();
  1278. ///}
  1279. /// </code>
  1280. /// </example>
  1281. public void SetDirection(Direction direction)
  1282. {
  1283. foreach (Cell c in Cells)
  1284. c.SetDirection(direction);
  1285. }
  1286. /// <summary>
  1287. /// Height in pixels. // Added by Joel, refactored by Cathal.
  1288. /// </summary>
  1289. public double Height
  1290. {
  1291. get
  1292. {
  1293. /*
  1294. * Get the trPr (table row properties) element for this Row,
  1295. * null will be return if no such element exists.
  1296. */
  1297. XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
  1298. // If trPr is null, this row contains no height information.
  1299. if(trPr == null)
  1300. return double.NaN;
  1301. /*
  1302. * Get the trHeight element for this Row,
  1303. * null will be return if no such element exists.
  1304. */
  1305. XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
  1306. // If trHeight is null, this row contains no height information.
  1307. if (trHeight == null)
  1308. return double.NaN;
  1309. // Get the val attribute for this trHeight element.
  1310. XAttribute val = trHeight.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1311. // If w is null, this cell contains no width information.
  1312. if (val == null)
  1313. return double.NaN;
  1314. // If val is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  1315. double heightInWordUnits;
  1316. if (!double.TryParse(val.Value, out heightInWordUnits))
  1317. {
  1318. val.Remove();
  1319. return double.NaN;
  1320. }
  1321. // 15 "word units" in one pixel
  1322. return (heightInWordUnits / 15);
  1323. }
  1324. set
  1325. {
  1326. /*
  1327. * Get the trPr (table row properties) element for this Row,
  1328. * null will be return if no such element exists.
  1329. */
  1330. XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
  1331. if (trPr == null)
  1332. {
  1333. Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
  1334. trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
  1335. }
  1336. /*
  1337. * Get the trHeight element for this Row,
  1338. * null will be return if no such element exists.
  1339. */
  1340. XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
  1341. if (trHeight == null)
  1342. {
  1343. trPr.SetElementValue(XName.Get("trHeight", DocX.w.NamespaceName), string.Empty);
  1344. trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
  1345. }
  1346. // The hRule attribute needs to be set to exact.
  1347. trHeight.SetAttributeValue(XName.Get("hRule", DocX.w.NamespaceName), "exact");
  1348. // 15 "word units" is equal to one pixel.
  1349. trHeight.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), (value * 15).ToString());
  1350. }
  1351. }
  1352. /// <summary>
  1353. /// Merge cells starting with startIndex and ending with endIndex.
  1354. /// </summary>
  1355. public void MergeCells(int startIndex, int endIndex)
  1356. {
  1357. // Check for valid start and end indexes.
  1358. if (startIndex < 0 || endIndex <= startIndex || endIndex > Cells.Count + 1)
  1359. throw new IndexOutOfRangeException();
  1360. // The sum of all merged gridSpans.
  1361. int gridSpanSum = 0;
  1362. // Foreach each Cell between startIndex and endIndex inclusive.
  1363. foreach (Cell c in cells.Where((z, i) => i > startIndex && i <= endIndex))
  1364. {
  1365. XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1366. if (tcPr != null)
  1367. {
  1368. XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1369. if (gridSpan != null)
  1370. {
  1371. XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1372. int value = 0;
  1373. if (val != null)
  1374. if (int.TryParse(val.Value, out value))
  1375. gridSpanSum += value - 1;
  1376. }
  1377. }
  1378. // Add this cells Pragraph to the merge start Cell.
  1379. cells[startIndex].Xml.Add(c.Xml.Elements(XName.Get("p", DocX.w.NamespaceName)));
  1380. // Remove this Cell.
  1381. c.Xml.Remove();
  1382. }
  1383. /*
  1384. * Get the tcPr (table cell properties) element for the first cell in this merge,
  1385. * null will be returned if no such element exists.
  1386. */
  1387. XElement start_tcPr = cells[startIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1388. if (start_tcPr == null)
  1389. {
  1390. cells[startIndex].Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1391. start_tcPr = cells[startIndex].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1392. }
  1393. /*
  1394. * Get the gridSpan element of this row,
  1395. * null will be returned if no such element exists.
  1396. */
  1397. XElement start_gridSpan = start_tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1398. if (start_gridSpan == null)
  1399. {
  1400. start_tcPr.SetElementValue(XName.Get("gridSpan", DocX.w.NamespaceName), string.Empty);
  1401. start_gridSpan = start_tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
  1402. }
  1403. /*
  1404. * Get the val attribute of this row,
  1405. * null will be returned if no such element exists.
  1406. */
  1407. XAttribute start_val = start_gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
  1408. int start_value = 0;
  1409. if (start_val != null)
  1410. if (int.TryParse(start_val.Value, out start_value))
  1411. gridSpanSum += start_value - 1;
  1412. // Set the val attribute to the number of merged cells.
  1413. start_gridSpan.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), (gridSpanSum + (endIndex - startIndex + 1)).ToString());
  1414. // Rebuild the cells collection.
  1415. cells =
  1416. (
  1417. from c in Xml.Elements(XName.Get("tc", DocX.w.NamespaceName))
  1418. select new Cell(Document, c)
  1419. ).ToList();
  1420. }
  1421. }
  1422. public class Cell:DocXElement
  1423. {
  1424. private List<Paragraph> paragraphs;
  1425. public List<Paragraph> Paragraphs
  1426. {
  1427. get { return paragraphs; }
  1428. set { paragraphs = value; }
  1429. }
  1430. internal Cell(DocX document, XElement xml):base(document, xml)
  1431. {
  1432. paragraphs = xml.Elements(XName.Get("p", DocX.w.NamespaceName)).Select(p => new Paragraph(document, p, 0)).ToList();
  1433. }
  1434. /// <summary>
  1435. /// Returns a list of all Pictures in a Cell.
  1436. /// </summary>
  1437. /// <example>
  1438. /// Returns a list of all Pictures in a Cell.
  1439. /// <code>
  1440. /// // Create a document.
  1441. /// using (DocX document = DocX.Load(@"Test.docx"))
  1442. /// {
  1443. /// // Get the first Table in a document.
  1444. /// Table t = document.Tables[0];
  1445. ///
  1446. /// // Get the first Row in a Table.
  1447. /// Row r = t.Rows[0];
  1448. ///
  1449. /// // Get the first Cell in a Row.
  1450. /// Cell c = r.Cells[0];
  1451. ///
  1452. /// // Get all of the Pictures in this Cell.
  1453. /// List<Picture> pictures = c.Pictures;
  1454. ///
  1455. /// // Save this document.
  1456. /// document.Save();
  1457. /// }
  1458. /// </code>
  1459. /// </example>
  1460. public List<Picture> Pictures
  1461. {
  1462. get
  1463. {
  1464. List<Picture> pictures = new List<Picture>();
  1465. foreach (Paragraph p in Paragraphs)
  1466. pictures.AddRange(p.Pictures);
  1467. return pictures;
  1468. }
  1469. }
  1470. /// <summary>
  1471. /// Get all of the Hyperlinks in this Cell.
  1472. /// </summary>
  1473. /// <example>
  1474. /// Get all of the Hyperlinks in this Cell.
  1475. /// <code>
  1476. /// // Create a document.
  1477. /// using (DocX document = DocX.Load(@"Test.docx"))
  1478. /// {
  1479. /// // Get the first Table in this document.
  1480. /// Table t = document.Tables[0];
  1481. ///
  1482. /// // Get the first Row in this Table.
  1483. /// Row r = t.Rows[0];
  1484. ///
  1485. /// // Get the first Cell in this Row.
  1486. /// Cell c = r.Cells[0];
  1487. ///
  1488. /// // Get a list of all Hyperlinks in this Cell.
  1489. /// List<Hyperlink> hyperlinks = c.Hyperlinks;
  1490. ///
  1491. /// // Save this document.
  1492. /// document.Save();
  1493. /// }
  1494. /// </code>
  1495. /// </example>
  1496. public List<Hyperlink> Hyperlinks
  1497. {
  1498. get
  1499. {
  1500. List<Hyperlink> hyperlinks = new List<Hyperlink>();
  1501. foreach (Paragraph p in Paragraphs)
  1502. hyperlinks.AddRange(p.Hyperlinks);
  1503. return hyperlinks;
  1504. }
  1505. }
  1506. /// <summary>
  1507. /// Set the content direction of a single Cell in a Table.
  1508. /// </summary>
  1509. /// <param name="direction">The direction either (LeftToRight or RightToLeft).</param>
  1510. /// <example>
  1511. /// Set the content direction of a single Cell in a Table.
  1512. /// <code>
  1513. /// // Load a document.
  1514. /// using (DocX document = DocX.Load(@"Test.docx"))
  1515. /// {
  1516. /// // Get the first Table from a document.
  1517. /// Table t = document.Tables[0];
  1518. ///
  1519. /// // Get the first row from this Table.
  1520. /// Row r = t.Rows[0];
  1521. ///
  1522. /// // Get the first cell from this Row.
  1523. /// Cell c = r.Cells[1];
  1524. ///
  1525. /// // Set the content direction of this Cell to RightToLeft.
  1526. /// c.SetDirection(Direction.RightToLeft);
  1527. ///
  1528. /// // Save all changes made to this document.
  1529. /// document.Save();
  1530. ///}
  1531. /// </code>
  1532. /// </example>
  1533. public void SetDirection(Direction direction)
  1534. {
  1535. foreach (Paragraph p in Paragraphs)
  1536. p.Direction = direction;
  1537. }
  1538. public Color Shading
  1539. {
  1540. get
  1541. {
  1542. /*
  1543. * Get the tcPr (table cell properties) element for this Cell,
  1544. * null will be return if no such element exists.
  1545. */
  1546. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1547. // If tcPr is null, this cell contains no Color information.
  1548. if (tcPr == null)
  1549. return Color.White;
  1550. /*
  1551. * Get the shd (table shade) element for this Cell,
  1552. * null will be return if no such element exists.
  1553. */
  1554. XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  1555. // If shd is null, this cell contains no Color information.
  1556. if (shd == null)
  1557. return Color.White;
  1558. // Get the w attribute of the tcW element.
  1559. XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName));
  1560. // If fill is null, this cell contains no Color information.
  1561. if (fill == null)
  1562. return Color.White;
  1563. return ColorTranslator.FromHtml(string.Format("#{0}", fill.Value));
  1564. }
  1565. set
  1566. {
  1567. /*
  1568. * Get the tcPr (table cell properties) element for this Cell,
  1569. * null will be return if no such element exists.
  1570. */
  1571. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1572. if (tcPr == null)
  1573. {
  1574. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1575. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1576. }
  1577. /*
  1578. * Get the shd (table shade) element for this Cell,
  1579. * null will be return if no such element exists.
  1580. */
  1581. XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  1582. if (shd == null)
  1583. {
  1584. tcPr.SetElementValue(XName.Get("shd", DocX.w.NamespaceName), string.Empty);
  1585. shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
  1586. }
  1587. // The val attribute needs to be set to clear
  1588. shd.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), "clear");
  1589. // The color attribute needs to be set to auto
  1590. shd.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), "auto");
  1591. // The fill attribute needs to be set to the hex for this Color.
  1592. shd.SetAttributeValue(XName.Get("fill", DocX.w.NamespaceName), value.ToHex());
  1593. }
  1594. }
  1595. /// <summary>
  1596. /// Width in pixels. // Added by Joel, refactored by Cathal
  1597. /// </summary>
  1598. public double Width
  1599. {
  1600. get
  1601. {
  1602. /*
  1603. * Get the tcPr (table cell properties) element for this Cell,
  1604. * null will be return if no such element exists.
  1605. */
  1606. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1607. // If tcPr is null, this cell contains no width information.
  1608. if (tcPr == null)
  1609. return double.NaN;
  1610. /*
  1611. * Get the tcW (table cell width) element for this Cell,
  1612. * null will be return if no such element exists.
  1613. */
  1614. XElement tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
  1615. // If tcW is null, this cell contains no width information.
  1616. if (tcW == null)
  1617. return double.NaN;
  1618. // Get the w attribute of the tcW element.
  1619. XAttribute w = tcW.Attribute(XName.Get("w", DocX.w.NamespaceName));
  1620. // If w is null, this cell contains no width information.
  1621. if (w == null)
  1622. return double.NaN;
  1623. // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
  1624. double widthInWordUnits;
  1625. if (!double.TryParse(w.Value, out widthInWordUnits))
  1626. {
  1627. w.Remove();
  1628. return double.NaN;
  1629. }
  1630. // 15 "word units" is equal to one pixel.
  1631. return (widthInWordUnits / 15);
  1632. }
  1633. set
  1634. {
  1635. /*
  1636. * Get the tcPr (table cell properties) element for this Cell,
  1637. * null will be return if no such element exists.
  1638. */
  1639. XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1640. if (tcPr == null)
  1641. {
  1642. Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
  1643. tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  1644. }
  1645. /*
  1646. * Get the tcW (table cell width) element for this Cell,
  1647. * null will be return if no such element exists.
  1648. */
  1649. XElement tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
  1650. if (tcW == null)
  1651. {
  1652. tcPr.SetElementValue(XName.Get("tcW", DocX.w.NamespaceName), string.Empty);
  1653. tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
  1654. }
  1655. // 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.
  1656. tcW.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
  1657. // 15 "word units" is equal to one pixel.
  1658. tcW.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), (value * 15).ToString());
  1659. }
  1660. }
  1661. }
  1662. }