Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

UnitTest1.cs 22KB

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