Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. // Try and remove a Hyperlink at an index greater than the last.
  152. // This should throw an exception.
  153. try
  154. {
  155. p1.RemoveHyperlink(3);
  156. Assert.Fail();
  157. }
  158. catch (ArgumentException e) {}
  159. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  160. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  161. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  162. }
  163. }
  164. [TestMethod]
  165. public void Test_Paragraph_ReplaceText()
  166. {
  167. // Create a new document
  168. using (DocX document = DocX.Create("Test.docx"))
  169. {
  170. // Simple
  171. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  172. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  173. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  174. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  175. // Difficult
  176. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  177. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  178. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  179. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  180. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  181. }
  182. }
  183. [TestMethod]
  184. public void Test_Paragraph_RemoveText()
  185. {
  186. // Create a new document
  187. using (DocX document = DocX.Create("Test.docx"))
  188. {
  189. // Simple
  190. //<p>
  191. // <r><t>HellWorld</t></r>
  192. //</p>
  193. Paragraph p1 = document.InsertParagraph("HelloWorld");
  194. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  195. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  196. p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
  197. // Difficult
  198. //<p>
  199. // <r><t>A</t></r>
  200. // <r><t>B</t></r>
  201. // <r><t>C</t></r>
  202. //</p>
  203. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  204. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  205. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  206. p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
  207. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  208. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  209. // Contrived 1
  210. //<p>
  211. // <r>
  212. // <t>A</t>
  213. // <t>B</t>
  214. // <t>C</t>
  215. // </r>
  216. //</p>
  217. Paragraph p3 = document.InsertParagraph("");
  218. p3.Xml = XElement.Parse
  219. (
  220. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  221. <w:pPr />
  222. <w:r>
  223. <w:rPr />
  224. <w:t>A</w:t>
  225. <w:t>B</w:t>
  226. <w:t>C</w:t>
  227. </w:r>
  228. </w:p>"
  229. );
  230. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  231. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  232. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  233. // Contrived 2
  234. //<p>
  235. // <r>
  236. // <t>A</t>
  237. // <t>B</t>
  238. // <t>C</t>
  239. // </r>
  240. //</p>
  241. Paragraph p4 = document.InsertParagraph("");
  242. p4.Xml = XElement.Parse
  243. (
  244. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  245. <w:pPr />
  246. <w:r>
  247. <w:rPr />
  248. <tab />
  249. <w:t>A</w:t>
  250. <tab />
  251. </w:r>
  252. <w:r>
  253. <w:rPr />
  254. <tab />
  255. <w:t>B</w:t>
  256. <tab />
  257. </w:r>
  258. </w:p>"
  259. );
  260. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  261. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  262. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  263. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  264. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  265. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  266. }
  267. }
  268. [TestMethod]
  269. public void Test_Paragraph_InsertText()
  270. {
  271. // Create a new document
  272. using (DocX document = DocX.Create("Test.docx"))
  273. {
  274. // Simple
  275. //<p>
  276. // <r><t>HelloWorld</t></r>
  277. //</p>
  278. Paragraph p1 = document.InsertParagraph("HelloWorld");
  279. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  280. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  281. p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  282. // Difficult
  283. //<p>
  284. // <r><t>A</t></r>
  285. // <r><t>B</t></r>
  286. // <r><t>C</t></r>
  287. //</p>
  288. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  289. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  290. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  291. p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  292. p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  293. // Contrived 1
  294. //<p>
  295. // <r>
  296. // <t>A</t>
  297. // <t>B</t>
  298. // <t>C</t>
  299. // </r>
  300. //</p>
  301. Paragraph p3 = document.InsertParagraph("");
  302. p3.Xml = XElement.Parse
  303. (
  304. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  305. <w:pPr />
  306. <w:r>
  307. <w:rPr />
  308. <w:t>A</w:t>
  309. <w:t>B</w:t>
  310. <w:t>C</w:t>
  311. </w:r>
  312. </w:p>"
  313. );
  314. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  315. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  316. p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  317. p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  318. // Contrived 2
  319. //<p>
  320. // <r>
  321. // <t>A</t>
  322. // <t>B</t>
  323. // <t>C</t>
  324. // </r>
  325. //</p>
  326. Paragraph p4 = document.InsertParagraph("");
  327. p4.Xml = XElement.Parse
  328. (
  329. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  330. <w:pPr />
  331. <w:r>
  332. <w:rPr />
  333. <w:t>A</w:t>
  334. <w:t>B</w:t>
  335. <w:t>C</w:t>
  336. </w:r>
  337. </w:p>"
  338. );
  339. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  340. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  341. p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  342. p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  343. }
  344. }
  345. [TestMethod]
  346. public void Test_Document_Paragraphs()
  347. {
  348. // This document contains a run with two text next to each other.
  349. // <run>
  350. // <text>Hello World</text>
  351. // <text>foo</text>
  352. // </run>
  353. using (DocX document = DocX.Load(@"C:\Users\Cathal\Desktop\Bug.docx"))
  354. {
  355. Paragraph p = document.Paragraphs[0];
  356. Assert.IsTrue(p.Text == "Hello worldfoo");
  357. p.RemoveText("Hello world".Length, 3, false);
  358. Assert.IsTrue(p.Text == "Hello world");
  359. }
  360. // Load the document 'Paragraphs.docx'
  361. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  362. {
  363. // Extract the Paragraphs from this document.
  364. List<Paragraph> paragraphs = document.Paragraphs;
  365. // There should be 3 Paragraphs in this document.
  366. Assert.IsTrue(paragraphs.Count() == 3);
  367. // Extract the 3 Paragraphs.
  368. Paragraph p1 = paragraphs[0];
  369. Paragraph p2 = paragraphs[1];
  370. Paragraph p3 = paragraphs[2];
  371. // Extract their Text properties.
  372. string p1_text = p1.Text;
  373. string p2_text = p2.Text;
  374. string p3_text = p3.Text;
  375. // Test their Text properties against absolutes.
  376. Assert.IsTrue(p1_text == "Paragraph 1");
  377. Assert.IsTrue(p2_text == "Paragraph 2");
  378. Assert.IsTrue(p3_text == "Paragraph 3");
  379. // Create a string to append to each Paragraph.
  380. string appended_text = "foo bar foo";
  381. // Test the appending of text to each Paragraph.
  382. Assert.IsTrue(p1.Append(appended_text).Text == p1_text + appended_text);
  383. Assert.IsTrue(p2.Append(appended_text).Text == p2_text + appended_text);
  384. Assert.IsTrue(p3.Append(appended_text).Text == p3_text + appended_text);
  385. // Test FindAll
  386. List<int> p1_foos = p1.FindAll("foo");
  387. Assert.IsTrue(p1_foos.Count() == 2 && p1_foos[0] == 11 && p1_foos[1] == 19);
  388. // Test ReplaceText
  389. p2.ReplaceText("foo", "bar", false);
  390. Assert.IsTrue(p2.Text == "Paragraph 2bar bar bar");
  391. // Test RemoveText
  392. p3.RemoveText(1, 3, false);
  393. Assert.IsTrue(p3.Text == "Pgraph 3foo bar foo");
  394. // Its important that each Paragraph knows the PackagePart it belongs to.
  395. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  396. // Test the saving of the document.
  397. document.SaveAs(file_temp);
  398. }
  399. // Delete the tempory file.
  400. File.Delete(file_temp);
  401. }
  402. [TestMethod]
  403. public void Test_Document_ApplyTemplate()
  404. {
  405. using (MemoryStream documentStream = new MemoryStream())
  406. {
  407. using (DocX document = DocX.Create(documentStream))
  408. {
  409. document.ApplyTemplate(directory_documents + "Template.dotx");
  410. document.Save();
  411. Header firstHeader = document.Headers.first;
  412. Header oddHeader = document.Headers.odd;
  413. Header evenHeader = document.Headers.even;
  414. Footer firstFooter = document.Footers.first;
  415. Footer oddFooter = document.Footers.odd;
  416. Footer evenFooter = document.Footers.even;
  417. Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
  418. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  419. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  420. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  421. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  422. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  423. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  424. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  425. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  426. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  427. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  428. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  429. Paragraph firstParagraph = document.Paragraphs[0];
  430. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  431. }
  432. }
  433. }
  434. }
  435. }