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

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