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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. using System.IO.Packaging;
  13. using System.Text.RegularExpressions;
  14. namespace UnitTests
  15. {
  16. /// <summary>
  17. /// Summary description for UnitTest1
  18. /// </summary>
  19. [TestClass]
  20. public class UnitTest1
  21. {
  22. // Get the fullpath to the executing assembly.
  23. string directory_executing_assembly;
  24. string directory_documents;
  25. string file_temp = "temp.docx";
  26. const string package_part_document = "/word/document.xml";
  27. public UnitTest1()
  28. {
  29. directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
  30. // The documents directory
  31. List<string> steps = directory_executing_assembly.Split('\\').ToList();
  32. steps.RemoveRange(steps.Count() - 3, 3);
  33. directory_documents = String.Join("\\", steps) + "\\documents\\";
  34. }
  35. [TestMethod]
  36. public void PatternReplacement()
  37. {
  38. Dictionary<string, string> testPatterns = new Dictionary<string, string>()
  39. {
  40. {"COURT NAME","Fred Frump"},
  41. {"Case Number","cr-md-2011-1234567"}
  42. };
  43. using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
  44. {
  45. foreach (var p in testPatterns)
  46. {
  47. replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
  48. }
  49. replaceDoc.SaveAs(directory_documents + "ReplaceResults.docx");
  50. }
  51. }
  52. [TestMethod]
  53. public void Test_EverybodyHasAHome_Loaded()
  54. {
  55. // Load a document.
  56. using (DocX document = DocX.Load(directory_documents + "EverybodyHasAHome.docx"))
  57. {
  58. // Main document tests.
  59. string document_xml_file = document.mainPart.Uri.OriginalString;
  60. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  61. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  62. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  63. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  64. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  65. // header first
  66. Header header_first = document.Headers.first;
  67. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  68. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  69. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  70. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  71. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  72. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  73. // header odd
  74. Header header_odd = document.Headers.odd;
  75. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  76. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  77. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  78. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  79. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  80. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  81. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  82. // header even
  83. Header header_even = document.Headers.even;
  84. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  85. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  86. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  87. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  88. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  89. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  90. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  91. // footer first
  92. Footer footer_first = document.Footers.first;
  93. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  94. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  95. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  96. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  97. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  98. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  99. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  100. // footer odd
  101. Footer footer_odd = document.Footers.odd;
  102. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  103. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  104. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  105. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  106. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  107. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  108. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  109. // footer even
  110. Footer footer_even = document.Footers.even;
  111. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  112. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  113. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  114. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  115. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  116. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  117. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  118. }
  119. }
  120. [TestMethod]
  121. public void Test_Insert_Picture_ParagraphBeforeSelf()
  122. {
  123. // Create test document.
  124. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  125. {
  126. // Add an Image to this document.
  127. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  128. // Create a Picture from this Image.
  129. Picture pic = img.CreatePicture();
  130. Assert.IsNotNull(pic);
  131. // Main document.
  132. Paragraph p0 = document.InsertParagraph("Hello");
  133. Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
  134. p1.InsertPicture(pic, 3);
  135. // Save this document.
  136. document.Save();
  137. }
  138. }
  139. [TestMethod]
  140. public void Test_Insert_Picture_ParagraphAfterSelf()
  141. {
  142. // Create test document.
  143. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  144. {
  145. // Add an Image to this document.
  146. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  147. // Create a Picture from this Image.
  148. Picture pic = img.CreatePicture();
  149. Assert.IsNotNull(pic);
  150. // Main document.
  151. Paragraph p0 = document.InsertParagraph("Hello");
  152. Paragraph p1 = p0.InsertParagraphAfterSelf("again");
  153. p1.InsertPicture(pic, 3);
  154. // Save this document.
  155. document.Save();
  156. }
  157. }
  158. [TestMethod]
  159. public void Test_EverybodyHasAHome_Created()
  160. {
  161. // Create a new document.
  162. using (DocX document = DocX.Create("Test.docx"))
  163. {
  164. // Create a Table.
  165. Table t = document.AddTable(3, 3);
  166. t.Design = TableDesign.TableGrid;
  167. // Insert a Paragraph and a Table into the main document.
  168. document.InsertParagraph();
  169. document.InsertTable(t);
  170. // Insert a Paragraph and a Table into every Header.
  171. document.AddHeaders();
  172. document.Headers.odd.InsertParagraph();
  173. document.Headers.odd.InsertTable(t);
  174. document.Headers.even.InsertParagraph();
  175. document.Headers.even.InsertTable(t);
  176. document.Headers.first.InsertParagraph();
  177. document.Headers.first.InsertTable(t);
  178. // Insert a Paragraph and a Table into every Footer.
  179. document.AddFooters();
  180. document.Footers.odd.InsertParagraph();
  181. document.Footers.odd.InsertTable(t);
  182. document.Footers.even.InsertParagraph();
  183. document.Footers.even.InsertTable(t);
  184. document.Footers.first.InsertParagraph();
  185. document.Footers.first.InsertTable(t);
  186. // Main document tests.
  187. string document_xml_file = document.mainPart.Uri.OriginalString;
  188. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  189. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  190. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  191. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  192. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  193. // header first
  194. Header header_first = document.Headers.first;
  195. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  196. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  197. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  198. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  199. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  200. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  201. // header odd
  202. Header header_odd = document.Headers.odd;
  203. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  204. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  205. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  206. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  207. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  208. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  209. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  210. // header even
  211. Header header_even = document.Headers.even;
  212. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  213. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  214. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  215. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  216. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  217. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  218. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  219. // footer first
  220. Footer footer_first = document.Footers.first;
  221. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  222. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  223. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  224. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  225. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  226. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  227. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  228. // footer odd
  229. Footer footer_odd = document.Footers.odd;
  230. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  231. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  232. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  233. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  234. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  235. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  236. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  237. // footer even
  238. Footer footer_even = document.Footers.even;
  239. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  240. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  241. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  242. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  243. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  244. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  245. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  246. }
  247. }
  248. [TestMethod]
  249. public void Test_Document_AddImage_FromDisk()
  250. {
  251. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  252. {
  253. // Add a png to into this document
  254. Novacode.Image png = document.AddImage(directory_documents + "purple.png");
  255. Assert.IsTrue(document.Images.Count == 1);
  256. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
  257. // Add a tiff into to this document
  258. Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
  259. Assert.IsTrue(document.Images.Count == 2);
  260. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
  261. // Add a gif into to this document
  262. Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
  263. Assert.IsTrue(document.Images.Count == 3);
  264. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
  265. // Add a jpg into to this document
  266. Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
  267. Assert.IsTrue(document.Images.Count == 4);
  268. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
  269. // Add a bitmap to this document
  270. Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
  271. Assert.IsTrue(document.Images.Count == 5);
  272. // Word does not allow bmp make sure it was inserted as a png.
  273. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
  274. }
  275. }
  276. [TestMethod]
  277. public void Test_Document_AddImage_FromStream()
  278. {
  279. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  280. {
  281. // DocX will always insert Images that come from Streams as jpeg.
  282. // Add a png to into this document
  283. Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
  284. Assert.IsTrue(document.Images.Count == 1);
  285. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
  286. // Add a tiff into to this document
  287. Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
  288. Assert.IsTrue(document.Images.Count == 2);
  289. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
  290. // Add a gif into to this document
  291. Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
  292. Assert.IsTrue(document.Images.Count == 3);
  293. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
  294. // Add a jpg into to this document
  295. Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
  296. Assert.IsTrue(document.Images.Count == 4);
  297. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
  298. // Add a bitmap to this document
  299. Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
  300. Assert.IsTrue(document.Images.Count == 5);
  301. // Word does not allow bmp make sure it was inserted as a png.
  302. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
  303. }
  304. }
  305. [TestMethod]
  306. public void Test_Tables()
  307. {
  308. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  309. {
  310. // There is only one Paragraph at the document level.
  311. Assert.IsTrue(document.Paragraphs.Count() == 13);
  312. // There is only one Table in this document.
  313. Assert.IsTrue(document.Tables.Count() == 1);
  314. // Extract the only Table.
  315. Table t0 = document.Tables[0];
  316. // This table has 12 Paragraphs.
  317. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  318. }
  319. }
  320. [TestMethod]
  321. public void Test_Images()
  322. {
  323. using (DocX document = DocX.Load(directory_documents + "Images.docx"))
  324. {
  325. // Extract Images from Document.
  326. List<Novacode.Image> document_images = document.Images;
  327. // Make sure there are 3 Images in this document.
  328. Assert.IsTrue(document_images.Count() == 3);
  329. // Extract the headers from this document.
  330. Headers headers = document.Headers;
  331. Header header_first = headers.first;
  332. Header header_odd = headers.odd;
  333. Header header_even = headers.even;
  334. #region Header_First
  335. // Extract Images from the first Header.
  336. List<Novacode.Image> header_first_images = header_first.Images;
  337. // Make sure there is 1 Image in the first header.
  338. Assert.IsTrue(header_first_images.Count() == 1);
  339. #endregion
  340. #region Header_Odd
  341. // Extract Images from the odd Header.
  342. List<Novacode.Image> header_odd_images = header_odd.Images;
  343. // Make sure there is 1 Image in the first header.
  344. Assert.IsTrue(header_odd_images.Count() == 1);
  345. #endregion
  346. #region Header_Even
  347. // Extract Images from the odd Header.
  348. List<Novacode.Image> header_even_images = header_even.Images;
  349. // Make sure there is 1 Image in the first header.
  350. Assert.IsTrue(header_even_images.Count() == 1);
  351. #endregion
  352. }
  353. }
  354. [TestMethod]
  355. public void Test_Insert_Picture()
  356. {
  357. // Load test document.
  358. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  359. {
  360. // Add Headers and Footers into this document.
  361. document.AddHeaders();
  362. document.AddFooters();
  363. document.DifferentFirstPage = true;
  364. document.DifferentOddAndEvenPages = true;
  365. // Add an Image to this document.
  366. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  367. // Create a Picture from this Image.
  368. Picture pic = img.CreatePicture();
  369. // Main document.
  370. Paragraph p0 = document.InsertParagraph("Hello");
  371. p0.InsertPicture(pic, 3);
  372. // Header first.
  373. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  374. p1.InsertPicture(pic, 2);
  375. // Header odd.
  376. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  377. p2.InsertPicture(pic, 2);
  378. // Header even.
  379. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  380. p3.InsertPicture(pic, 2);
  381. // Footer first.
  382. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  383. p4.InsertPicture(pic, 2);
  384. // Footer odd.
  385. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  386. p5.InsertPicture(pic, 2);
  387. // Footer even.
  388. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  389. p6.InsertPicture(pic, 2);
  390. // Save this document.
  391. document.Save();
  392. }
  393. }
  394. [TestMethod]
  395. public void Test_Insert_Hyperlink()
  396. {
  397. // Load test document.
  398. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  399. {
  400. // Add Headers and Footers into this document.
  401. document.AddHeaders();
  402. document.AddFooters();
  403. document.DifferentFirstPage = true;
  404. document.DifferentOddAndEvenPages = true;
  405. // Add a Hyperlink into this document.
  406. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  407. // Main document.
  408. Paragraph p0 = document.InsertParagraph("Hello");
  409. p0.InsertHyperlink(h, 3);
  410. // Header first.
  411. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  412. p1.InsertHyperlink(h, 3);
  413. // Header odd.
  414. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  415. p2.InsertHyperlink(h, 3);
  416. // Header even.
  417. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  418. p3.InsertHyperlink(h, 3);
  419. // Footer first.
  420. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  421. p4.InsertHyperlink(h, 3);
  422. // Footer odd.
  423. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  424. p5.InsertHyperlink(h, 3);
  425. // Footer even.
  426. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  427. p6.InsertHyperlink(h, 3);
  428. // Save this document.
  429. document.Save();
  430. }
  431. }
  432. [TestMethod]
  433. public void Test_Append_Hyperlink()
  434. {
  435. // Load test document.
  436. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  437. {
  438. // Add Headers and Footers into this document.
  439. document.AddHeaders();
  440. document.AddFooters();
  441. document.DifferentFirstPage = true;
  442. document.DifferentOddAndEvenPages = true;
  443. // Add a Hyperlink to this document.
  444. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  445. // Main document.
  446. Paragraph p0 = document.InsertParagraph("----");
  447. p0.AppendHyperlink(h);
  448. Assert.IsTrue(p0.Text == "----google");
  449. // Header first.
  450. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  451. p1.AppendHyperlink(h);
  452. Assert.IsTrue(p1.Text == "----google");
  453. // Header odd.
  454. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  455. p2.AppendHyperlink(h);
  456. Assert.IsTrue(p2.Text == "----google");
  457. // Header even.
  458. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  459. p3.AppendHyperlink(h);
  460. Assert.IsTrue(p3.Text == "----google");
  461. // Footer first.
  462. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  463. p4.AppendHyperlink(h);
  464. Assert.IsTrue(p4.Text == "----google");
  465. // Footer odd.
  466. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  467. p5.AppendHyperlink(h);
  468. Assert.IsTrue(p5.Text == "----google");
  469. // Footer even.
  470. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  471. p6.AppendHyperlink(h);
  472. Assert.IsTrue(p6.Text == "----google");
  473. // Save the document.
  474. document.Save();
  475. }
  476. }
  477. [TestMethod]
  478. public void Test_Append_Picture()
  479. {
  480. // Create test document.
  481. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  482. {
  483. // Add Headers and Footers into this document.
  484. document.AddHeaders();
  485. document.AddFooters();
  486. document.DifferentFirstPage = true;
  487. document.DifferentOddAndEvenPages = true;
  488. // Add an Image to this document.
  489. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  490. // Create a Picture from this Image.
  491. Picture pic = img.CreatePicture();
  492. // Main document.
  493. Paragraph p0 = document.InsertParagraph();
  494. p0.AppendPicture(pic);
  495. // Header first.
  496. Paragraph p1 = document.Headers.first.InsertParagraph();
  497. p1.AppendPicture(pic);
  498. // Header odd.
  499. Paragraph p2 = document.Headers.odd.InsertParagraph();
  500. p2.AppendPicture(pic);
  501. // Header even.
  502. Paragraph p3 = document.Headers.even.InsertParagraph();
  503. p3.AppendPicture(pic);
  504. // Footer first.
  505. Paragraph p4 = document.Footers.first.InsertParagraph();
  506. p4.AppendPicture(pic);
  507. // Footer odd.
  508. Paragraph p5 = document.Footers.odd.InsertParagraph();
  509. p5.AppendPicture(pic);
  510. // Footer even.
  511. Paragraph p6 = document.Footers.even.InsertParagraph();
  512. p6.AppendPicture(pic);
  513. // Save the document.
  514. document.Save();
  515. }
  516. }
  517. [TestMethod]
  518. public void Test_Move_Picture_Load()
  519. {
  520. // Load test document.
  521. using (DocX document = DocX.Load(directory_documents + "MovePicture.docx"))
  522. {
  523. // Extract the first Picture from the first Paragraph.
  524. Picture picture = document.Paragraphs.First().Pictures.First();
  525. // Move it into the first Header.
  526. Header header_first = document.Headers.first;
  527. header_first.Paragraphs.First().AppendPicture(picture);
  528. // Move it into the even Header.
  529. Header header_even = document.Headers.even;
  530. header_even.Paragraphs.First().AppendPicture(picture);
  531. // Move it into the odd Header.
  532. Header header_odd = document.Headers.odd;
  533. header_odd.Paragraphs.First().AppendPicture(picture);
  534. // Move it into the first Footer.
  535. Footer footer_first = document.Footers.first;
  536. footer_first.Paragraphs.First().AppendPicture(picture);
  537. // Move it into the even Footer.
  538. Footer footer_even = document.Footers.even;
  539. footer_even.Paragraphs.First().AppendPicture(picture);
  540. // Move it into the odd Footer.
  541. Footer footer_odd = document.Footers.odd;
  542. footer_odd.Paragraphs.First().AppendPicture(picture);
  543. // Save this as MovedPicture.docx
  544. document.SaveAs(directory_documents + "MovedPicture.docx");
  545. }
  546. }
  547. [TestMethod]
  548. public void Test_Paragraph_InsertHyperlink()
  549. {
  550. // Create a new document
  551. using (DocX document = DocX.Create("Test.docx"))
  552. {
  553. // Add a Hyperlink to this document.
  554. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  555. // Simple
  556. Paragraph p1 = document.InsertParagraph("AC");
  557. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  558. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  559. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  560. // Difficult
  561. Paragraph p2 = document.InsertParagraph("\tA\tC\t");
  562. p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
  563. p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
  564. p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
  565. // Contrived
  566. // Add a contrived Hyperlink to this document.
  567. Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
  568. Paragraph p3 = document.InsertParagraph("\tA\tC\t");
  569. p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
  570. p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
  571. p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
  572. }
  573. }
  574. [TestMethod]
  575. public void Test_Paragraph_RemoveHyperlink()
  576. {
  577. // Create a new document
  578. using (DocX document = DocX.Create("Test.docx"))
  579. {
  580. // Add a Hyperlink to this document.
  581. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  582. // Simple
  583. Paragraph p1 = document.InsertParagraph("AC");
  584. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  585. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  586. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  587. // Try and remove a Hyperlink using a negative index.
  588. // This should throw an exception.
  589. try
  590. {
  591. p1.RemoveHyperlink(-1);
  592. Assert.Fail();
  593. }
  594. catch (ArgumentException) { }
  595. catch (Exception) { Assert.Fail(); }
  596. // Try and remove a Hyperlink at an index greater than the last.
  597. // This should throw an exception.
  598. try
  599. {
  600. p1.RemoveHyperlink(3);
  601. Assert.Fail();
  602. }
  603. catch (ArgumentException) {}
  604. catch (Exception) { Assert.Fail(); }
  605. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  606. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  607. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  608. }
  609. }
  610. [TestMethod]
  611. public void Test_Paragraph_ReplaceText()
  612. {
  613. // Create a new document
  614. using (DocX document = DocX.Create("Test.docx"))
  615. {
  616. // Simple
  617. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  618. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  619. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  620. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  621. // Try and replace text that dosen't exist in the Paragraph.
  622. string old = p1.Text;
  623. p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
  624. // Difficult
  625. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  626. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  627. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  628. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  629. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  630. }
  631. }
  632. [TestMethod]
  633. public void Test_Paragraph_RemoveText()
  634. {
  635. // Create a new document
  636. using (DocX document = DocX.Create("Test.docx"))
  637. {
  638. // Simple
  639. //<p>
  640. // <r><t>HellWorld</t></r>
  641. //</p>
  642. Paragraph p1 = document.InsertParagraph("HelloWorld");
  643. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  644. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  645. p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
  646. // Try and remove text at an index greater than the last.
  647. // This should throw an exception.
  648. try
  649. {
  650. p1.RemoveText(p1.Text.Length, 1);
  651. Assert.Fail();
  652. }
  653. catch (ArgumentOutOfRangeException) { }
  654. catch (Exception) { Assert.Fail(); }
  655. // Try and remove text at a negative index.
  656. // This should throw an exception.
  657. try
  658. {
  659. p1.RemoveText(-1, 1);
  660. Assert.Fail();
  661. }
  662. catch (ArgumentOutOfRangeException) { }
  663. catch (Exception) { Assert.Fail(); }
  664. // Difficult
  665. //<p>
  666. // <r><t>A</t></r>
  667. // <r><t>B</t></r>
  668. // <r><t>C</t></r>
  669. //</p>
  670. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  671. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  672. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  673. p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
  674. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  675. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  676. // Contrived 1
  677. //<p>
  678. // <r>
  679. // <t>A</t>
  680. // <t>B</t>
  681. // <t>C</t>
  682. // </r>
  683. //</p>
  684. Paragraph p3 = document.InsertParagraph("");
  685. p3.Xml = XElement.Parse
  686. (
  687. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  688. <w:pPr />
  689. <w:r>
  690. <w:rPr />
  691. <w:t>A</w:t>
  692. <w:t>B</w:t>
  693. <w:t>C</w:t>
  694. </w:r>
  695. </w:p>"
  696. );
  697. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  698. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  699. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  700. // Contrived 2
  701. //<p>
  702. // <r>
  703. // <t>A</t>
  704. // <t>B</t>
  705. // <t>C</t>
  706. // </r>
  707. //</p>
  708. Paragraph p4 = document.InsertParagraph("");
  709. p4.Xml = XElement.Parse
  710. (
  711. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  712. <w:pPr />
  713. <w:r>
  714. <w:rPr />
  715. <tab />
  716. <w:t>A</w:t>
  717. <tab />
  718. </w:r>
  719. <w:r>
  720. <w:rPr />
  721. <tab />
  722. <w:t>B</w:t>
  723. <tab />
  724. </w:r>
  725. </w:p>"
  726. );
  727. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  728. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  729. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  730. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  731. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  732. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  733. }
  734. }
  735. [TestMethod]
  736. public void Test_Paragraph_InsertText()
  737. {
  738. // Create a new document
  739. using (DocX document = DocX.Create("Test.docx"))
  740. {
  741. // Simple
  742. //<p>
  743. // <r><t>HelloWorld</t></r>
  744. //</p>
  745. Paragraph p1 = document.InsertParagraph("HelloWorld");
  746. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  747. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  748. p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  749. // Try and insert text at an index greater than the last + 1.
  750. // This should throw an exception.
  751. try
  752. {
  753. p1.InsertText(p1.Text.Length + 1, "-");
  754. Assert.Fail();
  755. }
  756. catch (ArgumentOutOfRangeException) { }
  757. catch (Exception) { Assert.Fail(); }
  758. // Try and insert text at a negative index.
  759. // This should throw an exception.
  760. try
  761. {
  762. p1.InsertText(-1, "-");
  763. Assert.Fail();
  764. }
  765. catch (ArgumentOutOfRangeException) { }
  766. catch (Exception) { Assert.Fail(); }
  767. // Difficult
  768. //<p>
  769. // <r><t>A</t></r>
  770. // <r><t>B</t></r>
  771. // <r><t>C</t></r>
  772. //</p>
  773. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  774. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  775. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  776. p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  777. p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  778. // Contrived 1
  779. //<p>
  780. // <r>
  781. // <t>A</t>
  782. // <t>B</t>
  783. // <t>C</t>
  784. // </r>
  785. //</p>
  786. Paragraph p3 = document.InsertParagraph("");
  787. p3.Xml = XElement.Parse
  788. (
  789. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  790. <w:pPr />
  791. <w:r>
  792. <w:rPr />
  793. <w:t>A</w:t>
  794. <w:t>B</w:t>
  795. <w:t>C</w:t>
  796. </w:r>
  797. </w:p>"
  798. );
  799. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  800. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  801. p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  802. p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  803. // Contrived 2
  804. //<p>
  805. // <r>
  806. // <t>A</t>
  807. // <t>B</t>
  808. // <t>C</t>
  809. // </r>
  810. //</p>
  811. Paragraph p4 = document.InsertParagraph("");
  812. p4.Xml = XElement.Parse
  813. (
  814. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  815. <w:pPr />
  816. <w:r>
  817. <w:rPr />
  818. <w:t>A</w:t>
  819. <w:t>B</w:t>
  820. <w:t>C</w:t>
  821. </w:r>
  822. </w:p>"
  823. );
  824. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  825. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  826. p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  827. p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  828. }
  829. }
  830. [TestMethod]
  831. public void Test_Document_Paragraphs()
  832. {
  833. // Load the document 'Paragraphs.docx'
  834. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  835. {
  836. // Extract the Paragraphs from this document.
  837. List<Paragraph> paragraphs = document.Paragraphs;
  838. // There should be 3 Paragraphs in this document.
  839. Assert.IsTrue(paragraphs.Count() == 3);
  840. // Extract the 3 Paragraphs.
  841. Paragraph p1 = paragraphs[0];
  842. Paragraph p2 = paragraphs[1];
  843. Paragraph p3 = paragraphs[2];
  844. // Extract their Text properties.
  845. string p1_text = p1.Text;
  846. string p2_text = p2.Text;
  847. string p3_text = p3.Text;
  848. // Test their Text properties against absolutes.
  849. Assert.IsTrue(p1_text == "Paragraph 1");
  850. Assert.IsTrue(p2_text == "Paragraph 2");
  851. Assert.IsTrue(p3_text == "Paragraph 3");
  852. // Its important that each Paragraph knows the PackagePart it belongs to.
  853. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  854. // Test the saving of the document.
  855. document.SaveAs(file_temp);
  856. }
  857. // Delete the tempory file.
  858. File.Delete(file_temp);
  859. }
  860. [TestMethod]
  861. public void Test_Table_InsertRow()
  862. {
  863. using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
  864. {
  865. // Add a Table to a document.
  866. Table t = document.AddTable(2, 2);
  867. t.Design = TableDesign.TableGrid;
  868. // Insert the Table into the main section of the document.
  869. Table t1 = document.InsertTable(t);
  870. t1.InsertRow();
  871. t1.InsertColumn();
  872. // Save the document.
  873. document.Save();
  874. }
  875. }
  876. [TestMethod]
  877. public void Test_Document_ApplyTemplate()
  878. {
  879. using (MemoryStream documentStream = new MemoryStream())
  880. {
  881. using (DocX document = DocX.Create(documentStream))
  882. {
  883. document.ApplyTemplate(directory_documents + "Template.dotx");
  884. document.Save();
  885. Header firstHeader = document.Headers.first;
  886. Header oddHeader = document.Headers.odd;
  887. Header evenHeader = document.Headers.even;
  888. Footer firstFooter = document.Footers.first;
  889. Footer oddFooter = document.Footers.odd;
  890. Footer evenFooter = document.Footers.even;
  891. Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
  892. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  893. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  894. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  895. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  896. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  897. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  898. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  899. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  900. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  901. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  902. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  903. Paragraph firstParagraph = document.Paragraphs[0];
  904. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  905. }
  906. }
  907. }
  908. }
  909. }