Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UnitTest1.cs 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. namespace UnitTests
  13. {
  14. /// <summary>
  15. /// Summary description for UnitTest1
  16. /// </summary>
  17. [TestClass]
  18. public class UnitTest1
  19. {
  20. // Get the fullpath to the executing assembly.
  21. string directory_executing_assembly;
  22. string directory_documents;
  23. string file_temp = "temp.docx";
  24. const string package_part_document = "/word/document.xml";
  25. public UnitTest1()
  26. {
  27. directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
  28. // The documents directory
  29. List<string> steps = directory_executing_assembly.Split('\\').ToList();
  30. steps.RemoveRange(steps.Count() - 3, 3);
  31. directory_documents = String.Join("\\", steps) + "\\documents\\";
  32. }
  33. [TestMethod]
  34. public void Test_Document_AddImage_FromDisk()
  35. {
  36. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  37. {
  38. // Add a png to into this document
  39. Novacode.Image png = document.AddImage(directory_documents + "purple.png");
  40. Assert.IsTrue(document.Images.Count == 1);
  41. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
  42. // Add a tiff into to this document
  43. Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
  44. Assert.IsTrue(document.Images.Count == 2);
  45. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
  46. // Add a gif into to this document
  47. Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
  48. Assert.IsTrue(document.Images.Count == 3);
  49. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
  50. // Add a jpg into to this document
  51. Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
  52. Assert.IsTrue(document.Images.Count == 4);
  53. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
  54. // Add a bitmap to this document
  55. Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
  56. Assert.IsTrue(document.Images.Count == 5);
  57. // Word does not allow bmp make sure it was inserted as a png.
  58. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
  59. }
  60. }
  61. [TestMethod]
  62. public void Test_Document_AddImage_FromStream()
  63. {
  64. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  65. {
  66. // DocX will always insert Images that come from Streams as jpeg.
  67. // Add a png to into this document
  68. Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
  69. Assert.IsTrue(document.Images.Count == 1);
  70. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
  71. // Add a tiff into to this document
  72. Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
  73. Assert.IsTrue(document.Images.Count == 2);
  74. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
  75. // Add a gif into to this document
  76. Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
  77. Assert.IsTrue(document.Images.Count == 3);
  78. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
  79. // Add a jpg into to this document
  80. Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
  81. Assert.IsTrue(document.Images.Count == 4);
  82. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
  83. // Add a bitmap to this document
  84. Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
  85. Assert.IsTrue(document.Images.Count == 5);
  86. // Word does not allow bmp make sure it was inserted as a png.
  87. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
  88. }
  89. }
  90. [TestMethod]
  91. public void Test_Tables()
  92. {
  93. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  94. {
  95. // There is only one Paragraph at the document level.
  96. Assert.IsTrue(document.Paragraphs.Count() == 13);
  97. // There is only one Table in this document.
  98. Assert.IsTrue(document.Tables.Count() == 1);
  99. // Extract the only Table.
  100. Table t0 = document.Tables[0];
  101. // This table has 12 Paragraphs.
  102. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  103. }
  104. }
  105. [TestMethod]
  106. public void Test_Images()
  107. {
  108. using (DocX document = DocX.Load(directory_documents + "Images.docx"))
  109. {
  110. // Extract Images from Document.
  111. List<Novacode.Image> document_images = document.Images;
  112. // Make sure there are 3 Images in this document.
  113. Assert.IsTrue(document_images.Count() == 3);
  114. // Extract the headers from this document.
  115. Headers headers = document.Headers;
  116. Header header_first = headers.first;
  117. Header header_odd = headers.odd;
  118. Header header_even = headers.even;
  119. #region Header_First
  120. // Extract Images from the first Header.
  121. List<Novacode.Image> header_first_images = header_first.Images;
  122. // Make sure there is 1 Image in the first header.
  123. Assert.IsTrue(header_first_images.Count() == 1);
  124. #endregion
  125. #region Header_Odd
  126. // Extract Images from the odd Header.
  127. List<Novacode.Image> header_odd_images = header_odd.Images;
  128. // Make sure there is 1 Image in the first header.
  129. Assert.IsTrue(header_odd_images.Count() == 1);
  130. #endregion
  131. #region Header_Even
  132. // Extract Images from the odd Header.
  133. List<Novacode.Image> header_even_images = header_even.Images;
  134. // Make sure there is 1 Image in the first header.
  135. Assert.IsTrue(header_even_images.Count() == 1);
  136. #endregion
  137. }
  138. }
  139. // Write the string "Hello World" into this Image.
  140. private static void CoolExample(Novacode.Image i, Stream s, string str)
  141. {
  142. // Write "Hello World" into this Image.
  143. Bitmap b = new Bitmap(s);
  144. /*
  145. * Get the Graphics object for this Bitmap.
  146. * The Graphics object provides functions for drawing.
  147. */
  148. Graphics g = Graphics.FromImage(b);
  149. // Draw the string "Hello World".
  150. g.DrawString
  151. (
  152. str,
  153. new Font("Tahoma", 20),
  154. Brushes.Blue,
  155. new PointF(0, 0)
  156. );
  157. // Save this Bitmap back into the document using a Create\Write stream.
  158. b.Save(i.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
  159. }
  160. [TestMethod]
  161. public void Test_Paragraph_InsertHyperlink()
  162. {
  163. // Create a new document
  164. using (DocX document = DocX.Create("Test.docx"))
  165. {
  166. // Add a Hyperlink to this document.
  167. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  168. // Simple
  169. Paragraph p1 = document.InsertParagraph("AC");
  170. p1.InsertHyperlink(0, h); Assert.IsTrue(p1.Text == "linkAC");
  171. p1.InsertHyperlink(p1.Text.Length, h); Assert.IsTrue(p1.Text == "linkAClink");
  172. p1.InsertHyperlink(p1.Text.IndexOf("C"), h); Assert.IsTrue(p1.Text == "linkAlinkClink");
  173. // Difficult
  174. Paragraph p2 = document.InsertParagraph("\tA\tC\t");
  175. p2.InsertHyperlink(0, h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
  176. p2.InsertHyperlink(p2.Text.Length, h); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
  177. p2.InsertHyperlink(p2.Text.IndexOf("C"), h); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
  178. // Contrived
  179. // Add a contrived Hyperlink to this document.
  180. Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
  181. Paragraph p3 = document.InsertParagraph("\tA\tC\t");
  182. p3.InsertHyperlink(0, h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
  183. p3.InsertHyperlink(p3.Text.Length, h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
  184. p3.InsertHyperlink(p3.Text.IndexOf("C"), h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
  185. }
  186. }
  187. [TestMethod]
  188. public void Test_Paragraph_RemoveHyperlink()
  189. {
  190. // Create a new document
  191. using (DocX document = DocX.Create("Test.docx"))
  192. {
  193. // Add a Hyperlink to this document.
  194. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  195. // Simple
  196. Paragraph p1 = document.InsertParagraph("AC");
  197. p1.InsertHyperlink(0, h); Assert.IsTrue(p1.Text == "linkAC");
  198. p1.InsertHyperlink(p1.Text.Length, h); Assert.IsTrue(p1.Text == "linkAClink");
  199. p1.InsertHyperlink(p1.Text.IndexOf("C"), h); Assert.IsTrue(p1.Text == "linkAlinkClink");
  200. // Try and remove a Hyperlink using a negative index.
  201. // This should throw an exception.
  202. try
  203. {
  204. p1.RemoveHyperlink(-1);
  205. Assert.Fail();
  206. }
  207. catch (ArgumentException e) { }
  208. catch (Exception e) { Assert.Fail(); }
  209. // Try and remove a Hyperlink at an index greater than the last.
  210. // This should throw an exception.
  211. try
  212. {
  213. p1.RemoveHyperlink(3);
  214. Assert.Fail();
  215. }
  216. catch (ArgumentException e) {}
  217. catch (Exception e) { Assert.Fail(); }
  218. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  219. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  220. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  221. }
  222. }
  223. [TestMethod]
  224. public void Test_Paragraph_ReplaceText()
  225. {
  226. // Create a new document
  227. using (DocX document = DocX.Create("Test.docx"))
  228. {
  229. // Simple
  230. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  231. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  232. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  233. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  234. // Try and replace text that dosen't exist in the Paragraph.
  235. string old = p1.Text;
  236. p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
  237. // Difficult
  238. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  239. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  240. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  241. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  242. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  243. }
  244. }
  245. [TestMethod]
  246. public void Test_Paragraph_RemoveText()
  247. {
  248. // Create a new document
  249. using (DocX document = DocX.Create("Test.docx"))
  250. {
  251. // Simple
  252. //<p>
  253. // <r><t>HellWorld</t></r>
  254. //</p>
  255. Paragraph p1 = document.InsertParagraph("HelloWorld");
  256. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  257. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  258. p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
  259. // Try and remove text at an index greater than the last.
  260. // This should throw an exception.
  261. try
  262. {
  263. p1.RemoveText(p1.Text.Length, 1);
  264. Assert.Fail();
  265. }
  266. catch (ArgumentOutOfRangeException e) { }
  267. catch (Exception e) { Assert.Fail(); }
  268. // Try and remove text at a negative index.
  269. // This should throw an exception.
  270. try
  271. {
  272. p1.RemoveText(-1, 1);
  273. Assert.Fail();
  274. }
  275. catch (ArgumentOutOfRangeException e) { }
  276. catch (Exception e) { Assert.Fail(); }
  277. // Difficult
  278. //<p>
  279. // <r><t>A</t></r>
  280. // <r><t>B</t></r>
  281. // <r><t>C</t></r>
  282. //</p>
  283. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  284. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  285. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  286. p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
  287. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  288. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  289. // Contrived 1
  290. //<p>
  291. // <r>
  292. // <t>A</t>
  293. // <t>B</t>
  294. // <t>C</t>
  295. // </r>
  296. //</p>
  297. Paragraph p3 = document.InsertParagraph("");
  298. p3.Xml = XElement.Parse
  299. (
  300. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  301. <w:pPr />
  302. <w:r>
  303. <w:rPr />
  304. <w:t>A</w:t>
  305. <w:t>B</w:t>
  306. <w:t>C</w:t>
  307. </w:r>
  308. </w:p>"
  309. );
  310. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  311. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  312. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  313. // Contrived 2
  314. //<p>
  315. // <r>
  316. // <t>A</t>
  317. // <t>B</t>
  318. // <t>C</t>
  319. // </r>
  320. //</p>
  321. Paragraph p4 = document.InsertParagraph("");
  322. p4.Xml = XElement.Parse
  323. (
  324. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  325. <w:pPr />
  326. <w:r>
  327. <w:rPr />
  328. <tab />
  329. <w:t>A</w:t>
  330. <tab />
  331. </w:r>
  332. <w:r>
  333. <w:rPr />
  334. <tab />
  335. <w:t>B</w:t>
  336. <tab />
  337. </w:r>
  338. </w:p>"
  339. );
  340. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  341. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  342. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  343. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  344. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  345. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  346. }
  347. }
  348. [TestMethod]
  349. public void Test_Paragraph_InsertText()
  350. {
  351. // Create a new document
  352. using (DocX document = DocX.Create("Test.docx"))
  353. {
  354. // Simple
  355. //<p>
  356. // <r><t>HelloWorld</t></r>
  357. //</p>
  358. Paragraph p1 = document.InsertParagraph("HelloWorld");
  359. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  360. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  361. p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  362. // Try and insert text at an index greater than the last + 1.
  363. // This should throw an exception.
  364. try
  365. {
  366. p1.InsertText(p1.Text.Length + 1, "-");
  367. Assert.Fail();
  368. }
  369. catch (ArgumentOutOfRangeException e) { }
  370. catch (Exception e) { Assert.Fail(); }
  371. // Try and insert text at a negative index.
  372. // This should throw an exception.
  373. try
  374. {
  375. p1.InsertText(-1, "-");
  376. Assert.Fail();
  377. }
  378. catch (ArgumentOutOfRangeException e) { }
  379. catch (Exception e) { Assert.Fail(); }
  380. // Difficult
  381. //<p>
  382. // <r><t>A</t></r>
  383. // <r><t>B</t></r>
  384. // <r><t>C</t></r>
  385. //</p>
  386. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  387. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  388. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  389. p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  390. p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  391. // Contrived 1
  392. //<p>
  393. // <r>
  394. // <t>A</t>
  395. // <t>B</t>
  396. // <t>C</t>
  397. // </r>
  398. //</p>
  399. Paragraph p3 = document.InsertParagraph("");
  400. p3.Xml = XElement.Parse
  401. (
  402. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  403. <w:pPr />
  404. <w:r>
  405. <w:rPr />
  406. <w:t>A</w:t>
  407. <w:t>B</w:t>
  408. <w:t>C</w:t>
  409. </w:r>
  410. </w:p>"
  411. );
  412. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  413. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  414. p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  415. p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  416. // Contrived 2
  417. //<p>
  418. // <r>
  419. // <t>A</t>
  420. // <t>B</t>
  421. // <t>C</t>
  422. // </r>
  423. //</p>
  424. Paragraph p4 = document.InsertParagraph("");
  425. p4.Xml = XElement.Parse
  426. (
  427. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  428. <w:pPr />
  429. <w:r>
  430. <w:rPr />
  431. <w:t>A</w:t>
  432. <w:t>B</w:t>
  433. <w:t>C</w:t>
  434. </w:r>
  435. </w:p>"
  436. );
  437. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  438. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  439. p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  440. p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  441. }
  442. }
  443. [TestMethod]
  444. public void Test_Document_Paragraphs()
  445. {
  446. // Load the document 'Paragraphs.docx'
  447. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  448. {
  449. // Extract the Paragraphs from this document.
  450. List<Paragraph> paragraphs = document.Paragraphs;
  451. // There should be 3 Paragraphs in this document.
  452. Assert.IsTrue(paragraphs.Count() == 3);
  453. // Extract the 3 Paragraphs.
  454. Paragraph p1 = paragraphs[0];
  455. Paragraph p2 = paragraphs[1];
  456. Paragraph p3 = paragraphs[2];
  457. // Extract their Text properties.
  458. string p1_text = p1.Text;
  459. string p2_text = p2.Text;
  460. string p3_text = p3.Text;
  461. // Test their Text properties against absolutes.
  462. Assert.IsTrue(p1_text == "Paragraph 1");
  463. Assert.IsTrue(p2_text == "Paragraph 2");
  464. Assert.IsTrue(p3_text == "Paragraph 3");
  465. // Its important that each Paragraph knows the PackagePart it belongs to.
  466. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  467. // Test the saving of the document.
  468. document.SaveAs(file_temp);
  469. }
  470. // Delete the tempory file.
  471. File.Delete(file_temp);
  472. }
  473. [TestMethod]
  474. public void Test_Document_ApplyTemplate()
  475. {
  476. using (MemoryStream documentStream = new MemoryStream())
  477. {
  478. using (DocX document = DocX.Create(documentStream))
  479. {
  480. document.ApplyTemplate(directory_documents + "Template.dotx");
  481. document.Save();
  482. Header firstHeader = document.Headers.first;
  483. Header oddHeader = document.Headers.odd;
  484. Header evenHeader = document.Headers.even;
  485. Footer firstFooter = document.Footers.first;
  486. Footer oddFooter = document.Footers.odd;
  487. Footer evenFooter = document.Footers.even;
  488. Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
  489. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  490. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  491. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  492. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  493. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  494. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  495. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  496. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  497. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  498. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  499. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  500. Paragraph firstParagraph = document.Paragraphs[0];
  501. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  502. }
  503. }
  504. }
  505. }
  506. }