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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. namespace UnitTests
  12. {
  13. /// <summary>
  14. /// Summary description for UnitTest1
  15. /// </summary>
  16. [TestClass]
  17. public class UnitTest1
  18. {
  19. // Get the fullpath to the executing assembly.
  20. string directory_executing_assembly;
  21. string directory_documents;
  22. string file_temp = "temp.docx";
  23. const string package_part_document = "/word/document.xml";
  24. public UnitTest1()
  25. {
  26. directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
  27. // The documents directory
  28. List<string> steps = directory_executing_assembly.Split('\\').ToList();
  29. steps.RemoveRange(steps.Count() - 3, 3);
  30. directory_documents = String.Join("\\", steps) + "\\documents\\";
  31. }
  32. [TestMethod]
  33. public void Test_Tables()
  34. {
  35. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  36. {
  37. // There is only one Paragraph at the document level.
  38. Assert.IsTrue(document.Paragraphs.Count() == 1);
  39. // There is only one Table in this document.
  40. Assert.IsTrue(document.Tables.Count() == 1);
  41. // Extract the only Table.
  42. Table t0 = document.Tables[0];
  43. // This table has 12 Paragraphs.
  44. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  45. }
  46. }
  47. [TestMethod]
  48. public void Test_Images()
  49. {
  50. using (DocX document = DocX.Load(directory_documents + "Images.docx"))
  51. {
  52. // Extract Images from Document.
  53. List<Novacode.Image> document_images = document.Images;
  54. // Make sure there are 3 Images in this document.
  55. Assert.IsTrue(document_images.Count() == 3);
  56. // Extract the headers from this document.
  57. Headers headers = document.Headers;
  58. Header header_first = headers.first;
  59. Header header_odd = headers.odd;
  60. Header header_even = headers.even;
  61. #region Header_First
  62. // Extract Images from the first Header.
  63. List<Novacode.Image> header_first_images = header_first.Images;
  64. // Make sure there is 1 Image in the first header.
  65. Assert.IsTrue(header_first_images.Count() == 1);
  66. #endregion
  67. #region Header_Odd
  68. // Extract Images from the odd Header.
  69. List<Novacode.Image> header_odd_images = header_odd.Images;
  70. // Make sure there is 1 Image in the first header.
  71. Assert.IsTrue(header_odd_images.Count() == 1);
  72. #endregion
  73. #region Header_Even
  74. // Extract Images from the odd Header.
  75. List<Novacode.Image> header_even_images = header_even.Images;
  76. // Make sure there is 1 Image in the first header.
  77. Assert.IsTrue(header_even_images.Count() == 1);
  78. #endregion
  79. }
  80. }
  81. // Write the string "Hello World" into this Image.
  82. private static void CoolExample(Novacode.Image i, Stream s, string str)
  83. {
  84. // Write "Hello World" into this Image.
  85. Bitmap b = new Bitmap(s);
  86. /*
  87. * Get the Graphics object for this Bitmap.
  88. * The Graphics object provides functions for drawing.
  89. */
  90. Graphics g = Graphics.FromImage(b);
  91. // Draw the string "Hello World".
  92. g.DrawString
  93. (
  94. str,
  95. new Font("Tahoma", 20),
  96. Brushes.Blue,
  97. new PointF(0, 0)
  98. );
  99. // Save this Bitmap back into the document using a Create\Write stream.
  100. b.Save(i.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
  101. }
  102. [TestMethod]
  103. public void Test_Document_Paragraphs()
  104. {
  105. // Load the document 'Paragraphs.docx'
  106. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  107. {
  108. // Extract the Paragraphs from this document.
  109. List<Paragraph> paragraphs = document.Paragraphs;
  110. // There should be 3 Paragraphs in this document.
  111. Assert.IsTrue(paragraphs.Count() == 3);
  112. // Extract the 3 Paragraphs.
  113. Paragraph p1 = paragraphs[0];
  114. Paragraph p2 = paragraphs[1];
  115. Paragraph p3 = paragraphs[2];
  116. // Extract their Text properties.
  117. string p1_text = p1.Text;
  118. string p2_text = p2.Text;
  119. string p3_text = p3.Text;
  120. // Test their Text properties against absolutes.
  121. Assert.IsTrue(p1_text == "Paragraph 1");
  122. Assert.IsTrue(p2_text == "Paragraph 2");
  123. Assert.IsTrue(p3_text == "Paragraph 3");
  124. // Create a string to append to each Paragraph.
  125. string appended_text = "foo bar foo";
  126. // Test the appending of text to each Paragraph.
  127. Assert.IsTrue(p1.Append(appended_text).Text == p1_text + appended_text);
  128. Assert.IsTrue(p2.Append(appended_text).Text == p2_text + appended_text);
  129. Assert.IsTrue(p3.Append(appended_text).Text == p3_text + appended_text);
  130. // Test FindAll
  131. List<int> p1_foos = p1.FindAll("foo");
  132. Assert.IsTrue(p1_foos.Count() == 2 && p1_foos[0] == 11 && p1_foos[1] == 19);
  133. // Test ReplaceText
  134. p2.ReplaceText("foo", "bar", false);
  135. Assert.IsTrue(p2.Text == "Paragraph 2bar bar bar");
  136. // Test RemoveText
  137. p3.RemoveText(1, 3, false);
  138. Assert.IsTrue(p3.Text == "Pgraph 3foo bar foo");
  139. // Its important that each Paragraph knows the PackagePart it belongs to.
  140. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  141. // Test the saving of the document.
  142. document.SaveAs(file_temp);
  143. }
  144. // Delete the tempory file.
  145. File.Delete(file_temp);
  146. }
  147. [TestMethod]
  148. public void Test_Document_ApplyTemplate()
  149. {
  150. using (MemoryStream documentStream = new MemoryStream())
  151. {
  152. using (DocX document = DocX.Create(documentStream))
  153. {
  154. document.ApplyTemplate(directory_documents + "Template.dotx");
  155. document.Save();
  156. Header firstHeader = document.Headers.first;
  157. Header oddHeader = document.Headers.odd;
  158. Header evenHeader = document.Headers.even;
  159. Footer firstFooter = document.Footers.first;
  160. Footer oddFooter = document.Footers.odd;
  161. Footer evenFooter = document.Footers.even;
  162. Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
  163. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  164. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  165. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  166. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  167. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  168. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  169. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  170. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  171. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  172. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  173. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  174. Paragraph firstParagraph = document.Paragraphs[0];
  175. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  176. }
  177. }
  178. }
  179. }
  180. }