Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Novacode;
  10. using Image = Novacode.Image;
  11. namespace Examples
  12. {
  13. class Program
  14. {
  15. private static Border BlankBorder = new Border(BorderStyle.Tcbs_none, 0, 0, Color.White);
  16. static void Main(string[] args)
  17. {
  18. Setup();
  19. // Easy
  20. Console.WriteLine("\nRunning Easy Examples");
  21. HelloWorld();
  22. HelloWorldKeepLinesTogether();
  23. HelloWorldKeepWithNext();
  24. RightToLeft();
  25. Indentation();
  26. HeadersAndFooters();
  27. HyperlinksImagesTables();
  28. AddList();
  29. Equations();
  30. Bookmarks();
  31. BarChart();
  32. PieChart();
  33. LineChart();
  34. Chart3D();
  35. DocumentMargins();
  36. CreateTableWithTextDirection();
  37. AddToc();
  38. AddTocByReference();
  39. // Intermediate
  40. Console.WriteLine("\nRunning Intermediate Examples");
  41. CreateInvoice();
  42. HyperlinksImagesTablesWithLists();
  43. HeadersAndFootersWithImagesAndTables();
  44. HeadersAndFootersWithImagesAndTablesUsingInsertPicture();
  45. DocumentsWithListsFontChange();
  46. DocumentHeading();
  47. LargeTable();
  48. TableWithSpecifiedWidths();
  49. // Advanced
  50. Console.WriteLine("\nRunning Advanced Examples");
  51. ProgrammaticallyManipulateImbeddedImage();
  52. ReplaceTextParallel();
  53. Console.WriteLine("\nPress any key to exit.");
  54. Console.ReadKey();
  55. }
  56. private static void Setup()
  57. {
  58. if (!Directory.Exists("docs"))
  59. {
  60. Directory.CreateDirectory("docs");
  61. }
  62. }
  63. #region Charts
  64. private class ChartData
  65. {
  66. public String Mounth { get; set; }
  67. public Double Money { get; set; }
  68. public static List<ChartData> CreateCompanyList1()
  69. {
  70. List<ChartData> company1 = new List<ChartData>();
  71. company1.Add(new ChartData() { Mounth = "January", Money = 100 });
  72. company1.Add(new ChartData() { Mounth = "February", Money = 120 });
  73. company1.Add(new ChartData() { Mounth = "March", Money = 140 });
  74. return company1;
  75. }
  76. public static List<ChartData> CreateCompanyList2()
  77. {
  78. List<ChartData> company2 = new List<ChartData>();
  79. company2.Add(new ChartData() { Mounth = "January", Money = 80 });
  80. company2.Add(new ChartData() { Mounth = "February", Money = 160 });
  81. company2.Add(new ChartData() { Mounth = "March", Money = 130 });
  82. return company2;
  83. }
  84. }
  85. private static void BarChart()
  86. {
  87. Console.WriteLine("\tBarChart()");
  88. // Create new document.
  89. using (DocX document = DocX.Create(@"docs\BarChart.docx"))
  90. {
  91. // Create chart.
  92. BarChart c = new BarChart();
  93. c.BarDirection = BarDirection.Column;
  94. c.BarGrouping = BarGrouping.Standard;
  95. c.GapWidth = 400;
  96. c.AddLegend(ChartLegendPosition.Bottom, false);
  97. // Create data.
  98. List<ChartData> company1 = ChartData.CreateCompanyList1();
  99. List<ChartData> company2 = ChartData.CreateCompanyList2();
  100. // Create and add series
  101. Series s1 = new Series("Microsoft");
  102. s1.Color = Color.GreenYellow;
  103. s1.Bind(company1, "Mounth", "Money");
  104. c.AddSeries(s1);
  105. Series s2 = new Series("Apple");
  106. s2.Bind(company2, "Mounth", "Money");
  107. c.AddSeries(s2);
  108. // Insert chart into document
  109. document.InsertParagraph("Diagram").FontSize(20);
  110. document.InsertChart(c);
  111. document.Save();
  112. }
  113. Console.WriteLine("\tCreated: docs\\BarChart.docx\n");
  114. }
  115. private static void PieChart()
  116. {
  117. Console.WriteLine("\tPieChart()");
  118. // Create new document.
  119. using (DocX document = DocX.Create(@"docs\PieChart.docx"))
  120. {
  121. // Create chart.
  122. PieChart c = new PieChart();
  123. c.AddLegend(ChartLegendPosition.Bottom, false);
  124. // Create data.
  125. List<ChartData> company2 = ChartData.CreateCompanyList2();
  126. // Create and add series
  127. Series s = new Series("Apple");
  128. s.Bind(company2, "Mounth", "Money");
  129. c.AddSeries(s);
  130. // Insert chart into document
  131. document.InsertParagraph("Diagram").FontSize(20);
  132. document.InsertChart(c);
  133. document.Save();
  134. }
  135. Console.WriteLine("\tCreated: docs\\PieChart.docx\n");
  136. }
  137. private static void LineChart()
  138. {
  139. Console.WriteLine("\tLineChart()");
  140. // Create new document.
  141. using (DocX document = DocX.Create(@"docs\LineChart.docx"))
  142. {
  143. // Create chart.
  144. LineChart c = new LineChart();
  145. c.AddLegend(ChartLegendPosition.Bottom, false);
  146. // Create data.
  147. List<ChartData> company1 = ChartData.CreateCompanyList1();
  148. List<ChartData> company2 = ChartData.CreateCompanyList2();
  149. // Create and add series
  150. Series s1 = new Series("Microsoft");
  151. s1.Color = Color.GreenYellow;
  152. s1.Bind(company1, "Mounth", "Money");
  153. c.AddSeries(s1);
  154. Series s2 = new Series("Apple");
  155. s2.Bind(company2, "Mounth", "Money");
  156. c.AddSeries(s2);
  157. // Insert chart into document
  158. document.InsertParagraph("Diagram").FontSize(20);
  159. document.InsertChart(c);
  160. document.Save();
  161. }
  162. Console.WriteLine("\tCreated: docs\\LineChart.docx\n");
  163. }
  164. private static void Chart3D()
  165. {
  166. Console.WriteLine("\tChart3D()");
  167. // Create new document.
  168. using (DocX document = DocX.Create(@"docs\3DChart.docx"))
  169. {
  170. // Create chart.
  171. BarChart c = new BarChart();
  172. c.View3D = true;
  173. // Create data.
  174. List<ChartData> company1 = ChartData.CreateCompanyList1();
  175. // Create and add series
  176. Series s = new Series("Microsoft");
  177. s.Color = Color.GreenYellow;
  178. s.Bind(company1, "Mounth", "Money");
  179. c.AddSeries(s);
  180. // Insert chart into document
  181. document.InsertParagraph("3D Diagram").FontSize(20);
  182. document.InsertChart(c);
  183. document.Save();
  184. }
  185. Console.WriteLine("\tCreated: docs\\3DChart.docx\n");
  186. }
  187. #endregion
  188. /// <summary>
  189. /// Create a document with two equations.
  190. /// </summary>
  191. private static void Equations()
  192. {
  193. Console.WriteLine("\tEquations()");
  194. // Create a new document.
  195. using (DocX document = DocX.Create(@"docs\Equations.docx"))
  196. {
  197. // Insert first Equation in this document.
  198. Paragraph pEquation1 = document.InsertEquation("x = y+z");
  199. // Insert second Equation in this document and add formatting.
  200. Paragraph pEquation2 = document.InsertEquation("x = (y+z)/t").FontSize(18).Color(Color.Blue);
  201. // Save this document to disk.
  202. document.Save();
  203. Console.WriteLine("\tCreated: docs\\Equations.docx\n");
  204. }
  205. }
  206. public static void DocumentHeading()
  207. {
  208. Console.WriteLine("\tDocumentHeading()");
  209. using (DocX document = DocX.Create(@"docs\DocumentHeading.docx"))
  210. {
  211. foreach (HeadingType heading in (HeadingType[])Enum.GetValues(typeof(HeadingType)))
  212. {
  213. string text = string.Format("{0} - The quick brown fox jumps over the lazy dog", heading.EnumDescription());
  214. Paragraph p = document.InsertParagraph();
  215. p.AppendLine(text).Heading(heading);
  216. }
  217. document.Save();
  218. Console.WriteLine("\tCreated: docs\\DocumentHeading.docx\n");
  219. }
  220. }
  221. private static void Bookmarks()
  222. {
  223. Console.WriteLine("\tBookmarks()");
  224. using (var document = DocX.Create(@"docs\Bookmarks.docx"))
  225. {
  226. var paragraph = document.InsertBookmark("firstBookmark");
  227. var paragraph2 = document.InsertParagraph("This is a paragraph which contains a ");
  228. paragraph2.AppendBookmark("secondBookmark");
  229. paragraph2.Append("bookmark");
  230. paragraph2.InsertAtBookmark("handy ", "secondBookmark");
  231. document.Save();
  232. Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n");
  233. }
  234. }
  235. /// <summary>
  236. /// Create a document with a Paragraph whos first line is indented.
  237. /// </summary>
  238. private static void Indentation()
  239. {
  240. Console.WriteLine("\tIndentation()");
  241. // Create a new document.
  242. using (DocX document = DocX.Create(@"docs\Indentation.docx"))
  243. {
  244. // Create a new Paragraph.
  245. Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
  246. // Indent only the first line of the Paragraph.
  247. p.IndentationFirstLine = 1.0f;
  248. // Save all changes made to this document.
  249. document.Save();
  250. Console.WriteLine("\tCreated: docs\\Indentation.docx\n");
  251. }
  252. }
  253. /// <summary>
  254. /// Create a document that with RightToLeft text flow.
  255. /// </summary>
  256. private static void RightToLeft()
  257. {
  258. Console.WriteLine("\tRightToLeft()");
  259. // Create a new document.
  260. using (DocX document = DocX.Create(@"docs\RightToLeft.docx"))
  261. {
  262. // Create a new Paragraph with the text "Hello World".
  263. Paragraph p = document.InsertParagraph("Hello World.");
  264. // Make this Paragraph flow right to left. Default is left to right.
  265. p.Direction = Direction.RightToLeft;
  266. // You don't need to manually set the text direction foreach Paragraph, you can just call this function.
  267. document.SetDirection(Direction.RightToLeft);
  268. // Save all changes made to this document.
  269. document.Save();
  270. Console.WriteLine("\tCreated: docs\\RightToLeft.docx\n");
  271. }
  272. }
  273. /// <summary>
  274. /// Creates a document with a Hyperlink, an Image and a Table.
  275. /// </summary>
  276. private static void HyperlinksImagesTables()
  277. {
  278. Console.WriteLine("\tHyperlinksImagesTables()");
  279. // Create a document.
  280. using (DocX document = DocX.Create(@"docs\HyperlinksImagesTables.docx"))
  281. {
  282. // Add a hyperlink into the document.
  283. Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));
  284. // Add a Table into the document.
  285. Table table = document.AddTable(2, 2);
  286. table.Design = TableDesign.ColorfulGridAccent2;
  287. table.Alignment = Alignment.center;
  288. table.Rows[0].Cells[0].Paragraphs[0].Append("1");
  289. table.Rows[0].Cells[1].Paragraphs[0].Append("2");
  290. table.Rows[1].Cells[0].Paragraphs[0].Append("3");
  291. table.Rows[1].Cells[1].Paragraphs[0].Append("4");
  292. Row newRow = table.InsertRow(table.Rows[1]);
  293. newRow.ReplaceText("4", "5");
  294. // Add an image into the document.
  295. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
  296. rd.Up(2);
  297. Image image = document.AddImage(rd.Path + @"\images\logo_template.png");
  298. // Create a picture (A custom view of an Image).
  299. Picture picture = image.CreatePicture();
  300. picture.Rotation = 10;
  301. picture.SetPictureShape(BasicShapes.cube);
  302. // Insert a new Paragraph into the document.
  303. Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
  304. title.Alignment = Alignment.center;
  305. // Insert a new Paragraph into the document.
  306. Paragraph p1 = document.InsertParagraph();
  307. // Append content to the Paragraph
  308. p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
  309. p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
  310. p1.AppendLine();
  311. p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
  312. p1.AppendLine();
  313. p1.AppendLine("Can you check this Table of figures for me?");
  314. p1.AppendLine();
  315. // Insert the Table after Paragraph 1.
  316. p1.InsertTableAfterSelf(table);
  317. // Insert a new Paragraph into the document.
  318. Paragraph p2 = document.InsertParagraph();
  319. // Append content to the Paragraph.
  320. p2.AppendLine("Is it correct?");
  321. // Save this document.
  322. document.Save();
  323. Console.WriteLine("\tCreated: docs\\HyperlinksImagesTables.docx\n");
  324. }
  325. }
  326. private static void HyperlinksImagesTablesWithLists()
  327. {
  328. Console.WriteLine("\tHyperlinksImagesTablesWithLists()");
  329. // Create a document.
  330. using (DocX document = DocX.Create(@"docs\HyperlinksImagesTablesWithLists.docx"))
  331. {
  332. // Add a hyperlink into the document.
  333. Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));
  334. // created numbered lists
  335. var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1);
  336. document.AddListItem(numberedList, "First sub list item", 1);
  337. document.AddListItem(numberedList, "Second List Item.");
  338. document.AddListItem(numberedList, "Third list item.");
  339. document.AddListItem(numberedList, "Nested item.", 1);
  340. document.AddListItem(numberedList, "Second nested item.", 1);
  341. // created bulleted lists
  342. var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
  343. document.AddListItem(bulletedList, "Second bullet item");
  344. document.AddListItem(bulletedList, "Sub bullet item", 1);
  345. document.AddListItem(bulletedList, "Second sub bullet item", 1);
  346. document.AddListItem(bulletedList, "Third bullet item");
  347. // Add a Table into the document.
  348. Table table = document.AddTable(2, 2);
  349. table.Design = TableDesign.ColorfulGridAccent2;
  350. table.Alignment = Alignment.center;
  351. table.Rows[0].Cells[0].Paragraphs[0].Append("1");
  352. table.Rows[0].Cells[1].Paragraphs[0].Append("2");
  353. table.Rows[1].Cells[0].Paragraphs[0].Append("3");
  354. table.Rows[1].Cells[1].Paragraphs[0].Append("4");
  355. Row newRow = table.InsertRow(table.Rows[1]);
  356. newRow.ReplaceText("4", "5");
  357. // Add an image into the document.
  358. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
  359. rd.Up(2);
  360. Image image = document.AddImage(rd.Path + @"\images\logo_template.png");
  361. // Create a picture (A custom view of an Image).
  362. Picture picture = image.CreatePicture();
  363. picture.Rotation = 10;
  364. picture.SetPictureShape(BasicShapes.cube);
  365. // Insert a new Paragraph into the document.
  366. Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
  367. title.Alignment = Alignment.center;
  368. // Insert a new Paragraph into the document.
  369. Paragraph p1 = document.InsertParagraph();
  370. // Append content to the Paragraph
  371. p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
  372. p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
  373. p1.AppendLine();
  374. p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
  375. p1.AppendLine();
  376. p1.AppendLine("Can you check this Table of figures for me?");
  377. p1.AppendLine();
  378. // Insert the Table after Paragraph 1.
  379. p1.InsertTableAfterSelf(table);
  380. // Insert a new Paragraph into the document.
  381. Paragraph p2 = document.InsertParagraph();
  382. // Append content to the Paragraph.
  383. p2.AppendLine("Is it correct?");
  384. p2.AppendLine();
  385. p2.AppendLine("Adding bullet list below: ");
  386. document.InsertList(bulletedList);
  387. // Adding another paragraph to add table and bullet list after it
  388. Paragraph p3 = document.InsertParagraph();
  389. p3.AppendLine();
  390. p3.AppendLine("Adding another table...");
  391. // Adding another table
  392. Table table1 = document.AddTable(2, 2);
  393. table1.Design = TableDesign.ColorfulGridAccent2;
  394. table1.Alignment = Alignment.center;
  395. table1.Rows[0].Cells[0].Paragraphs[0].Append("1");
  396. table1.Rows[0].Cells[1].Paragraphs[0].Append("2");
  397. table1.Rows[1].Cells[0].Paragraphs[0].Append("3");
  398. table1.Rows[1].Cells[1].Paragraphs[0].Append("4");
  399. Paragraph p4 = document.InsertParagraph();
  400. p4.InsertTableBeforeSelf(table1);
  401. p4.AppendLine();
  402. // Insert numbered list after table
  403. Paragraph p5 = document.InsertParagraph();
  404. p5.AppendLine("Adding numbered list below: ");
  405. p5.AppendLine();
  406. document.InsertList(numberedList);
  407. // Save this document.
  408. document.Save();
  409. Console.WriteLine("\tCreated: docs\\HyperlinksImagesTablesWithLists.docx\n");
  410. }
  411. }
  412. private static void DocumentMargins()
  413. {
  414. Console.WriteLine("\tDocumentMargins()");
  415. // Create a document.
  416. using (DocX document = DocX.Create(@"docs\DocumentMargins.docx"))
  417. {
  418. // Create a float var that contains doc Margins properties.
  419. float leftMargin = document.MarginLeft;
  420. float rightMargin = document.MarginRight;
  421. float topMargin = document.MarginTop;
  422. float bottomMargin = document.MarginBottom;
  423. // Modify using your own vars.
  424. leftMargin = 95F;
  425. rightMargin = 45F;
  426. topMargin = 50F;
  427. bottomMargin = 180F;
  428. // Or simply work the margins by setting the property directly.
  429. document.MarginLeft = leftMargin;
  430. document.MarginRight = rightMargin;
  431. document.MarginTop = topMargin;
  432. document.MarginBottom = bottomMargin;
  433. // created bulleted lists
  434. var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
  435. document.AddListItem(bulletedList, "Second bullet item");
  436. document.AddListItem(bulletedList, "Sub bullet item", 1);
  437. document.AddListItem(bulletedList, "Second sub bullet item", 1);
  438. document.AddListItem(bulletedList, "Third bullet item");
  439. document.InsertList(bulletedList);
  440. // Save this document.
  441. document.Save();
  442. Console.WriteLine("\tCreated: docs\\DocumentMargins.docx\n");
  443. }
  444. }
  445. private static void DocumentsWithListsFontChange()
  446. {
  447. Console.WriteLine("\tDocumentsWithListsFontChange()");
  448. // Create a document.
  449. using (DocX document = DocX.Create(@"docs\DocumentsWithListsFontChange.docx"))
  450. {
  451. foreach ( FontFamily oneFontFamily in FontFamily.Families ) {
  452. FontFamily fontFamily = oneFontFamily;
  453. double fontSize = 15;
  454. // created numbered lists
  455. var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1);
  456. document.AddListItem(numberedList, "First sub list item", 1);
  457. document.AddListItem(numberedList, "Second List Item.");
  458. document.AddListItem(numberedList, "Third list item.");
  459. document.AddListItem(numberedList, "Nested item.", 1);
  460. document.AddListItem(numberedList, "Second nested item.", 1);
  461. // created bulleted lists
  462. var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
  463. document.AddListItem(bulletedList, "Second bullet item");
  464. document.AddListItem(bulletedList, "Sub bullet item", 1);
  465. document.AddListItem(bulletedList, "Second sub bullet item", 1);
  466. document.AddListItem(bulletedList, "Third bullet item");
  467. document.InsertList(bulletedList);
  468. document.InsertList(numberedList, fontFamily, fontSize);
  469. }
  470. // Save this document.
  471. document.Save();
  472. Console.WriteLine("\tCreated: docs\\DocumentsWithListsFontChange.docx\n");
  473. }
  474. }
  475. private static void AddList()
  476. {
  477. Console.WriteLine("\tAddList()");
  478. using (var document = DocX.Create(@"docs\Lists.docx"))
  479. {
  480. var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 2);
  481. document.AddListItem(numberedList, "First sub list item", 1);
  482. document.AddListItem(numberedList, "Second List Item.");
  483. document.AddListItem(numberedList, "Third list item.");
  484. document.AddListItem(numberedList, "Nested item.", 1);
  485. document.AddListItem(numberedList, "Second nested item.", 1);
  486. var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
  487. document.AddListItem(bulletedList, "Second bullet item");
  488. document.AddListItem(bulletedList, "Sub bullet item", 1);
  489. document.AddListItem(bulletedList, "Second sub bullet item", 1);
  490. document.AddListItem(bulletedList, "Third bullet item");
  491. document.InsertList(numberedList);
  492. document.InsertList(bulletedList);
  493. document.Save();
  494. Console.WriteLine("\tCreated: docs\\Lists.docx");
  495. }
  496. }
  497. private static void HeadersAndFooters()
  498. {
  499. Console.WriteLine("\tHeadersAndFooters()");
  500. // Create a new document.
  501. using (DocX document = DocX.Create(@"docs\HeadersAndFooters.docx"))
  502. {
  503. // Add Headers and Footers to this document.
  504. document.AddHeaders();
  505. document.AddFooters();
  506. // Force the first page to have a different Header and Footer.
  507. document.DifferentFirstPage = true;
  508. // Force odd & even pages to have different Headers and Footers.
  509. document.DifferentOddAndEvenPages = true;
  510. // Get the first, odd and even Headers for this document.
  511. Header header_first = document.Headers.first;
  512. Header header_odd = document.Headers.odd;
  513. Header header_even = document.Headers.even;
  514. // Get the first, odd and even Footer for this document.
  515. Footer footer_first = document.Footers.first;
  516. Footer footer_odd = document.Footers.odd;
  517. Footer footer_even = document.Footers.even;
  518. // Insert a Paragraph into the first Header.
  519. Paragraph p0 = header_first.InsertParagraph();
  520. p0.Append("Hello First Header.").Bold();
  521. // Insert a Paragraph into the odd Header.
  522. Paragraph p1 = header_odd.InsertParagraph();
  523. p1.Append("Hello Odd Header.").Bold();
  524. // Insert a Paragraph into the even Header.
  525. Paragraph p2 = header_even.InsertParagraph();
  526. p2.Append("Hello Even Header.").Bold();
  527. // Insert a Paragraph into the first Footer.
  528. Paragraph p3 = footer_first.InsertParagraph();
  529. p3.Append("Hello First Footer.").Bold();
  530. // Insert a Paragraph into the odd Footer.
  531. Paragraph p4 = footer_odd.InsertParagraph();
  532. p4.Append("Hello Odd Footer.").Bold();
  533. // Insert a Paragraph into the even Header.
  534. Paragraph p5 = footer_even.InsertParagraph();
  535. p5.Append("Hello Even Footer.").Bold();
  536. // Insert a Paragraph into the document.
  537. Paragraph p6 = document.InsertParagraph();
  538. p6.AppendLine("Hello First page.");
  539. // Create a second page to show that the first page has its own header and footer.
  540. p6.InsertPageBreakAfterSelf();
  541. // Insert a Paragraph after the page break.
  542. Paragraph p7 = document.InsertParagraph();
  543. p7.AppendLine("Hello Second page.");
  544. // Create a third page to show that even and odd pages have different headers and footers.
  545. p7.InsertPageBreakAfterSelf();
  546. // Insert a Paragraph after the page break.
  547. Paragraph p8 = document.InsertParagraph();
  548. p8.AppendLine("Hello Third page.");
  549. //Insert a next page break, which is a section break combined with a page break
  550. document.InsertSectionPageBreak();
  551. //Insert a paragraph after the "Next" page break
  552. Paragraph p9 = document.InsertParagraph();
  553. p9.Append("Next page section break.");
  554. //Insert a continuous section break
  555. document.InsertSection();
  556. //Create a paragraph in the new section
  557. var p10 = document.InsertParagraph();
  558. p10.Append("Continuous section paragraph.");
  559. // Save all changes to this document.
  560. document.Save();
  561. Console.WriteLine("\tCreated: docs\\HeadersAndFooters.docx\n");
  562. }// Release this document from memory.
  563. }
  564. private static void HeadersAndFootersWithImagesAndTables()
  565. {
  566. Console.WriteLine("\tHeadersAndFootersWithImagesAndTables()");
  567. // Create a new document.
  568. using (DocX document = DocX.Create(@"docs\HeadersAndFootersWithImagesAndTables.docx"))
  569. {
  570. // Add a template logo image to this document.
  571. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
  572. rd.Up(2);
  573. Image logo = document.AddImage(rd.Path + @"\images\logo_the_happy_builder.png");
  574. // Add Headers and Footers to this document.
  575. document.AddHeaders();
  576. document.AddFooters();
  577. // Force the first page to have a different Header and Footer.
  578. document.DifferentFirstPage = true;
  579. // Force odd & even pages to have different Headers and Footers.
  580. document.DifferentOddAndEvenPages = true;
  581. // Get the first, odd and even Headers for this document.
  582. Header header_first = document.Headers.first;
  583. Header header_odd = document.Headers.odd;
  584. Header header_even = document.Headers.even;
  585. // Get the first, odd and even Footer for this document.
  586. Footer footer_first = document.Footers.first;
  587. Footer footer_odd = document.Footers.odd;
  588. Footer footer_even = document.Footers.even;
  589. // Insert a Paragraph into the first Header.
  590. Paragraph p0 = header_first.InsertParagraph();
  591. p0.Append("Hello First Header.").Bold();
  592. // Insert a Paragraph into the odd Header.
  593. Paragraph p1 = header_odd.InsertParagraph();
  594. p1.Append("Hello Odd Header.").Bold();
  595. // Insert a Paragraph into the even Header.
  596. Paragraph p2 = header_even.InsertParagraph();
  597. p2.Append("Hello Even Header.").Bold();
  598. // Insert a Paragraph into the first Footer.
  599. Paragraph p3 = footer_first.InsertParagraph();
  600. p3.Append("Hello First Footer.").Bold();
  601. // Insert a Paragraph into the odd Footer.
  602. Paragraph p4 = footer_odd.InsertParagraph();
  603. p4.Append("Hello Odd Footer.").Bold();
  604. // Insert a Paragraph into the even Header.
  605. Paragraph p5 = footer_even.InsertParagraph();
  606. p5.Append("Hello Even Footer.").Bold();
  607. // Insert a Paragraph into the document.
  608. Paragraph p6 = document.InsertParagraph();
  609. p6.AppendLine("Hello First page.");
  610. // Create a second page to show that the first page has its own header and footer.
  611. p6.InsertPageBreakAfterSelf();
  612. // Insert a Paragraph after the page break.
  613. Paragraph p7 = document.InsertParagraph();
  614. p7.AppendLine("Hello Second page.");
  615. // Create a third page to show that even and odd pages have different headers and footers.
  616. p7.InsertPageBreakAfterSelf();
  617. // Insert a Paragraph after the page break.
  618. Paragraph p8 = document.InsertParagraph();
  619. p8.AppendLine("Hello Third page.");
  620. //Insert a next page break, which is a section break combined with a page break
  621. document.InsertSectionPageBreak();
  622. //Insert a paragraph after the "Next" page break
  623. Paragraph p9 = document.InsertParagraph();
  624. p9.Append("Next page section break.");
  625. //Insert a continuous section break
  626. document.InsertSection();
  627. //Create a paragraph in the new section
  628. var p10 = document.InsertParagraph();
  629. p10.Append("Continuous section paragraph.");
  630. // Inserting logo into footer and header into Tables
  631. #region Company Logo in Header in Table
  632. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  633. Table header_first_table = header_first.InsertTable(1, 2);
  634. header_first_table.Design = TableDesign.TableGrid;
  635. header_first_table.AutoFit = AutoFit.Window;
  636. // Get the upper right Paragraph in the layout_table.
  637. Paragraph upperRightParagraph = header_first.Tables[0].Rows[0].Cells[1].Paragraphs[0];
  638. // Insert this template logo into the upper right Paragraph of Table.
  639. upperRightParagraph.AppendPicture(logo.CreatePicture());
  640. upperRightParagraph.Alignment = Alignment.right;
  641. // Get the upper left Paragraph in the layout_table.
  642. Paragraph upperLeftParagraphFirstTable = header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
  643. upperLeftParagraphFirstTable.Append("Company Name - DocX Corporation");
  644. #endregion
  645. #region Company Logo in Header in Invisible Table
  646. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  647. Table header_second_table = header_odd.InsertTable(1, 2);
  648. header_second_table.Design = TableDesign.None;
  649. header_second_table.AutoFit = AutoFit.Window;
  650. // Get the upper right Paragraph in the layout_table.
  651. Paragraph upperRightParagraphSecondTable = header_second_table.Rows[0].Cells[1].Paragraphs[0];
  652. // Insert this template logo into the upper right Paragraph of Table.
  653. upperRightParagraphSecondTable.AppendPicture(logo.CreatePicture());
  654. upperRightParagraphSecondTable.Alignment = Alignment.right;
  655. // Get the upper left Paragraph in the layout_table.
  656. Paragraph upperLeftParagraphSecondTable = header_second_table.Rows[0].Cells[0].Paragraphs[0];
  657. upperLeftParagraphSecondTable.Append("Company Name - DocX Corporation");
  658. #endregion
  659. #region Company Logo in Footer in Table
  660. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  661. Table footer_first_table = footer_first.InsertTable(1, 2);
  662. footer_first_table.Design = TableDesign.TableGrid;
  663. footer_first_table.AutoFit = AutoFit.Window;
  664. // Get the upper right Paragraph in the layout_table.
  665. Paragraph upperRightParagraphFooterParagraph = footer_first.Tables[0].Rows[0].Cells[1].Paragraphs[0];
  666. // Insert this template logo into the upper right Paragraph of Table.
  667. upperRightParagraphFooterParagraph.AppendPicture(logo.CreatePicture());
  668. upperRightParagraphFooterParagraph.Alignment = Alignment.right;
  669. // Get the upper left Paragraph in the layout_table.
  670. Paragraph upperLeftParagraphFirstTableFooter = footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
  671. upperLeftParagraphFirstTableFooter.Append("Company Name - DocX Corporation");
  672. #endregion
  673. #region Company Logo in Header in Invisible Table
  674. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  675. Table footer_second_table = footer_odd.InsertTable(1, 2);
  676. footer_second_table.Design = TableDesign.None;
  677. footer_second_table.AutoFit = AutoFit.Window;
  678. // Get the upper right Paragraph in the layout_table.
  679. Paragraph upperRightParagraphSecondTableFooter = footer_second_table.Rows[0].Cells[1].Paragraphs[0];
  680. // Insert this template logo into the upper right Paragraph of Table.
  681. upperRightParagraphSecondTableFooter.AppendPicture(logo.CreatePicture());
  682. upperRightParagraphSecondTableFooter.Alignment = Alignment.right;
  683. // Get the upper left Paragraph in the layout_table.
  684. Paragraph upperLeftParagraphSecondTableFooter = footer_second_table.Rows[0].Cells[0].Paragraphs[0];
  685. upperLeftParagraphSecondTableFooter.Append("Company Name - DocX Corporation");
  686. #endregion
  687. // Save all changes to this document.
  688. document.Save();
  689. Console.WriteLine("\tCreated: docs\\HeadersAndFootersWithImagesAndTables.docx\n");
  690. }// Release this document from memory.
  691. }
  692. private static void HeadersAndFootersWithImagesAndTablesUsingInsertPicture()
  693. {
  694. Console.WriteLine("\tHeadersAndFootersWithImagesAndTablesUsingInsertPicture()");
  695. // Create a new document.
  696. using (DocX document = DocX.Create(@"docs\HeadersAndFootersWithImagesAndTablesUsingInsertPicture.docx"))
  697. {
  698. // Add a template logo image to this document.
  699. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
  700. rd.Up(2);
  701. Image logo = document.AddImage(rd.Path + @"\images\logo_the_happy_builder.png");
  702. // Add Headers and Footers to this document.
  703. document.AddHeaders();
  704. document.AddFooters();
  705. // Force the first page to have a different Header and Footer.
  706. document.DifferentFirstPage = true;
  707. // Force odd & even pages to have different Headers and Footers.
  708. document.DifferentOddAndEvenPages = true;
  709. // Get the first, odd and even Headers for this document.
  710. Header header_first = document.Headers.first;
  711. Header header_odd = document.Headers.odd;
  712. Header header_even = document.Headers.even;
  713. // Get the first, odd and even Footer for this document.
  714. Footer footer_first = document.Footers.first;
  715. Footer footer_odd = document.Footers.odd;
  716. Footer footer_even = document.Footers.even;
  717. // Insert a Paragraph into the first Header.
  718. Paragraph p0 = header_first.InsertParagraph();
  719. p0.Append("Hello First Header.").Bold();
  720. // Insert a Paragraph into the odd Header.
  721. Paragraph p1 = header_odd.InsertParagraph();
  722. p1.Append("Hello Odd Header.").Bold();
  723. // Insert a Paragraph into the even Header.
  724. Paragraph p2 = header_even.InsertParagraph();
  725. p2.Append("Hello Even Header.").Bold();
  726. // Insert a Paragraph into the first Footer.
  727. Paragraph p3 = footer_first.InsertParagraph();
  728. p3.Append("Hello First Footer.").Bold();
  729. // Insert a Paragraph into the odd Footer.
  730. Paragraph p4 = footer_odd.InsertParagraph();
  731. p4.Append("Hello Odd Footer.").Bold();
  732. // Insert a Paragraph into the even Header.
  733. Paragraph p5 = footer_even.InsertParagraph();
  734. p5.Append("Hello Even Footer.").Bold();
  735. // Insert a Paragraph into the document.
  736. Paragraph p6 = document.InsertParagraph();
  737. p6.AppendLine("Hello First page.");
  738. // Create a second page to show that the first page has its own header and footer.
  739. p6.InsertPageBreakAfterSelf();
  740. // Insert a Paragraph after the page break.
  741. Paragraph p7 = document.InsertParagraph();
  742. p7.AppendLine("Hello Second page.");
  743. // Create a third page to show that even and odd pages have different headers and footers.
  744. p7.InsertPageBreakAfterSelf();
  745. // Insert a Paragraph after the page break.
  746. Paragraph p8 = document.InsertParagraph();
  747. p8.AppendLine("Hello Third page.");
  748. //Insert a next page break, which is a section break combined with a page break
  749. document.InsertSectionPageBreak();
  750. //Insert a paragraph after the "Next" page break
  751. Paragraph p9 = document.InsertParagraph();
  752. p9.Append("Next page section break.");
  753. //Insert a continuous section break
  754. document.InsertSection();
  755. //Create a paragraph in the new section
  756. var p10 = document.InsertParagraph();
  757. p10.Append("Continuous section paragraph.");
  758. // Inserting logo into footer and header into Tables
  759. #region Company Logo in Header in Table
  760. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  761. Table header_first_table = header_first.InsertTable(1, 2);
  762. header_first_table.Design = TableDesign.TableGrid;
  763. header_first_table.AutoFit = AutoFit.Window;
  764. // Get the upper right Paragraph in the layout_table.
  765. Paragraph upperRightParagraph = header_first.Tables[0].Rows[0].Cells[1].Paragraphs[0];
  766. // Insert this template logo into the upper right Paragraph of Table.
  767. upperRightParagraph.InsertPicture(logo.CreatePicture());
  768. upperRightParagraph.Alignment = Alignment.right;
  769. // Get the upper left Paragraph in the layout_table.
  770. Paragraph upperLeftParagraphFirstTable = header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
  771. upperLeftParagraphFirstTable.Append("Company Name - DocX Corporation");
  772. #endregion
  773. #region Company Logo in Header in Invisible Table
  774. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  775. Table header_second_table = header_odd.InsertTable(1, 2);
  776. header_second_table.Design = TableDesign.None;
  777. header_second_table.AutoFit = AutoFit.Window;
  778. // Get the upper right Paragraph in the layout_table.
  779. Paragraph upperRightParagraphSecondTable = header_second_table.Rows[0].Cells[1].Paragraphs[0];
  780. // Insert this template logo into the upper right Paragraph of Table.
  781. upperRightParagraphSecondTable.InsertPicture(logo.CreatePicture());
  782. upperRightParagraphSecondTable.Alignment = Alignment.right;
  783. // Get the upper left Paragraph in the layout_table.
  784. Paragraph upperLeftParagraphSecondTable = header_second_table.Rows[0].Cells[0].Paragraphs[0];
  785. upperLeftParagraphSecondTable.Append("Company Name - DocX Corporation");
  786. #endregion
  787. #region Company Logo in Footer in Table
  788. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  789. Table footer_first_table = footer_first.InsertTable(1, 2);
  790. footer_first_table.Design = TableDesign.TableGrid;
  791. footer_first_table.AutoFit = AutoFit.Window;
  792. // Get the upper right Paragraph in the layout_table.
  793. Paragraph upperRightParagraphFooterParagraph = footer_first.Tables[0].Rows[0].Cells[1].Paragraphs[0];
  794. // Insert this template logo into the upper right Paragraph of Table.
  795. upperRightParagraphFooterParagraph.InsertPicture(logo.CreatePicture());
  796. upperRightParagraphFooterParagraph.Alignment = Alignment.right;
  797. // Get the upper left Paragraph in the layout_table.
  798. Paragraph upperLeftParagraphFirstTableFooter = footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
  799. upperLeftParagraphFirstTableFooter.Append("Company Name - DocX Corporation");
  800. #endregion
  801. #region Company Logo in Header in Invisible Table
  802. // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
  803. Table footer_second_table = footer_odd.InsertTable(1, 2);
  804. footer_second_table.Design = TableDesign.None;
  805. footer_second_table.AutoFit = AutoFit.Window;
  806. // Get the upper right Paragraph in the layout_table.
  807. Paragraph upperRightParagraphSecondTableFooter = footer_second_table.Rows[0].Cells[1].Paragraphs[0];
  808. // Insert this template logo into the upper right Paragraph of Table.
  809. upperRightParagraphSecondTableFooter.InsertPicture(logo.CreatePicture());
  810. upperRightParagraphSecondTableFooter.Alignment = Alignment.right;
  811. // Get the upper left Paragraph in the layout_table.
  812. Paragraph upperLeftParagraphSecondTableFooter = footer_second_table.Rows[0].Cells[0].Paragraphs[0];
  813. upperLeftParagraphSecondTableFooter.Append("Company Name - DocX Corporation");
  814. #endregion
  815. // Save all changes to this document.
  816. document.Save();
  817. Console.WriteLine("\tCreated: docs\\HeadersAndFootersWithImagesAndTablesUsingInsertPicture.docx\n");
  818. }// Release this document from memory.
  819. }
  820. private static void CreateInvoice()
  821. {
  822. Console.WriteLine("\tCreateInvoice()");
  823. DocX g_document;
  824. try
  825. {
  826. // Store a global reference to the loaded document.
  827. g_document = DocX.Load(@"docs\InvoiceTemplate.docx");
  828. /*
  829. * The template 'InvoiceTemplate.docx' does exist,
  830. * so lets use it to create an invoice for a factitious company
  831. * called "The Happy Builder" and store a global reference it.
  832. */
  833. g_document = CreateInvoiceFromTemplate(DocX.Load(@"docs\InvoiceTemplate.docx"));
  834. // Save all changes made to this template as Invoice_The_Happy_Builder.docx (We don't want to replace InvoiceTemplate.docx).
  835. g_document.SaveAs(@"docs\Invoice_The_Happy_Builder.docx");
  836. Console.WriteLine("\tCreated: docs\\Invoice_The_Happy_Builder.docx\n");
  837. }
  838. // The template 'InvoiceTemplate.docx' does not exist, so create it.
  839. catch (FileNotFoundException)
  840. {
  841. // Create and store a global reference to the template 'InvoiceTemplate.docx'.
  842. g_document = CreateInvoiceTemplate();
  843. // Save the template 'InvoiceTemplate.docx'.
  844. g_document.Save();
  845. Console.WriteLine("\tCreated: docs\\InvoiceTemplate.docx");
  846. // The template exists now so re-call CreateInvoice().
  847. CreateInvoice();
  848. }
  849. }
  850. private static void CreateTableWithTextDirection()
  851. {
  852. Console.WriteLine("\tCreateTableWithTextDirection()");
  853. // Create a document.
  854. using (DocX document = DocX.Create(@"docs\\CeateTableWithTextDirection.docx"))
  855. {
  856. // Add a Table to this document.
  857. Table t = document.AddTable(2, 3);
  858. // Specify some properties for this Table.
  859. t.Alignment = Alignment.center;
  860. t.Design = TableDesign.MediumGrid1Accent2;
  861. // Add content to this Table.
  862. t.Rows[0].Cells[0].Paragraphs.First().Append("A");
  863. t.Rows[0].Cells[0].TextDirection = TextDirection.btLr;
  864. t.Rows[0].Cells[1].Paragraphs.First().Append("B");
  865. t.Rows[0].Cells[1].TextDirection = TextDirection.btLr;
  866. t.Rows[0].Cells[2].Paragraphs.First().Append("C");
  867. t.Rows[0].Cells[2].TextDirection = TextDirection.btLr;
  868. t.Rows[1].Cells[0].Paragraphs.First().Append("D");
  869. t.Rows[1].Cells[1].Paragraphs.First().Append("E");
  870. t.Rows[1].Cells[2].Paragraphs.First().Append("F");
  871. // Insert the Table into the document.
  872. document.InsertTable(t);
  873. document.Save();
  874. }// Release this document from memory.
  875. Console.WriteLine("\tCreated: docs\\CreateTableWithTextDirection.docx");
  876. }
  877. // Create an invoice for a factitious company called "The Happy Builder".
  878. private static DocX CreateInvoiceFromTemplate(DocX template)
  879. {
  880. #region Logo
  881. // A quick glance at the template shows us that the logo Paragraph is in row zero cell 1.
  882. Paragraph logo_paragraph = template.Tables[0].Rows[0].Cells[1].Paragraphs[0];
  883. // Remove the template Picture that is in this Paragraph.
  884. logo_paragraph.Pictures[0].Remove();
  885. // Add the Happy Builders logo to this document.
  886. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
  887. rd.Up(2);
  888. Image logo = template.AddImage(rd.Path + @"\images\logo_the_happy_builder.png");
  889. // Insert the Happy Builders logo into this Paragraph.
  890. logo_paragraph.InsertPicture(logo.CreatePicture());
  891. #endregion
  892. #region Set CustomProperty values
  893. // Set the value of the custom property 'company_name'.
  894. template.AddCustomProperty(new CustomProperty("company_name", "The Happy Builder"));
  895. // Set the value of the custom property 'company_slogan'.
  896. template.AddCustomProperty(new CustomProperty("company_slogan", "No job too small"));
  897. // Set the value of the custom properties 'hired_company_address_line_one', 'hired_company_address_line_two' and 'hired_company_address_line_three'.
  898. template.AddCustomProperty(new CustomProperty("hired_company_address_line_one", "The Crooked House,"));
  899. template.AddCustomProperty(new CustomProperty("hired_company_address_line_two", "Dublin,"));
  900. template.AddCustomProperty(new CustomProperty("hired_company_address_line_three", "12345"));
  901. // Set the value of the custom property 'invoice_date'.
  902. template.AddCustomProperty(new CustomProperty("invoice_date", DateTime.Today.Date.ToString("d")));
  903. // Set the value of the custom property 'invoice_number'.
  904. template.AddCustomProperty(new CustomProperty("invoice_number", 1));
  905. // Set the value of the custom property 'hired_company_details_line_one' and 'hired_company_details_line_two'.
  906. template.AddCustomProperty(new CustomProperty("hired_company_details_line_one", "Business Street, Dublin, 12345"));
  907. template.AddCustomProperty(new CustomProperty("hired_company_details_line_two", "Phone: 012-345-6789, Fax: 012-345-6789, e-mail: support@thehappybuilder.com"));
  908. #endregion
  909. /*
  910. * InvoiceTemplate.docx contains a blank Table,
  911. * we want to replace this with a new Table that
  912. * contains all of our invoice data.
  913. */
  914. Table t = template.Tables[1];
  915. Table invoice_table = CreateAndInsertInvoiceTableAfter(t, ref template);
  916. t.Remove();
  917. // Return the template now that it has been modified to hold all of our custom data.
  918. return template;
  919. }
  920. // Create an invoice template.
  921. private static DocX CreateInvoiceTemplate()
  922. {
  923. // Create a new document.
  924. DocX document = DocX.Create(@"docs\InvoiceTemplate.docx");
  925. // Create a table for layout purposes (This table will be invisible).
  926. Table layout_table = document.InsertTable(2, 2);
  927. layout_table.Design = TableDesign.TableNormal;
  928. layout_table.AutoFit = AutoFit.Window;
  929. // Dark formatting
  930. Formatting dark_formatting = new Formatting();
  931. dark_formatting.Bold = true;
  932. dark_formatting.Size = 12;
  933. dark_formatting.FontColor = Color.FromArgb(31, 73, 125);
  934. // Light formatting
  935. Formatting light_formatting = new Formatting();
  936. light_formatting.Italic = true;
  937. light_formatting.Size = 11;
  938. light_formatting.FontColor = Color.FromArgb(79, 129, 189);
  939. #region Company Name
  940. // Get the upper left Paragraph in the layout_table.
  941. Paragraph upper_left_paragraph = layout_table.Rows[0].Cells[0].Paragraphs[0];
  942. // Create a custom property called company_name
  943. CustomProperty company_name = new CustomProperty("company_name", "Company Name");
  944. // Insert a field of type doc property (This will display the custom property 'company_name')
  945. layout_table.Rows[0].Cells[0].Paragraphs[0].InsertDocProperty(company_name, f: dark_formatting);
  946. // Force the next text insert to be on a new line.
  947. upper_left_paragraph.InsertText("\n", false);
  948. #endregion
  949. #region Company Slogan
  950. // Create a custom property called company_slogan
  951. CustomProperty company_slogan = new CustomProperty("company_slogan", "Company slogan goes here.");
  952. // Insert a field of type doc property (This will display the custom property 'company_slogan')
  953. upper_left_paragraph.InsertDocProperty(company_slogan, f: light_formatting);
  954. #endregion
  955. #region Company Logo
  956. // Get the upper right Paragraph in the layout_table.
  957. Paragraph upper_right_paragraph = layout_table.Rows[0].Cells[1].Paragraphs[0];
  958. // Add a template logo image to this document.
  959. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
  960. rd.Up(2);
  961. Image logo = document.AddImage(rd.Path + @"\images\logo_template.png");
  962. // Insert this template logo into the upper right Paragraph.
  963. upper_right_paragraph.InsertPicture(logo.CreatePicture());
  964. upper_right_paragraph.Alignment = Alignment.right;
  965. #endregion
  966. // Custom properties cannot contain newlines, so the company address must be split into 3 custom properties.
  967. #region Hired Company Address
  968. // Create a custom property called company_address_line_one
  969. CustomProperty hired_company_address_line_one = new CustomProperty("hired_company_address_line_one", "Street Address,");
  970. // Get the lower left Paragraph in the layout_table.
  971. Paragraph lower_left_paragraph = layout_table.Rows[1].Cells[0].Paragraphs[0];
  972. lower_left_paragraph.InsertText("TO:\n", false, dark_formatting);
  973. // Insert a field of type doc property (This will display the custom property 'hired_company_address_line_one')
  974. lower_left_paragraph.InsertDocProperty(hired_company_address_line_one, f: light_formatting);
  975. // Force the next text insert to be on a new line.
  976. lower_left_paragraph.InsertText("\n", false);
  977. // Create a custom property called company_address_line_two
  978. CustomProperty hired_company_address_line_two = new CustomProperty("hired_company_address_line_two", "City,");
  979. // Insert a field of type doc property (This will display the custom property 'hired_company_address_line_two')
  980. lower_left_paragraph.InsertDocProperty(hired_company_address_line_two, f: light_formatting);
  981. // Force the next text insert to be on a new line.
  982. lower_left_paragraph.InsertText("\n", false);
  983. // Create a custom property called company_address_line_two
  984. CustomProperty hired_company_address_line_three = new CustomProperty("hired_company_address_line_three", "Zip Code");
  985. // Insert a field of type doc property (This will display the custom property 'hired_company_address_line_three')
  986. lower_left_paragraph.InsertDocProperty(hired_company_address_line_three, f: light_formatting);
  987. #endregion
  988. #region Date & Invoice number
  989. // Get the lower right Paragraph from the layout table.
  990. Paragraph lower_right_paragraph = layout_table.Rows[1].Cells[1].Paragraphs[0];
  991. CustomProperty invoice_date = new CustomProperty("invoice_date", DateTime.Today.Date.ToString("d"));
  992. lower_right_paragraph.InsertText("Date: ", false, dark_formatting);
  993. lower_right_paragraph.InsertDocProperty(invoice_date, f: light_formatting);
  994. CustomProperty invoice_number = new CustomProperty("invoice_number", 1);
  995. lower_right_paragraph.InsertText("\nInvoice: ", false, dark_formatting);
  996. lower_right_paragraph.InsertText("#", false, light_formatting);
  997. lower_right_paragraph.InsertDocProperty(invoice_number, f: light_formatting);
  998. lower_right_paragraph.Alignment = Alignment.right;
  999. #endregion
  1000. // Insert an empty Paragraph between two Tables, so that they do not touch.
  1001. document.InsertParagraph(string.Empty, false);
  1002. // This table will hold all of the invoice data.
  1003. Table invoice_table = document.InsertTable(4, 4);
  1004. invoice_table.Design = TableDesign.LightShadingAccent1;
  1005. invoice_table.Alignment = Alignment.center;
  1006. // A nice thank you Paragraph.
  1007. Paragraph thankyou = document.InsertParagraph("\nThank you for your business, we hope to work with you again soon.", false, dark_formatting);
  1008. thankyou.Alignment = Alignment.center;
  1009. #region Hired company details
  1010. CustomProperty hired_company_details_line_one = new CustomProperty("hired_company_details_line_one", "Street Address, City, ZIP Code");
  1011. CustomProperty hired_company_details_line_two = new CustomProperty("hired_company_details_line_two", "Phone: 000-000-0000, Fax: 000-000-0000, e-mail: support@companyname.com");
  1012. Paragraph companyDetails = document.InsertParagraph(string.Empty, false);
  1013. companyDetails.InsertDocProperty(hired_company_details_line_one, f: light_formatting);
  1014. companyDetails.InsertText("\n", false);
  1015. companyDetails.InsertDocProperty(hired_company_details_line_two, f: light_formatting);
  1016. companyDetails.Alignment = Alignment.center;
  1017. #endregion
  1018. // Return the document now that it has been created.
  1019. return document;
  1020. }
  1021. private static Table CreateAndInsertInvoiceTableAfter(Table t, ref DocX document)
  1022. {
  1023. // Grab data from somewhere (Most likely a database)
  1024. DataTable data = GetDataFromDatabase();
  1025. /*
  1026. * The trick to replacing one Table with another,
  1027. * is to insert the new Table after the old one,
  1028. * and then remove the old one.
  1029. */
  1030. Table invoice_table = t.InsertTableAfterSelf(data.Rows.Count + 1, data.Columns.Count);
  1031. invoice_table.Design = TableDesign.LightShadingAccent1;
  1032. #region Table title
  1033. Formatting table_title = new Formatting();
  1034. table_title.Bold = true;
  1035. invoice_table.Rows[0].Cells[0].Paragraphs[0].InsertText("Description", false, table_title);
  1036. invoice_table.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.center;
  1037. invoice_table.Rows[0].Cells[1].Paragraphs[0].InsertText("Hours", false, table_title);
  1038. invoice_table.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.center;
  1039. invoice_table.Rows[0].Cells[2].Paragraphs[0].InsertText("Rate", false, table_title);
  1040. invoice_table.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.center;
  1041. invoice_table.Rows[0].Cells[3].Paragraphs[0].InsertText("Amount", false, table_title);
  1042. invoice_table.Rows[0].Cells[3].Paragraphs[0].Alignment = Alignment.center;
  1043. #endregion
  1044. // Loop through the rows in the Table and insert data from the data source.
  1045. for (int row = 1; row < invoice_table.RowCount; row++)
  1046. {
  1047. for (int cell = 0; cell < invoice_table.Rows[row].Cells.Count; cell++)
  1048. {
  1049. Paragraph cell_paragraph = invoice_table.Rows[row].Cells[cell].Paragraphs[0];
  1050. cell_paragraph.InsertText(data.Rows[row - 1].ItemArray[cell].ToString(), false);
  1051. }
  1052. }
  1053. // We want to fill in the total by suming the values from the amount column.
  1054. Row total = invoice_table.InsertRow();
  1055. total.Cells[0].Paragraphs[0].InsertText("Total:", false);
  1056. Paragraph total_paragraph = total.Cells[invoice_table.ColumnCount - 1].Paragraphs[0];
  1057. /*
  1058. * Lots of people are scared of LINQ,
  1059. * so I will walk you through this line by line.
  1060. *
  1061. * invoice_table.Rows is an IEnumerable<Row> (i.e a collection of rows), with LINQ you can query collections.
  1062. * .Where(condition) is a filter that you want to apply to the items of this collection.
  1063. * My condition is that the index of the row must be greater than 0 and less than RowCount.
  1064. * .Select(something) lets you select something from each item in the filtered collection.
  1065. * I am selecting the Text value from each row, for example €100, then I am remove the €,
  1066. * and then I am parsing the remaining string as a double. This will return a collection of doubles,
  1067. * the final thing I do is call .Sum() on this collection which return one double the sum of all the doubles,
  1068. * this is the total.
  1069. */
  1070. double totalCost =
  1071. (
  1072. invoice_table.Rows
  1073. .Where((row, index) => index > 0 && index < invoice_table.RowCount - 1)
  1074. .Select(row => double.Parse(row.Cells[row.Cells.Count() - 1].Paragraphs[0].Text.Remove(0, 1)))
  1075. ).Sum();
  1076. // Insert the total calculated above using LINQ into the total Paragraph.
  1077. total_paragraph.InsertText(string.Format("€{0}", totalCost), false);
  1078. // Let the tables columns expand to fit its contents.
  1079. invoice_table.AutoFit = AutoFit.Contents;
  1080. // Center the Table
  1081. invoice_table.Alignment = Alignment.center;
  1082. // Return the invloce table now that it has been created.
  1083. return invoice_table;
  1084. }
  1085. // You need to rewrite this function to grab data from your data source.
  1086. private static DataTable GetDataFromDatabase()
  1087. {
  1088. DataTable table = new DataTable();
  1089. table.Columns.AddRange(new DataColumn[] { new DataColumn("Description"), new DataColumn("Hours"), new DataColumn("Rate"), new DataColumn("Amount") });
  1090. table.Rows.Add
  1091. (
  1092. "Install wooden doors (Kitchen, Sitting room, Dining room & Bedrooms)",
  1093. "5",
  1094. "€25",
  1095. string.Format("€{0}", 5 * 25)
  1096. );
  1097. table.Rows.Add
  1098. (
  1099. "Fit stairs",
  1100. "20",
  1101. "€30",
  1102. string.Format("€{0}", 20 * 30)
  1103. );
  1104. table.Rows.Add
  1105. (
  1106. "Replace Sitting room window",
  1107. "6",
  1108. "€50",
  1109. string.Format("€{0}", 6 * 50)
  1110. );
  1111. table.Rows.Add
  1112. (
  1113. "Build garden shed",
  1114. "10",
  1115. "€10",
  1116. string.Format("€{0}", 10 * 10)
  1117. );
  1118. table.Rows.Add
  1119. (
  1120. "Fit new lock on back door",
  1121. "0.5",
  1122. "€30",
  1123. string.Format("€{0}", 0.5 * 30)
  1124. );
  1125. table.Rows.Add
  1126. (
  1127. "Tile Kitchen floor",
  1128. "24",
  1129. "€25",
  1130. string.Format("€{0}", 24 * 25)
  1131. );
  1132. return table;
  1133. }
  1134. /// <summary>
  1135. /// Creates a simple document with the text Hello World.
  1136. /// </summary>
  1137. static void HelloWorld()
  1138. {
  1139. Console.WriteLine("\tHelloWorld()");
  1140. // Create a new document.
  1141. using (DocX document = DocX.Create(@"docs\Hello World.docx"))
  1142. {
  1143. // Insert a Paragraph into this document.
  1144. Paragraph p = document.InsertParagraph();
  1145. // Append some text and add formatting.
  1146. p.Append("Hello World!^011Hello World!")
  1147. .Font(new FontFamily("Times New Roman"))
  1148. .FontSize(32)
  1149. .Color(Color.Blue)
  1150. .Bold();
  1151. // Save this document to disk.
  1152. document.Save();
  1153. Console.WriteLine("\tCreated: docs\\Hello World.docx\n");
  1154. }
  1155. }
  1156. /// <summary>
  1157. /// Loads a document 'Input.docx' and writes the text 'Hello World' into the first imbedded Image.
  1158. /// This code creates the file 'Output.docx'.
  1159. /// </summary>
  1160. static void ProgrammaticallyManipulateImbeddedImage()
  1161. {
  1162. Console.WriteLine("\tProgrammaticallyManipulateImbeddedImage()");
  1163. const string str = "Hello World";
  1164. // Open the document Input.docx.
  1165. using (DocX document = DocX.Load(@"Input.docx"))
  1166. {
  1167. // Make sure this document has at least one Image.
  1168. if (document.Images.Count() > 0)
  1169. {
  1170. Image img = document.Images[0];
  1171. // Write "Hello World" into this Image.
  1172. Bitmap b = new Bitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite));
  1173. /*
  1174. * Get the Graphics object for this Bitmap.
  1175. * The Graphics object provides functions for drawing.
  1176. */
  1177. Graphics g = Graphics.FromImage(b);
  1178. // Draw the string "Hello World".
  1179. g.DrawString
  1180. (
  1181. str,
  1182. new Font("Tahoma", 20),
  1183. Brushes.Blue,
  1184. new PointF(0, 0)
  1185. );
  1186. // Save this Bitmap back into the document using a Create\Write stream.
  1187. b.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
  1188. }
  1189. else
  1190. Console.WriteLine("The provided document contains no Images.");
  1191. // Save this document as Output.docx.
  1192. document.SaveAs(@"docs\Output.docx");
  1193. Console.WriteLine("\tCreated: docs\\Output.docx\n");
  1194. }
  1195. }
  1196. /// <summary>
  1197. /// For each of the documents in the folder 'docs\',
  1198. /// Replace the string a with the string b,
  1199. /// Do this in Parrallel accross many CPU cores.
  1200. /// </summary>
  1201. static void ReplaceTextParallel()
  1202. {
  1203. Console.WriteLine("\tReplaceTextParallel()\n");
  1204. const string a = "apple";
  1205. const string b = "pear";
  1206. // Directory containing many .docx documents.
  1207. DirectoryInfo di = new DirectoryInfo(@"docs\");
  1208. // Loop through each document in this specified direction.
  1209. Parallel.ForEach
  1210. (
  1211. di.GetFiles(),
  1212. currentFile =>
  1213. {
  1214. // Load the document.
  1215. using (DocX document = DocX.Load(currentFile.FullName))
  1216. {
  1217. // Replace text in this document.
  1218. document.ReplaceText(a, b);
  1219. // Save changes made to this document.
  1220. document.Save();
  1221. } // Release this document from memory.
  1222. }
  1223. );
  1224. Console.WriteLine("\tCreated: None\n");
  1225. }
  1226. static void AddToc()
  1227. {
  1228. Console.WriteLine("\tAddToc()");
  1229. using (var document = DocX.Create(@"docs\Toc.docx"))
  1230. {
  1231. document.InsertTableOfContents("I can haz table of contentz", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2");
  1232. var h1 = document.InsertParagraph("Heading 1");
  1233. h1.StyleName = "Heading1";
  1234. document.InsertParagraph("Some very interesting content here");
  1235. var h2 = document.InsertParagraph("Heading 2");
  1236. document.InsertSectionPageBreak();
  1237. h2.StyleName = "Heading1";
  1238. document.InsertParagraph("Some very interesting content here as well");
  1239. var h3 = document.InsertParagraph("Heading 2.1");
  1240. h3.StyleName = "Heading2";
  1241. document.InsertParagraph("Not so very interesting....");
  1242. document.Save();
  1243. }
  1244. }
  1245. static void AddTocByReference()
  1246. {
  1247. Console.WriteLine("\tAddTocByReference()");
  1248. using (var document = DocX.Create(@"docs\TocByReference.docx"))
  1249. {
  1250. var h1 = document.InsertParagraph("Heading 1");
  1251. h1.StyleName = "Heading1";
  1252. document.InsertParagraph("Some very interesting content here");
  1253. var h2 = document.InsertParagraph("Heading 2");
  1254. document.InsertSectionPageBreak();
  1255. h2.StyleName = "Heading1";
  1256. document.InsertParagraph("Some very interesting content here as well");
  1257. var h3 = document.InsertParagraph("Heading 2.1");
  1258. h3.StyleName = "Heading2";
  1259. document.InsertParagraph("Not so very interesting....");
  1260. document.InsertTableOfContents(h2, "I can haz table of contentz", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2");
  1261. document.Save();
  1262. }
  1263. }
  1264. static void HelloWorldKeepWithNext()
  1265. {
  1266. // Create a Paragraph that will stay on the same page as the paragraph that comes next
  1267. Console.WriteLine("\tHelloWorldKeepWithNext()");
  1268. // Create a new document.
  1269. using (DocX document = DocX.Create("docs\\HelloWorldKeepWithNext.docx"))
  1270. {
  1271. // Create a new Paragraph with the text "Hello World".
  1272. Paragraph p = document.InsertParagraph("Hello World.");
  1273. p.KeepWithNext();
  1274. document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
  1275. // Save all changes made to this document.
  1276. document.Save();
  1277. Console.WriteLine("\tCreated: docs\\HelloWorldKeepWithNext.docx\n");
  1278. }
  1279. }
  1280. static void HelloWorldKeepLinesTogether()
  1281. {
  1282. // Create a Paragraph that will stay on the same page as the paragraph that comes next
  1283. Console.WriteLine("\tHelloWorldKeepLinesTogether()");
  1284. // Create a new document.
  1285. using (DocX document = DocX.Create("docs\\HelloWorldKeepLinesTogether.docx"))
  1286. {
  1287. // Create a new Paragraph with the text "Hello World".
  1288. Paragraph p = document.InsertParagraph("All lines of this paragraph will appear on the same page...\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6...");
  1289. p.KeepLinesTogether();
  1290. // Save all changes made to this document.
  1291. document.Save();
  1292. Console.WriteLine("\tCreated: docs\\HelloWorldKeepLinesTogether.docx\n");
  1293. }
  1294. }
  1295. static void LargeTable()
  1296. {
  1297. Console.WriteLine("\tLargeTable()");
  1298. var _directoryWithFiles = "docs\\";
  1299. using (var output = File.Open(_directoryWithFiles + "LargeTable.docx", FileMode.Create))
  1300. {
  1301. using (var doc = DocX.Create(output))
  1302. {
  1303. var tbl = doc.InsertTable(1, 18);
  1304. var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight;
  1305. var colWidth = wholeWidth / tbl.ColumnCount;
  1306. var colWidths = new int[tbl.ColumnCount];
  1307. tbl.AutoFit = AutoFit.Contents;
  1308. var r = tbl.Rows[0];
  1309. var cx = 0;
  1310. foreach (var cell in r.Cells)
  1311. {
  1312. cell.Paragraphs.First().Append("Col " + cx);
  1313. //cell.Width = colWidth;
  1314. cell.MarginBottom = 0;
  1315. cell.MarginLeft = 0;
  1316. cell.MarginRight = 0;
  1317. cell.MarginTop = 0;
  1318. cx++;
  1319. }
  1320. tbl.SetBorder(TableBorderType.Bottom, BlankBorder);
  1321. tbl.SetBorder(TableBorderType.Left, BlankBorder);
  1322. tbl.SetBorder(TableBorderType.Right, BlankBorder);
  1323. tbl.SetBorder(TableBorderType.Top, BlankBorder);
  1324. tbl.SetBorder(TableBorderType.InsideV, BlankBorder);
  1325. tbl.SetBorder(TableBorderType.InsideH, BlankBorder);
  1326. doc.Save();
  1327. }
  1328. }
  1329. Console.WriteLine("\tCreated: docs\\LargeTable.docx\n");
  1330. }
  1331. static void TableWithSpecifiedWidths()
  1332. {
  1333. Console.WriteLine("\tTableSpecifiedWidths()");
  1334. var _directoryWithFiles = "docs\\";
  1335. using (var output = File.Open(_directoryWithFiles + "TableSpecifiedWidths.docx", FileMode.Create))
  1336. {
  1337. using (var doc = DocX.Create(output))
  1338. {
  1339. var widths = new float[] { 200f, 100f, 300f };
  1340. var tbl = doc.InsertTable(1, widths.Length);
  1341. tbl.SetWidths(widths);
  1342. var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight;
  1343. tbl.AutoFit = AutoFit.Contents;
  1344. var r = tbl.Rows[0];
  1345. var cx = 0;
  1346. foreach (var cell in r.Cells)
  1347. {
  1348. cell.Paragraphs.First().Append("Col " + cx);
  1349. //cell.Width = colWidth;
  1350. cell.MarginBottom = 0;
  1351. cell.MarginLeft = 0;
  1352. cell.MarginRight = 0;
  1353. cell.MarginTop = 0;
  1354. cx++;
  1355. }
  1356. //add new rows
  1357. for (var x = 0; x < 5; x++)
  1358. {
  1359. r = tbl.InsertRow();
  1360. cx = 0;
  1361. foreach (var cell in r.Cells)
  1362. {
  1363. cell.Paragraphs.First().Append("Col " + cx);
  1364. //cell.Width = colWidth;
  1365. cell.MarginBottom = 0;
  1366. cell.MarginLeft = 0;
  1367. cell.MarginRight = 0;
  1368. cell.MarginTop = 0;
  1369. cx++;
  1370. }
  1371. }
  1372. tbl.SetBorder(TableBorderType.Bottom, BlankBorder);
  1373. tbl.SetBorder(TableBorderType.Left, BlankBorder);
  1374. tbl.SetBorder(TableBorderType.Right, BlankBorder);
  1375. tbl.SetBorder(TableBorderType.Top, BlankBorder);
  1376. tbl.SetBorder(TableBorderType.InsideV, BlankBorder);
  1377. tbl.SetBorder(TableBorderType.InsideH, BlankBorder);
  1378. doc.Save();
  1379. }
  1380. }
  1381. Console.WriteLine("\tCreated: docs\\TableSpecifiedWidths.docx\n");
  1382. }
  1383. }
  1384. }