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

UnitTest1.cs 52KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Novacode;
  7. using System.Reflection;
  8. using System.IO;
  9. using System.Drawing;
  10. using System.Drawing.Imaging;
  11. using System.Xml.Linq;
  12. using System.IO.Packaging;
  13. using System.Text.RegularExpressions;
  14. namespace UnitTests
  15. {
  16. /// <summary>
  17. /// Summary description for UnitTest1
  18. /// </summary>
  19. [TestClass]
  20. public class UnitTest1
  21. {
  22. // Get the fullpath to the executing assembly.
  23. string directory_executing_assembly;
  24. string directory_documents;
  25. string file_temp = "temp.docx";
  26. const string package_part_document = "/word/document.xml";
  27. public UnitTest1()
  28. {
  29. directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
  30. // The documents directory
  31. List<string> steps = directory_executing_assembly.Split('\\').ToList();
  32. steps.RemoveRange(steps.Count() - 3, 3);
  33. directory_documents = String.Join("\\", steps) + "\\documents\\";
  34. }
  35. [TestMethod]
  36. public void Test_Pattern_Replacement()
  37. {
  38. Dictionary<string, string> testPatterns = new Dictionary<string, string>()
  39. {
  40. {"COURT NAME","Fred Frump"},
  41. {"Case Number","cr-md-2011-1234567"}
  42. };
  43. using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
  44. {
  45. // Make sure the origional string are the document.
  46. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
  47. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
  48. // Make sure the new strings are not in the document.
  49. Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
  50. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
  51. // Do the replacing
  52. foreach (var p in testPatterns)
  53. replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
  54. // Make sure the origional string are no longer in the document.
  55. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
  56. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
  57. // Make sure the new strings are now in the document.
  58. Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
  59. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
  60. // Make sure the replacement worked.
  61. Assert.IsTrue(replaceDoc.Text == "\t\t\t\t\t\t\t\t\t\t\t\t\t\tThese two tables should look identical:\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSTATE OF IOWA,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlaintiff,\t\t\t\t\t\t\t\t\t\t\t\t\t\tvs.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFRED FRUMP,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tDefendant.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCase No.: cr-md-2011-1234567\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER SETTING ASIDE DEFAULT JUDGMENT\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSTATE OF IOWA,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlaintiff,\t\t\t\t\t\t\t\t\t\t\t\t\t\tvs.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFRED FRUMP,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tDefendant.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCase No.: cr-md-2011-1234567\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER SETTING ASIDE DEFAULT JUDGMENT\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
  62. }
  63. }
  64. [TestMethod]
  65. public void Test_EverybodyHasAHome_Loaded()
  66. {
  67. // Load a document.
  68. using (DocX document = DocX.Load(directory_documents + "EverybodyHasAHome.docx"))
  69. {
  70. // Main document tests.
  71. string document_xml_file = document.mainPart.Uri.OriginalString;
  72. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  73. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  74. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  75. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  76. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  77. // header first
  78. Header header_first = document.Headers.first;
  79. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  80. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  81. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  82. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  83. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  84. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  85. // header odd
  86. Header header_odd = document.Headers.odd;
  87. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  88. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  89. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  90. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  91. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  92. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  93. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  94. // header even
  95. Header header_even = document.Headers.even;
  96. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  97. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  98. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  99. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  100. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  101. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  102. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  103. // footer first
  104. Footer footer_first = document.Footers.first;
  105. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  106. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  107. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  108. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  109. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  110. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  111. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  112. // footer odd
  113. Footer footer_odd = document.Footers.odd;
  114. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  115. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  116. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  117. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  118. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  119. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  120. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  121. // footer even
  122. Footer footer_even = document.Footers.even;
  123. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  124. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  125. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  126. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  127. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  128. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  129. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  130. }
  131. }
  132. [TestMethod]
  133. public void Test_Insert_Picture_ParagraphBeforeSelf()
  134. {
  135. // Create test document.
  136. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  137. {
  138. // Add an Image to this document.
  139. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  140. // Create a Picture from this Image.
  141. Picture pic = img.CreatePicture();
  142. Assert.IsNotNull(pic);
  143. // Main document.
  144. Paragraph p0 = document.InsertParagraph("Hello");
  145. Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
  146. p1.InsertPicture(pic, 3);
  147. // Save this document.
  148. document.Save();
  149. }
  150. }
  151. [TestMethod]
  152. public void Test_Insert_Picture_ParagraphAfterSelf()
  153. {
  154. // Create test document.
  155. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  156. {
  157. // Add an Image to this document.
  158. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  159. // Create a Picture from this Image.
  160. Picture pic = img.CreatePicture();
  161. Assert.IsNotNull(pic);
  162. // Main document.
  163. Paragraph p0 = document.InsertParagraph("Hello");
  164. Paragraph p1 = p0.InsertParagraphAfterSelf("again");
  165. p1.InsertPicture(pic, 3);
  166. // Save this document.
  167. document.Save();
  168. }
  169. }
  170. [TestMethod]
  171. public void Test_EverybodyHasAHome_Created()
  172. {
  173. // Create a new document.
  174. using (DocX document = DocX.Create("Test.docx"))
  175. {
  176. // Create a Table.
  177. Table t = document.AddTable(3, 3);
  178. t.Design = TableDesign.TableGrid;
  179. // Insert a Paragraph and a Table into the main document.
  180. document.InsertParagraph();
  181. document.InsertTable(t);
  182. // Insert a Paragraph and a Table into every Header.
  183. document.AddHeaders();
  184. document.Headers.odd.InsertParagraph();
  185. document.Headers.odd.InsertTable(t);
  186. document.Headers.even.InsertParagraph();
  187. document.Headers.even.InsertTable(t);
  188. document.Headers.first.InsertParagraph();
  189. document.Headers.first.InsertTable(t);
  190. // Insert a Paragraph and a Table into every Footer.
  191. document.AddFooters();
  192. document.Footers.odd.InsertParagraph();
  193. document.Footers.odd.InsertTable(t);
  194. document.Footers.even.InsertParagraph();
  195. document.Footers.even.InsertTable(t);
  196. document.Footers.first.InsertParagraph();
  197. document.Footers.first.InsertTable(t);
  198. // Main document tests.
  199. string document_xml_file = document.mainPart.Uri.OriginalString;
  200. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  201. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  202. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  203. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  204. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  205. // header first
  206. Header header_first = document.Headers.first;
  207. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  208. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  209. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  210. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  211. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  212. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  213. // header odd
  214. Header header_odd = document.Headers.odd;
  215. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  216. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  217. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  218. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  219. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  220. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  221. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  222. // header even
  223. Header header_even = document.Headers.even;
  224. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  225. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  226. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  227. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  228. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  229. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  230. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  231. // footer first
  232. Footer footer_first = document.Footers.first;
  233. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  234. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  235. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  236. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  237. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  238. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  239. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  240. // footer odd
  241. Footer footer_odd = document.Footers.odd;
  242. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  243. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  244. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  245. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  246. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  247. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  248. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  249. // footer even
  250. Footer footer_even = document.Footers.even;
  251. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  252. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  253. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  254. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  255. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  256. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  257. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  258. }
  259. }
  260. [TestMethod]
  261. public void Test_Document_AddImage_FromDisk()
  262. {
  263. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  264. {
  265. // Add a png to into this document
  266. Novacode.Image png = document.AddImage(directory_documents + "purple.png");
  267. Assert.IsTrue(document.Images.Count == 1);
  268. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
  269. // Add a tiff into to this document
  270. Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
  271. Assert.IsTrue(document.Images.Count == 2);
  272. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
  273. // Add a gif into to this document
  274. Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
  275. Assert.IsTrue(document.Images.Count == 3);
  276. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
  277. // Add a jpg into to this document
  278. Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
  279. Assert.IsTrue(document.Images.Count == 4);
  280. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
  281. // Add a bitmap to this document
  282. Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
  283. Assert.IsTrue(document.Images.Count == 5);
  284. // Word does not allow bmp make sure it was inserted as a png.
  285. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
  286. }
  287. }
  288. [TestMethod]
  289. public void Test_Document_AddImage_FromStream()
  290. {
  291. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  292. {
  293. // DocX will always insert Images that come from Streams as jpeg.
  294. // Add a png to into this document
  295. Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
  296. Assert.IsTrue(document.Images.Count == 1);
  297. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
  298. // Add a tiff into to this document
  299. Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
  300. Assert.IsTrue(document.Images.Count == 2);
  301. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
  302. // Add a gif into to this document
  303. Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
  304. Assert.IsTrue(document.Images.Count == 3);
  305. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
  306. // Add a jpg into to this document
  307. Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
  308. Assert.IsTrue(document.Images.Count == 4);
  309. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
  310. // Add a bitmap to this document
  311. Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
  312. Assert.IsTrue(document.Images.Count == 5);
  313. // Word does not allow bmp make sure it was inserted as a png.
  314. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
  315. }
  316. }
  317. [TestMethod]
  318. public void Test_Tables()
  319. {
  320. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  321. {
  322. // There is only one Paragraph at the document level.
  323. Assert.IsTrue(document.Paragraphs.Count() == 13);
  324. // There is only one Table in this document.
  325. Assert.IsTrue(document.Tables.Count() == 1);
  326. // Extract the only Table.
  327. Table t0 = document.Tables[0];
  328. // This table has 12 Paragraphs.
  329. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  330. }
  331. }
  332. [TestMethod]
  333. public void Test_Images()
  334. {
  335. using (DocX document = DocX.Load(directory_documents + "Images.docx"))
  336. {
  337. // Extract Images from Document.
  338. List<Novacode.Image> document_images = document.Images;
  339. // Make sure there are 3 Images in this document.
  340. Assert.IsTrue(document_images.Count() == 3);
  341. // Extract the headers from this document.
  342. Headers headers = document.Headers;
  343. Header header_first = headers.first;
  344. Header header_odd = headers.odd;
  345. Header header_even = headers.even;
  346. #region Header_First
  347. // Extract Images from the first Header.
  348. List<Novacode.Image> header_first_images = header_first.Images;
  349. // Make sure there is 1 Image in the first header.
  350. Assert.IsTrue(header_first_images.Count() == 1);
  351. #endregion
  352. #region Header_Odd
  353. // Extract Images from the odd Header.
  354. List<Novacode.Image> header_odd_images = header_odd.Images;
  355. // Make sure there is 1 Image in the first header.
  356. Assert.IsTrue(header_odd_images.Count() == 1);
  357. #endregion
  358. #region Header_Even
  359. // Extract Images from the odd Header.
  360. List<Novacode.Image> header_even_images = header_even.Images;
  361. // Make sure there is 1 Image in the first header.
  362. Assert.IsTrue(header_even_images.Count() == 1);
  363. #endregion
  364. }
  365. }
  366. [TestMethod]
  367. public void Test_Insert_Picture()
  368. {
  369. // Load test document.
  370. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  371. {
  372. // Add Headers and Footers into this document.
  373. document.AddHeaders();
  374. document.AddFooters();
  375. document.DifferentFirstPage = true;
  376. document.DifferentOddAndEvenPages = true;
  377. // Add an Image to this document.
  378. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  379. // Create a Picture from this Image.
  380. Picture pic = img.CreatePicture();
  381. // Main document.
  382. Paragraph p0 = document.InsertParagraph("Hello");
  383. p0.InsertPicture(pic, 3);
  384. // Header first.
  385. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  386. p1.InsertPicture(pic, 2);
  387. // Header odd.
  388. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  389. p2.InsertPicture(pic, 2);
  390. // Header even.
  391. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  392. p3.InsertPicture(pic, 2);
  393. // Footer first.
  394. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  395. p4.InsertPicture(pic, 2);
  396. // Footer odd.
  397. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  398. p5.InsertPicture(pic, 2);
  399. // Footer even.
  400. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  401. p6.InsertPicture(pic, 2);
  402. // Save this document.
  403. document.Save();
  404. }
  405. }
  406. [TestMethod]
  407. public void Test_Insert_Hyperlink()
  408. {
  409. // Load test document.
  410. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  411. {
  412. // Add Headers and Footers into this document.
  413. document.AddHeaders();
  414. document.AddFooters();
  415. document.DifferentFirstPage = true;
  416. document.DifferentOddAndEvenPages = true;
  417. // Add a Hyperlink into this document.
  418. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  419. // Main document.
  420. Paragraph p0 = document.InsertParagraph("Hello");
  421. p0.InsertHyperlink(h, 3);
  422. // Header first.
  423. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  424. p1.InsertHyperlink(h, 3);
  425. // Header odd.
  426. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  427. p2.InsertHyperlink(h, 3);
  428. // Header even.
  429. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  430. p3.InsertHyperlink(h, 3);
  431. // Footer first.
  432. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  433. p4.InsertHyperlink(h, 3);
  434. // Footer odd.
  435. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  436. p5.InsertHyperlink(h, 3);
  437. // Footer even.
  438. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  439. p6.InsertHyperlink(h, 3);
  440. // Save this document.
  441. document.Save();
  442. }
  443. }
  444. [TestMethod]
  445. public void Test_Append_Hyperlink()
  446. {
  447. // Load test document.
  448. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  449. {
  450. // Add Headers and Footers into this document.
  451. document.AddHeaders();
  452. document.AddFooters();
  453. document.DifferentFirstPage = true;
  454. document.DifferentOddAndEvenPages = true;
  455. // Add a Hyperlink to this document.
  456. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  457. // Main document.
  458. Paragraph p0 = document.InsertParagraph("----");
  459. p0.AppendHyperlink(h);
  460. Assert.IsTrue(p0.Text == "----google");
  461. // Header first.
  462. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  463. p1.AppendHyperlink(h);
  464. Assert.IsTrue(p1.Text == "----google");
  465. // Header odd.
  466. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  467. p2.AppendHyperlink(h);
  468. Assert.IsTrue(p2.Text == "----google");
  469. // Header even.
  470. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  471. p3.AppendHyperlink(h);
  472. Assert.IsTrue(p3.Text == "----google");
  473. // Footer first.
  474. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  475. p4.AppendHyperlink(h);
  476. Assert.IsTrue(p4.Text == "----google");
  477. // Footer odd.
  478. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  479. p5.AppendHyperlink(h);
  480. Assert.IsTrue(p5.Text == "----google");
  481. // Footer even.
  482. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  483. p6.AppendHyperlink(h);
  484. Assert.IsTrue(p6.Text == "----google");
  485. // Save the document.
  486. document.Save();
  487. }
  488. }
  489. [TestMethod]
  490. public void Test_Append_Picture()
  491. {
  492. // Create test document.
  493. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  494. {
  495. // Add Headers and Footers into this document.
  496. document.AddHeaders();
  497. document.AddFooters();
  498. document.DifferentFirstPage = true;
  499. document.DifferentOddAndEvenPages = true;
  500. // Add an Image to this document.
  501. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  502. // Create a Picture from this Image.
  503. Picture pic = img.CreatePicture();
  504. // Main document.
  505. Paragraph p0 = document.InsertParagraph();
  506. p0.AppendPicture(pic);
  507. // Header first.
  508. Paragraph p1 = document.Headers.first.InsertParagraph();
  509. p1.AppendPicture(pic);
  510. // Header odd.
  511. Paragraph p2 = document.Headers.odd.InsertParagraph();
  512. p2.AppendPicture(pic);
  513. // Header even.
  514. Paragraph p3 = document.Headers.even.InsertParagraph();
  515. p3.AppendPicture(pic);
  516. // Footer first.
  517. Paragraph p4 = document.Footers.first.InsertParagraph();
  518. p4.AppendPicture(pic);
  519. // Footer odd.
  520. Paragraph p5 = document.Footers.odd.InsertParagraph();
  521. p5.AppendPicture(pic);
  522. // Footer even.
  523. Paragraph p6 = document.Footers.even.InsertParagraph();
  524. p6.AppendPicture(pic);
  525. // Save the document.
  526. document.Save();
  527. }
  528. }
  529. [TestMethod]
  530. public void Test_Move_Picture_Load()
  531. {
  532. // Load test document.
  533. using (DocX document = DocX.Load(directory_documents + "MovePicture.docx"))
  534. {
  535. // Extract the first Picture from the first Paragraph.
  536. Picture picture = document.Paragraphs.First().Pictures.First();
  537. // Move it into the first Header.
  538. Header header_first = document.Headers.first;
  539. header_first.Paragraphs.First().AppendPicture(picture);
  540. // Move it into the even Header.
  541. Header header_even = document.Headers.even;
  542. header_even.Paragraphs.First().AppendPicture(picture);
  543. // Move it into the odd Header.
  544. Header header_odd = document.Headers.odd;
  545. header_odd.Paragraphs.First().AppendPicture(picture);
  546. // Move it into the first Footer.
  547. Footer footer_first = document.Footers.first;
  548. footer_first.Paragraphs.First().AppendPicture(picture);
  549. // Move it into the even Footer.
  550. Footer footer_even = document.Footers.even;
  551. footer_even.Paragraphs.First().AppendPicture(picture);
  552. // Move it into the odd Footer.
  553. Footer footer_odd = document.Footers.odd;
  554. footer_odd.Paragraphs.First().AppendPicture(picture);
  555. // Save this as MovedPicture.docx
  556. document.SaveAs(directory_documents + "MovedPicture.docx");
  557. }
  558. }
  559. [TestMethod]
  560. public void Test_Paragraph_InsertHyperlink()
  561. {
  562. // Create a new document
  563. using (DocX document = DocX.Create("Test.docx"))
  564. {
  565. // Add a Hyperlink to this document.
  566. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  567. // Simple
  568. Paragraph p1 = document.InsertParagraph("AC");
  569. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  570. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  571. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  572. // Difficult
  573. Paragraph p2 = document.InsertParagraph("\tA\tC\t");
  574. p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
  575. p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
  576. p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
  577. // Contrived
  578. // Add a contrived Hyperlink to this document.
  579. Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
  580. Paragraph p3 = document.InsertParagraph("\tA\tC\t");
  581. p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
  582. p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
  583. p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
  584. }
  585. }
  586. [TestMethod]
  587. public void Test_Paragraph_RemoveHyperlink()
  588. {
  589. // Create a new document
  590. using (DocX document = DocX.Create("Test.docx"))
  591. {
  592. // Add a Hyperlink to this document.
  593. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  594. // Simple
  595. Paragraph p1 = document.InsertParagraph("AC");
  596. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  597. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  598. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  599. // Try and remove a Hyperlink using a negative index.
  600. // This should throw an exception.
  601. try
  602. {
  603. p1.RemoveHyperlink(-1);
  604. Assert.Fail();
  605. }
  606. catch (ArgumentException) { }
  607. catch (Exception) { Assert.Fail(); }
  608. // Try and remove a Hyperlink at an index greater than the last.
  609. // This should throw an exception.
  610. try
  611. {
  612. p1.RemoveHyperlink(3);
  613. Assert.Fail();
  614. }
  615. catch (ArgumentException) {}
  616. catch (Exception) { Assert.Fail(); }
  617. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  618. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  619. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  620. }
  621. }
  622. [TestMethod]
  623. public void Test_Paragraph_ReplaceText()
  624. {
  625. // Create a new document
  626. using (DocX document = DocX.Create("Test.docx"))
  627. {
  628. // Simple
  629. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  630. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  631. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  632. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  633. // Try and replace text that dosen't exist in the Paragraph.
  634. string old = p1.Text;
  635. p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
  636. // Difficult
  637. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  638. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  639. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  640. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  641. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  642. }
  643. }
  644. [TestMethod]
  645. public void Test_Paragraph_RemoveText()
  646. {
  647. // Create a new document
  648. using (DocX document = DocX.Create("Test.docx"))
  649. {
  650. // Simple
  651. //<p>
  652. // <r><t>HellWorld</t></r>
  653. //</p>
  654. Paragraph p1 = document.InsertParagraph("HelloWorld");
  655. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  656. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  657. p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
  658. // Try and remove text at an index greater than the last.
  659. // This should throw an exception.
  660. try
  661. {
  662. p1.RemoveText(p1.Text.Length, 1);
  663. Assert.Fail();
  664. }
  665. catch (ArgumentOutOfRangeException) { }
  666. catch (Exception) { Assert.Fail(); }
  667. // Try and remove text at a negative index.
  668. // This should throw an exception.
  669. try
  670. {
  671. p1.RemoveText(-1, 1);
  672. Assert.Fail();
  673. }
  674. catch (ArgumentOutOfRangeException) { }
  675. catch (Exception) { Assert.Fail(); }
  676. // Difficult
  677. //<p>
  678. // <r><t>A</t></r>
  679. // <r><t>B</t></r>
  680. // <r><t>C</t></r>
  681. //</p>
  682. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  683. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  684. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  685. p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
  686. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  687. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  688. // Contrived 1
  689. //<p>
  690. // <r>
  691. // <t>A</t>
  692. // <t>B</t>
  693. // <t>C</t>
  694. // </r>
  695. //</p>
  696. Paragraph p3 = document.InsertParagraph("");
  697. p3.Xml = XElement.Parse
  698. (
  699. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  700. <w:pPr />
  701. <w:r>
  702. <w:rPr />
  703. <w:t>A</w:t>
  704. <w:t>B</w:t>
  705. <w:t>C</w:t>
  706. </w:r>
  707. </w:p>"
  708. );
  709. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  710. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  711. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  712. // Contrived 2
  713. //<p>
  714. // <r>
  715. // <t>A</t>
  716. // <t>B</t>
  717. // <t>C</t>
  718. // </r>
  719. //</p>
  720. Paragraph p4 = document.InsertParagraph("");
  721. p4.Xml = XElement.Parse
  722. (
  723. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  724. <w:pPr />
  725. <w:r>
  726. <w:rPr />
  727. <tab />
  728. <w:t>A</w:t>
  729. <tab />
  730. </w:r>
  731. <w:r>
  732. <w:rPr />
  733. <tab />
  734. <w:t>B</w:t>
  735. <tab />
  736. </w:r>
  737. </w:p>"
  738. );
  739. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  740. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  741. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  742. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  743. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  744. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  745. }
  746. }
  747. [TestMethod]
  748. public void Test_Paragraph_InsertText()
  749. {
  750. // Create a new document
  751. using (DocX document = DocX.Create("Test.docx"))
  752. {
  753. // Simple
  754. //<p>
  755. // <r><t>HelloWorld</t></r>
  756. //</p>
  757. Paragraph p1 = document.InsertParagraph("HelloWorld");
  758. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  759. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  760. p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  761. // Try and insert text at an index greater than the last + 1.
  762. // This should throw an exception.
  763. try
  764. {
  765. p1.InsertText(p1.Text.Length + 1, "-");
  766. Assert.Fail();
  767. }
  768. catch (ArgumentOutOfRangeException) { }
  769. catch (Exception) { Assert.Fail(); }
  770. // Try and insert text at a negative index.
  771. // This should throw an exception.
  772. try
  773. {
  774. p1.InsertText(-1, "-");
  775. Assert.Fail();
  776. }
  777. catch (ArgumentOutOfRangeException) { }
  778. catch (Exception) { Assert.Fail(); }
  779. // Difficult
  780. //<p>
  781. // <r><t>A</t></r>
  782. // <r><t>B</t></r>
  783. // <r><t>C</t></r>
  784. //</p>
  785. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  786. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  787. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  788. p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  789. p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  790. // Contrived 1
  791. //<p>
  792. // <r>
  793. // <t>A</t>
  794. // <t>B</t>
  795. // <t>C</t>
  796. // </r>
  797. //</p>
  798. Paragraph p3 = document.InsertParagraph("");
  799. p3.Xml = XElement.Parse
  800. (
  801. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  802. <w:pPr />
  803. <w:r>
  804. <w:rPr />
  805. <w:t>A</w:t>
  806. <w:t>B</w:t>
  807. <w:t>C</w:t>
  808. </w:r>
  809. </w:p>"
  810. );
  811. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  812. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  813. p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  814. p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  815. // Contrived 2
  816. //<p>
  817. // <r>
  818. // <t>A</t>
  819. // <t>B</t>
  820. // <t>C</t>
  821. // </r>
  822. //</p>
  823. Paragraph p4 = document.InsertParagraph("");
  824. p4.Xml = XElement.Parse
  825. (
  826. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  827. <w:pPr />
  828. <w:r>
  829. <w:rPr />
  830. <w:t>A</w:t>
  831. <w:t>B</w:t>
  832. <w:t>C</w:t>
  833. </w:r>
  834. </w:p>"
  835. );
  836. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  837. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  838. p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  839. p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  840. }
  841. }
  842. [TestMethod]
  843. public void Test_Document_Paragraphs()
  844. {
  845. // Load the document 'Paragraphs.docx'
  846. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  847. {
  848. // Extract the Paragraphs from this document.
  849. List<Paragraph> paragraphs = document.Paragraphs;
  850. // There should be 3 Paragraphs in this document.
  851. Assert.IsTrue(paragraphs.Count() == 3);
  852. // Extract the 3 Paragraphs.
  853. Paragraph p1 = paragraphs[0];
  854. Paragraph p2 = paragraphs[1];
  855. Paragraph p3 = paragraphs[2];
  856. // Extract their Text properties.
  857. string p1_text = p1.Text;
  858. string p2_text = p2.Text;
  859. string p3_text = p3.Text;
  860. // Test their Text properties against absolutes.
  861. Assert.IsTrue(p1_text == "Paragraph 1");
  862. Assert.IsTrue(p2_text == "Paragraph 2");
  863. Assert.IsTrue(p3_text == "Paragraph 3");
  864. // Its important that each Paragraph knows the PackagePart it belongs to.
  865. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  866. // Test the saving of the document.
  867. document.SaveAs(file_temp);
  868. }
  869. // Delete the tempory file.
  870. File.Delete(file_temp);
  871. }
  872. [TestMethod]
  873. public void Test_Table_InsertRow()
  874. {
  875. using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
  876. {
  877. // Add a Table to a document.
  878. Table t = document.AddTable(2, 2);
  879. t.Design = TableDesign.TableGrid;
  880. // Insert the Table into the main section of the document.
  881. Table t1 = document.InsertTable(t);
  882. t1.InsertRow();
  883. t1.InsertColumn();
  884. // Save the document.
  885. document.Save();
  886. }
  887. }
  888. [TestMethod]
  889. public void Test_Document_ApplyTemplate()
  890. {
  891. using (MemoryStream documentStream = new MemoryStream())
  892. {
  893. using (DocX document = DocX.Create(documentStream))
  894. {
  895. document.ApplyTemplate(directory_documents + "Template.dotx");
  896. document.Save();
  897. Header firstHeader = document.Headers.first;
  898. Header oddHeader = document.Headers.odd;
  899. Header evenHeader = document.Headers.even;
  900. Footer firstFooter = document.Footers.first;
  901. Footer oddFooter = document.Footers.odd;
  902. Footer evenFooter = document.Footers.even;
  903. Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
  904. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  905. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  906. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  907. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  908. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  909. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  910. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  911. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  912. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  913. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  914. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  915. Paragraph firstParagraph = document.Paragraphs[0];
  916. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  917. }
  918. }
  919. }
  920. }
  921. }