選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UnitTest1.cs 49KB

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