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

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