Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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