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

UnitTest1.cs 55KB

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