Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DocXUnitTests.cs 84KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using System.Xml.Linq;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. using Novacode;
  11. namespace UnitTests
  12. {
  13. /// <summary>
  14. /// Summary description for DocXUnitTest
  15. /// </summary>
  16. [TestClass]
  17. public class DocXUnitTests
  18. {
  19. // Get the fullpath to the executing assembly.
  20. string directory_executing_assembly;
  21. string directory_documents;
  22. string file_temp = "temp.docx";
  23. const string package_part_document = "/word/document.xml";
  24. public DocXUnitTests()
  25. {
  26. directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
  27. // The documents directory
  28. List<string> steps = directory_executing_assembly.Split('\\').ToList();
  29. steps.RemoveRange(steps.Count() - 2, 2);
  30. directory_documents = String.Join("\\", steps) + "\\documents\\";
  31. Setup(directory_documents);
  32. }
  33. private static void Setup(string path)
  34. {
  35. if (!Directory.Exists(path))
  36. {
  37. Directory.CreateDirectory(path);
  38. }
  39. }
  40. [TestMethod]
  41. public void Test_Pattern_Replacement()
  42. {
  43. Dictionary<string, string> testPatterns = new Dictionary<string, string>()
  44. {
  45. {"COURT NAME","Fred Frump"},
  46. {"Case Number","cr-md-2011-1234567"}
  47. };
  48. using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
  49. {
  50. foreach (var t in replaceDoc.Tables)
  51. { // each table has 1 row and 3 columns
  52. Assert.IsTrue(t.Rows[0].Cells.Count == 3);
  53. Assert.IsTrue(t.ColumnCount == 3);
  54. Assert.IsTrue(t.Rows.Count == 1);
  55. Assert.IsTrue(t.RowCount == 1);
  56. }
  57. // Make sure the origional strings are in the document.
  58. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
  59. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
  60. // There are only two patterns, even though each pattern is used more than once
  61. Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
  62. // Make sure the new strings are not in the document.
  63. Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
  64. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
  65. // Do the replacing
  66. foreach (var p in testPatterns)
  67. replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
  68. // Make sure the origional string are no longer in the document.
  69. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
  70. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
  71. // Make sure the new strings are now in the document.
  72. Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
  73. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
  74. // Make sure the replacement worked.
  75. 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");
  76. }
  77. }
  78. [TestMethod]
  79. public void Test_CustomProperty_Add()
  80. {
  81. // Load a document.
  82. using (DocX document = DocX.Create("CustomProperty_Add.docx"))
  83. {
  84. Assert.IsTrue(document.CustomProperties.Count == 0);
  85. document.AddCustomProperty(new CustomProperty("fname", "cathal"));
  86. Assert.IsTrue(document.CustomProperties.Count == 1);
  87. Assert.IsTrue(document.CustomProperties.ContainsKey("fname"));
  88. Assert.IsTrue((String)document.CustomProperties["fname"].Value == "cathal");
  89. document.AddCustomProperty(new CustomProperty("age", 24));
  90. Assert.IsTrue(document.CustomProperties.Count == 2);
  91. Assert.IsTrue(document.CustomProperties.ContainsKey("age"));
  92. Assert.IsTrue((int)document.CustomProperties["age"].Value == 24);
  93. document.AddCustomProperty(new CustomProperty("male", true));
  94. Assert.IsTrue(document.CustomProperties.Count == 3);
  95. Assert.IsTrue(document.CustomProperties.ContainsKey("male"));
  96. Assert.IsTrue((bool)document.CustomProperties["male"].Value == true);
  97. document.AddCustomProperty(new CustomProperty("newyear2012", new DateTime(2012, 1, 1)));
  98. Assert.IsTrue(document.CustomProperties.Count == 4);
  99. Assert.IsTrue(document.CustomProperties.ContainsKey("newyear2012"));
  100. Assert.IsTrue((DateTime)document.CustomProperties["newyear2012"].Value == new DateTime(2012, 1, 1));
  101. document.AddCustomProperty(new CustomProperty("fav_num", 3.141592));
  102. Assert.IsTrue(document.CustomProperties.Count == 5);
  103. Assert.IsTrue(document.CustomProperties.ContainsKey("fav_num"));
  104. Assert.IsTrue((double)document.CustomProperties["fav_num"].Value == 3.141592);
  105. }
  106. }
  107. [TestMethod]
  108. public void Test_EverybodyHasAHome_Loaded()
  109. {
  110. // Load a document.
  111. using (DocX document = DocX.Load(directory_documents + "EverybodyHasAHome.docx"))
  112. {
  113. // Main document tests.
  114. string document_xml_file = document.mainPart.Uri.OriginalString;
  115. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  116. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  117. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  118. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  119. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  120. // header first
  121. Header header_first = document.Headers.first;
  122. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  123. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  124. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  125. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  126. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  127. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  128. // header odd
  129. Header header_odd = document.Headers.odd;
  130. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  131. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  132. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  133. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  134. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  135. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  136. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  137. // header even
  138. Header header_even = document.Headers.even;
  139. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  140. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  141. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  142. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  143. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  144. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  145. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  146. // footer first
  147. Footer footer_first = document.Footers.first;
  148. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  149. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  150. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  151. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  152. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  153. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  154. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  155. // footer odd
  156. Footer footer_odd = document.Footers.odd;
  157. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  158. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  159. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  160. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  161. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  162. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  163. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  164. // footer even
  165. Footer footer_even = document.Footers.even;
  166. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  167. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  168. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  169. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  170. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  171. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  172. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  173. }
  174. }
  175. [TestMethod]
  176. public void Test_Insert_Picture_ParagraphBeforeSelf()
  177. {
  178. // Create test document.
  179. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  180. {
  181. // Add an Image to this document.
  182. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  183. // Create a Picture from this Image.
  184. Picture pic = img.CreatePicture();
  185. Assert.IsNotNull(pic);
  186. // Main document.
  187. Paragraph p0 = document.InsertParagraph("Hello");
  188. Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
  189. p1.InsertPicture(pic, 3);
  190. // Save this document.
  191. document.Save();
  192. }
  193. }
  194. [TestMethod]
  195. public void Test_Insert_Picture_ParagraphAfterSelf()
  196. {
  197. // Create test document.
  198. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  199. {
  200. // Add an Image to this document.
  201. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  202. // Create a Picture from this Image.
  203. Picture pic = img.CreatePicture();
  204. Assert.IsNotNull(pic);
  205. // Main document.
  206. Paragraph p0 = document.InsertParagraph("Hello");
  207. Paragraph p1 = p0.InsertParagraphAfterSelf("again");
  208. p1.InsertPicture(pic, 3);
  209. // Save this document.
  210. document.Save();
  211. }
  212. }
  213. [TestMethod]
  214. public void Test_EverybodyHasAHome_Created()
  215. {
  216. // Create a new document.
  217. using (DocX document = DocX.Create("Test.docx"))
  218. {
  219. // Create a Table.
  220. Table t = document.AddTable(3, 3);
  221. t.Design = TableDesign.TableGrid;
  222. // Insert a Paragraph and a Table into the main document.
  223. document.InsertParagraph();
  224. document.InsertTable(t);
  225. // Insert a Paragraph and a Table into every Header.
  226. document.AddHeaders();
  227. document.Headers.odd.InsertParagraph();
  228. document.Headers.odd.InsertTable(t);
  229. document.Headers.even.InsertParagraph();
  230. document.Headers.even.InsertTable(t);
  231. document.Headers.first.InsertParagraph();
  232. document.Headers.first.InsertTable(t);
  233. // Insert a Paragraph and a Table into every Footer.
  234. document.AddFooters();
  235. document.Footers.odd.InsertParagraph();
  236. document.Footers.odd.InsertTable(t);
  237. document.Footers.even.InsertParagraph();
  238. document.Footers.even.InsertTable(t);
  239. document.Footers.first.InsertParagraph();
  240. document.Footers.first.InsertTable(t);
  241. // Main document tests.
  242. string document_xml_file = document.mainPart.Uri.OriginalString;
  243. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  244. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  245. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  246. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  247. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  248. // header first
  249. Header header_first = document.Headers.first;
  250. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  251. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  252. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  253. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  254. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  255. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  256. // header odd
  257. Header header_odd = document.Headers.odd;
  258. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  259. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  260. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  261. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  262. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  263. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  264. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  265. // header even
  266. Header header_even = document.Headers.even;
  267. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  268. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  269. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  270. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  271. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  272. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  273. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  274. // footer first
  275. Footer footer_first = document.Footers.first;
  276. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  277. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  278. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  279. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  280. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  281. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  282. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  283. // footer odd
  284. Footer footer_odd = document.Footers.odd;
  285. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  286. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  287. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  288. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  289. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  290. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  291. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  292. // footer even
  293. Footer footer_even = document.Footers.even;
  294. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  295. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  296. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  297. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  298. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  299. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  300. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  301. }
  302. }
  303. [TestMethod]
  304. public void Test_Document_AddImage_FromDisk()
  305. {
  306. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  307. {
  308. // Add a png to into this document
  309. Novacode.Image png = document.AddImage(directory_documents + "purple.png");
  310. Assert.IsTrue(document.Images.Count == 1);
  311. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
  312. // Add a tiff into to this document
  313. Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
  314. Assert.IsTrue(document.Images.Count == 2);
  315. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
  316. // Add a gif into to this document
  317. Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
  318. Assert.IsTrue(document.Images.Count == 3);
  319. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
  320. // Add a jpg into to this document
  321. Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
  322. Assert.IsTrue(document.Images.Count == 4);
  323. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
  324. // Add a bitmap to this document
  325. Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
  326. Assert.IsTrue(document.Images.Count == 5);
  327. // Word does not allow bmp make sure it was inserted as a png.
  328. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
  329. }
  330. }
  331. [TestMethod]
  332. public void Test_Document_AddImage_FromStream()
  333. {
  334. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  335. {
  336. // DocX will always insert Images that come from Streams as jpeg.
  337. // Add a png to into this document
  338. Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
  339. Assert.IsTrue(document.Images.Count == 1);
  340. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
  341. // Add a tiff into to this document
  342. Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
  343. Assert.IsTrue(document.Images.Count == 2);
  344. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
  345. // Add a gif into to this document
  346. Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
  347. Assert.IsTrue(document.Images.Count == 3);
  348. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
  349. // Add a jpg into to this document
  350. Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
  351. Assert.IsTrue(document.Images.Count == 4);
  352. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
  353. // Add a bitmap to this document
  354. Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
  355. Assert.IsTrue(document.Images.Count == 5);
  356. // Word does not allow bmp make sure it was inserted as a png.
  357. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
  358. }
  359. }
  360. [TestMethod]
  361. public void Test_Tables()
  362. {
  363. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  364. {
  365. // There is only one Paragraph at the document level.
  366. Assert.IsTrue(document.Paragraphs.Count() == 13);
  367. // There is only one Table in this document.
  368. Assert.IsTrue(document.Tables.Count() == 1);
  369. // Extract the only Table.
  370. Table t0 = document.Tables[0];
  371. // This table has 12 Paragraphs.
  372. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  373. }
  374. }
  375. [TestMethod]
  376. public void Test_Images()
  377. {
  378. using (DocX document = DocX.Load(directory_documents + "Images.docx"))
  379. {
  380. // Extract Images from Document.
  381. List<Novacode.Image> document_images = document.Images;
  382. // Make sure there are 3 Images in this document.
  383. Assert.IsTrue(document_images.Count() == 3);
  384. // Extract the headers from this document.
  385. Headers headers = document.Headers;
  386. Header header_first = headers.first;
  387. Header header_odd = headers.odd;
  388. Header header_even = headers.even;
  389. #region Header_First
  390. // Extract Images from the first Header.
  391. List<Novacode.Image> header_first_images = header_first.Images;
  392. // Make sure there is 1 Image in the first header.
  393. Assert.IsTrue(header_first_images.Count() == 1);
  394. #endregion
  395. #region Header_Odd
  396. // Extract Images from the odd Header.
  397. List<Novacode.Image> header_odd_images = header_odd.Images;
  398. // Make sure there is 1 Image in the first header.
  399. Assert.IsTrue(header_odd_images.Count() == 1);
  400. #endregion
  401. #region Header_Even
  402. // Extract Images from the odd Header.
  403. List<Novacode.Image> header_even_images = header_even.Images;
  404. // Make sure there is 1 Image in the first header.
  405. Assert.IsTrue(header_even_images.Count() == 1);
  406. #endregion
  407. }
  408. }
  409. [TestMethod]
  410. public void Test_Insert_Picture()
  411. {
  412. // Load test document.
  413. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  414. {
  415. // Add Headers and Footers into this document.
  416. document.AddHeaders();
  417. document.AddFooters();
  418. document.DifferentFirstPage = true;
  419. document.DifferentOddAndEvenPages = true;
  420. // Add an Image to this document.
  421. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  422. // Create a Picture from this Image.
  423. Picture pic = img.CreatePicture();
  424. // Main document.
  425. Paragraph p0 = document.InsertParagraph("Hello");
  426. p0.InsertPicture(pic, 3);
  427. // Header first.
  428. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  429. p1.InsertPicture(pic, 2);
  430. // Header odd.
  431. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  432. p2.InsertPicture(pic, 2);
  433. // Header even.
  434. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  435. p3.InsertPicture(pic, 2);
  436. // Footer first.
  437. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  438. p4.InsertPicture(pic, 2);
  439. // Footer odd.
  440. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  441. p5.InsertPicture(pic, 2);
  442. // Footer even.
  443. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  444. p6.InsertPicture(pic, 2);
  445. // Save this document.
  446. document.Save();
  447. }
  448. }
  449. [TestMethod]
  450. public void Test_Insert_Hyperlink()
  451. {
  452. // Load test document.
  453. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  454. {
  455. // Add Headers and Footers into this document.
  456. document.AddHeaders();
  457. document.AddFooters();
  458. document.DifferentFirstPage = true;
  459. document.DifferentOddAndEvenPages = true;
  460. // Add a Hyperlink into this document.
  461. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  462. // Main document.
  463. Paragraph p0 = document.InsertParagraph("Hello");
  464. p0.InsertHyperlink(h, 3);
  465. // Header first.
  466. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  467. p1.InsertHyperlink(h, 3);
  468. // Header odd.
  469. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  470. p2.InsertHyperlink(h, 3);
  471. // Header even.
  472. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  473. p3.InsertHyperlink(h, 3);
  474. // Footer first.
  475. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  476. p4.InsertHyperlink(h, 3);
  477. // Footer odd.
  478. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  479. p5.InsertHyperlink(h, 3);
  480. // Footer even.
  481. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  482. p6.InsertHyperlink(h, 3);
  483. // Save this document.
  484. document.Save();
  485. }
  486. }
  487. [TestMethod]
  488. public void Test_Get_Set_Hyperlink()
  489. {
  490. // Load test document.
  491. using (DocX document = DocX.Load(directory_documents + "Hyperlinks.docx"))
  492. {
  493. // Hyperlinks in the document.
  494. Assert.IsTrue(document.Hyperlinks.Count == 3);
  495. Assert.IsTrue(document.Hyperlinks[0].Text == "page1");
  496. Assert.IsTrue(document.Hyperlinks[0].Uri.AbsoluteUri == "http://www.page1.com/");
  497. Assert.IsTrue(document.Hyperlinks[1].Text == "page2");
  498. Assert.IsTrue(document.Hyperlinks[1].Uri.AbsoluteUri == "http://www.page2.com/");
  499. Assert.IsTrue(document.Hyperlinks[2].Text == "page3");
  500. Assert.IsTrue(document.Hyperlinks[2].Uri.AbsoluteUri == "http://www.page3.com/");
  501. // Change the Hyperlinks and check that it has in fact changed.
  502. document.Hyperlinks[0].Text = "somethingnew";
  503. document.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  504. Assert.IsTrue(document.Hyperlinks[0].Text == "somethingnew");
  505. Assert.IsTrue(document.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  506. document.Hyperlinks[1].Text = "somethingnew";
  507. document.Hyperlinks[1].Uri = new Uri("http://www.google.com/");
  508. Assert.IsTrue(document.Hyperlinks[1].Text == "somethingnew");
  509. Assert.IsTrue(document.Hyperlinks[1].Uri.AbsoluteUri == "http://www.google.com/");
  510. document.Hyperlinks[2].Text = "somethingnew";
  511. document.Hyperlinks[2].Uri = new Uri("http://www.google.com/");
  512. Assert.IsTrue(document.Hyperlinks[2].Text == "somethingnew");
  513. Assert.IsTrue(document.Hyperlinks[2].Uri.AbsoluteUri == "http://www.google.com/");
  514. Assert.IsTrue(document.Headers.first.Hyperlinks.Count == 1);
  515. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Text == "header-first");
  516. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-first.com/");
  517. // Change the Hyperlinks and check that it has in fact changed.
  518. document.Headers.first.Hyperlinks[0].Text = "somethingnew";
  519. document.Headers.first.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  520. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Text == "somethingnew");
  521. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  522. Assert.IsTrue(document.Headers.odd.Hyperlinks.Count == 1);
  523. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Text == "header-odd");
  524. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-odd.com/");
  525. // Change the Hyperlinks and check that it has in fact changed.
  526. document.Headers.odd.Hyperlinks[0].Text = "somethingnew";
  527. document.Headers.odd.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  528. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Text == "somethingnew");
  529. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  530. Assert.IsTrue(document.Headers.even.Hyperlinks.Count == 1);
  531. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Text == "header-even");
  532. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-even.com/");
  533. // Change the Hyperlinks and check that it has in fact changed.
  534. document.Headers.even.Hyperlinks[0].Text = "somethingnew";
  535. document.Headers.even.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  536. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Text == "somethingnew");
  537. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  538. Assert.IsTrue(document.Footers.first.Hyperlinks.Count == 1);
  539. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Text == "footer-first");
  540. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-first.com/");
  541. // Change the Hyperlinks and check that it has in fact changed.
  542. document.Footers.first.Hyperlinks[0].Text = "somethingnew";
  543. document.Footers.first.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  544. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Text == "somethingnew");
  545. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  546. Assert.IsTrue(document.Footers.odd.Hyperlinks.Count == 1);
  547. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Text == "footer-odd");
  548. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-odd.com/");
  549. // Change the Hyperlinks and check that it has in fact changed.
  550. document.Footers.odd.Hyperlinks[0].Text = "somethingnew";
  551. document.Footers.odd.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  552. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Text == "somethingnew");
  553. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  554. Assert.IsTrue(document.Footers.even.Hyperlinks.Count == 1);
  555. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Text == "footer-even");
  556. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-even.com/");
  557. // Change the Hyperlinks and check that it has in fact changed.
  558. document.Footers.even.Hyperlinks[0].Text = "somethingnew";
  559. document.Footers.even.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  560. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Text == "somethingnew");
  561. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  562. }
  563. }
  564. [TestMethod]
  565. public void Test_Append_Hyperlink()
  566. {
  567. // Load test document.
  568. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  569. {
  570. // Add Headers and Footers into this document.
  571. document.AddHeaders();
  572. document.AddFooters();
  573. document.DifferentFirstPage = true;
  574. document.DifferentOddAndEvenPages = true;
  575. // Add a Hyperlink to this document.
  576. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  577. // Main document.
  578. Paragraph p0 = document.InsertParagraph("----");
  579. p0.AppendHyperlink(h);
  580. Assert.IsTrue(p0.Text == "----google");
  581. // Header first.
  582. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  583. p1.AppendHyperlink(h);
  584. Assert.IsTrue(p1.Text == "----google");
  585. // Header odd.
  586. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  587. p2.AppendHyperlink(h);
  588. Assert.IsTrue(p2.Text == "----google");
  589. // Header even.
  590. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  591. p3.AppendHyperlink(h);
  592. Assert.IsTrue(p3.Text == "----google");
  593. // Footer first.
  594. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  595. p4.AppendHyperlink(h);
  596. Assert.IsTrue(p4.Text == "----google");
  597. // Footer odd.
  598. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  599. p5.AppendHyperlink(h);
  600. Assert.IsTrue(p5.Text == "----google");
  601. // Footer even.
  602. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  603. p6.AppendHyperlink(h);
  604. Assert.IsTrue(p6.Text == "----google");
  605. // Save the document.
  606. document.Save();
  607. }
  608. }
  609. [TestMethod]
  610. public void Test_Append_Picture()
  611. {
  612. // Create test document.
  613. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  614. {
  615. // Add Headers and Footers into this document.
  616. document.AddHeaders();
  617. document.AddFooters();
  618. document.DifferentFirstPage = true;
  619. document.DifferentOddAndEvenPages = true;
  620. // Add an Image to this document.
  621. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  622. // Create a Picture from this Image.
  623. Picture pic = img.CreatePicture();
  624. // Main document.
  625. Paragraph p0 = document.InsertParagraph();
  626. p0.AppendPicture(pic);
  627. // Header first.
  628. Paragraph p1 = document.Headers.first.InsertParagraph();
  629. p1.AppendPicture(pic);
  630. // Header odd.
  631. Paragraph p2 = document.Headers.odd.InsertParagraph();
  632. p2.AppendPicture(pic);
  633. // Header even.
  634. Paragraph p3 = document.Headers.even.InsertParagraph();
  635. p3.AppendPicture(pic);
  636. // Footer first.
  637. Paragraph p4 = document.Footers.first.InsertParagraph();
  638. p4.AppendPicture(pic);
  639. // Footer odd.
  640. Paragraph p5 = document.Footers.odd.InsertParagraph();
  641. p5.AppendPicture(pic);
  642. // Footer even.
  643. Paragraph p6 = document.Footers.even.InsertParagraph();
  644. p6.AppendPicture(pic);
  645. // Save the document.
  646. document.Save();
  647. }
  648. }
  649. [TestMethod]
  650. public void Test_Move_Picture_Load()
  651. {
  652. // Load test document.
  653. using (DocX document = DocX.Load(directory_documents + "MovePicture.docx"))
  654. {
  655. // Extract the first Picture from the first Paragraph.
  656. Picture picture = document.Paragraphs.First().Pictures.First();
  657. // Move it into the first Header.
  658. Header header_first = document.Headers.first;
  659. header_first.Paragraphs.First().AppendPicture(picture);
  660. // Move it into the even Header.
  661. Header header_even = document.Headers.even;
  662. header_even.Paragraphs.First().AppendPicture(picture);
  663. // Move it into the odd Header.
  664. Header header_odd = document.Headers.odd;
  665. header_odd.Paragraphs.First().AppendPicture(picture);
  666. // Move it into the first Footer.
  667. Footer footer_first = document.Footers.first;
  668. footer_first.Paragraphs.First().AppendPicture(picture);
  669. // Move it into the even Footer.
  670. Footer footer_even = document.Footers.even;
  671. footer_even.Paragraphs.First().AppendPicture(picture);
  672. // Move it into the odd Footer.
  673. Footer footer_odd = document.Footers.odd;
  674. footer_odd.Paragraphs.First().AppendPicture(picture);
  675. // Save this as MovedPicture.docx
  676. document.SaveAs(directory_documents + "MovedPicture.docx");
  677. }
  678. }
  679. [TestMethod]
  680. public void Test_Paragraph_InsertHyperlink()
  681. {
  682. // Create a new document
  683. using (DocX document = DocX.Create("Test.docx"))
  684. {
  685. // Add a Hyperlink to this document.
  686. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  687. // Simple
  688. Paragraph p1 = document.InsertParagraph("AC");
  689. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  690. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  691. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  692. // Difficult
  693. Paragraph p2 = document.InsertParagraph("\tA\tC\t");
  694. p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
  695. p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
  696. p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
  697. // Contrived
  698. // Add a contrived Hyperlink to this document.
  699. Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
  700. Paragraph p3 = document.InsertParagraph("\tA\tC\t");
  701. p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
  702. p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
  703. p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
  704. }
  705. }
  706. [TestMethod]
  707. public void Test_Paragraph_RemoveHyperlink()
  708. {
  709. // Create a new document
  710. using (DocX document = DocX.Create("Test.docx"))
  711. {
  712. // Add a Hyperlink to this document.
  713. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  714. // Simple
  715. Paragraph p1 = document.InsertParagraph("AC");
  716. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  717. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  718. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  719. // Try and remove a Hyperlink using a negative index.
  720. // This should throw an exception.
  721. try
  722. {
  723. p1.RemoveHyperlink(-1);
  724. Assert.Fail();
  725. }
  726. catch (ArgumentException) { }
  727. catch (Exception) { Assert.Fail(); }
  728. // Try and remove a Hyperlink at an index greater than the last.
  729. // This should throw an exception.
  730. try
  731. {
  732. p1.RemoveHyperlink(3);
  733. Assert.Fail();
  734. }
  735. catch (ArgumentException) { }
  736. catch (Exception) { Assert.Fail(); }
  737. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  738. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  739. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  740. }
  741. }
  742. [TestMethod]
  743. public void Test_Paragraph_ReplaceText()
  744. {
  745. // Create a new document
  746. using (DocX document = DocX.Create("Test.docx"))
  747. {
  748. // Simple
  749. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  750. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  751. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  752. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  753. // Try and replace text that dosen't exist in the Paragraph.
  754. string old = p1.Text;
  755. p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
  756. // Difficult
  757. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  758. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  759. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  760. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  761. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  762. }
  763. }
  764. [TestMethod]
  765. public void Test_Paragraph_RemoveText()
  766. {
  767. // Create a new document
  768. using (DocX document = DocX.Create("Test.docx"))
  769. {
  770. // Simple
  771. //<p>
  772. // <r><t>HellWorld</t></r>
  773. //</p>
  774. Paragraph p1 = document.InsertParagraph("HelloWorld");
  775. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  776. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  777. p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
  778. // Try and remove text at an index greater than the last.
  779. // This should throw an exception.
  780. try
  781. {
  782. p1.RemoveText(p1.Text.Length, 1);
  783. Assert.Fail();
  784. }
  785. catch (ArgumentOutOfRangeException) { }
  786. catch (Exception) { Assert.Fail(); }
  787. // Try and remove text at a negative index.
  788. // This should throw an exception.
  789. try
  790. {
  791. p1.RemoveText(-1, 1);
  792. Assert.Fail();
  793. }
  794. catch (ArgumentOutOfRangeException) { }
  795. catch (Exception) { Assert.Fail(); }
  796. // Difficult
  797. //<p>
  798. // <r><t>A</t></r>
  799. // <r><t>B</t></r>
  800. // <r><t>C</t></r>
  801. //</p>
  802. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  803. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  804. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  805. p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
  806. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  807. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  808. // Contrived 1
  809. //<p>
  810. // <r>
  811. // <t>A</t>
  812. // <t>B</t>
  813. // <t>C</t>
  814. // </r>
  815. //</p>
  816. Paragraph p3 = document.InsertParagraph("");
  817. p3.Xml = XElement.Parse
  818. (
  819. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  820. <w:pPr />
  821. <w:r>
  822. <w:rPr />
  823. <w:t>A</w:t>
  824. <w:t>B</w:t>
  825. <w:t>C</w:t>
  826. </w:r>
  827. </w:p>"
  828. );
  829. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  830. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  831. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  832. // Contrived 2
  833. //<p>
  834. // <r>
  835. // <t>A</t>
  836. // <t>B</t>
  837. // <t>C</t>
  838. // </r>
  839. //</p>
  840. Paragraph p4 = document.InsertParagraph("");
  841. p4.Xml = XElement.Parse
  842. (
  843. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  844. <w:pPr />
  845. <w:r>
  846. <w:rPr />
  847. <tab />
  848. <w:t>A</w:t>
  849. <tab />
  850. </w:r>
  851. <w:r>
  852. <w:rPr />
  853. <tab />
  854. <w:t>B</w:t>
  855. <tab />
  856. </w:r>
  857. </w:p>"
  858. );
  859. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  860. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  861. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  862. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  863. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  864. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  865. }
  866. }
  867. [TestMethod]
  868. public void Test_Paragraph_InsertText()
  869. {
  870. // Create a new document
  871. using (DocX document = DocX.Create("Test.docx"))
  872. {
  873. // Simple
  874. //<p>
  875. // <r><t>HelloWorld</t></r>
  876. //</p>
  877. Paragraph p1 = document.InsertParagraph("HelloWorld");
  878. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  879. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  880. p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  881. // Try and insert text at an index greater than the last + 1.
  882. // This should throw an exception.
  883. try
  884. {
  885. p1.InsertText(p1.Text.Length + 1, "-");
  886. Assert.Fail();
  887. }
  888. catch (ArgumentOutOfRangeException) { }
  889. catch (Exception) { Assert.Fail(); }
  890. // Try and insert text at a negative index.
  891. // This should throw an exception.
  892. try
  893. {
  894. p1.InsertText(-1, "-");
  895. Assert.Fail();
  896. }
  897. catch (ArgumentOutOfRangeException) { }
  898. catch (Exception) { Assert.Fail(); }
  899. // Difficult
  900. //<p>
  901. // <r><t>A</t></r>
  902. // <r><t>B</t></r>
  903. // <r><t>C</t></r>
  904. //</p>
  905. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  906. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  907. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  908. p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  909. p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  910. // Contrived 1
  911. //<p>
  912. // <r>
  913. // <t>A</t>
  914. // <t>B</t>
  915. // <t>C</t>
  916. // </r>
  917. //</p>
  918. Paragraph p3 = document.InsertParagraph("");
  919. p3.Xml = XElement.Parse
  920. (
  921. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  922. <w:pPr />
  923. <w:r>
  924. <w:rPr />
  925. <w:t>A</w:t>
  926. <w:t>B</w:t>
  927. <w:t>C</w:t>
  928. </w:r>
  929. </w:p>"
  930. );
  931. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  932. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  933. p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  934. p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  935. // Contrived 2
  936. //<p>
  937. // <r>
  938. // <t>A</t>
  939. // <t>B</t>
  940. // <t>C</t>
  941. // </r>
  942. //</p>
  943. Paragraph p4 = document.InsertParagraph("");
  944. p4.Xml = XElement.Parse
  945. (
  946. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  947. <w:pPr />
  948. <w:r>
  949. <w:rPr />
  950. <w:t>A</w:t>
  951. <w:t>B</w:t>
  952. <w:t>C</w:t>
  953. </w:r>
  954. </w:p>"
  955. );
  956. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  957. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  958. p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  959. p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  960. }
  961. }
  962. [TestMethod]
  963. public void Test_Document_Paragraphs()
  964. {
  965. // Load the document 'Paragraphs.docx'
  966. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  967. {
  968. // Extract the Paragraphs from this document.
  969. List<Paragraph> paragraphs = document.Paragraphs;
  970. // There should be 3 Paragraphs in this document.
  971. Assert.IsTrue(paragraphs.Count() == 3);
  972. // Extract the 3 Paragraphs.
  973. Paragraph p1 = paragraphs[0];
  974. Paragraph p2 = paragraphs[1];
  975. Paragraph p3 = paragraphs[2];
  976. // Extract their Text properties.
  977. string p1_text = p1.Text;
  978. string p2_text = p2.Text;
  979. string p3_text = p3.Text;
  980. // Test their Text properties against absolutes.
  981. Assert.IsTrue(p1_text == "Paragraph 1");
  982. Assert.IsTrue(p2_text == "Paragraph 2");
  983. Assert.IsTrue(p3_text == "Paragraph 3");
  984. // Its important that each Paragraph knows the PackagePart it belongs to.
  985. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  986. // Test the saving of the document.
  987. document.SaveAs(file_temp);
  988. }
  989. // Delete the tempory file.
  990. File.Delete(file_temp);
  991. }
  992. [TestMethod]
  993. public void Test_Table_mainPart_bug9526()
  994. {
  995. using (DocX document = DocX.Create("test.docx"))
  996. {
  997. Hyperlink h = document.AddHyperlink("follow me", new Uri("http://www.google.com"));
  998. Table t = document.AddTable(2, 3);
  999. int cc = t.ColumnCount;
  1000. Paragraph p = t.Rows[0].Cells[0].Paragraphs[0];
  1001. p.AppendHyperlink(h);
  1002. }
  1003. }
  1004. [TestMethod]
  1005. public void Test_Table_InsertRowAndColumn()
  1006. {
  1007. // Create a table
  1008. using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
  1009. {
  1010. // Add a Table to a document.
  1011. Table t = document.AddTable(2, 2);
  1012. t.Design = TableDesign.TableGrid;
  1013. t.Rows[0].Cells[0].Paragraphs[0].InsertText("X");
  1014. t.Rows[0].Cells[1].Paragraphs[0].InsertText("X");
  1015. t.Rows[1].Cells[0].Paragraphs[0].InsertText("X");
  1016. t.Rows[1].Cells[1].Paragraphs[0].InsertText("X");
  1017. // Insert the Table into the main section of the document.
  1018. Table t1 = document.InsertTable(t);
  1019. // ... and add a column and a row
  1020. t1.InsertRow(1);
  1021. t1.InsertColumn(1);
  1022. // Save the document.
  1023. document.Save();
  1024. }
  1025. // Check table
  1026. using (DocX document = DocX.Load(directory_documents + "Tables2.docx"))
  1027. {
  1028. // Get a table from a document
  1029. Table t = document.Tables[0];
  1030. // Check that the table is equal this:
  1031. // X - X
  1032. // - - -
  1033. // X - X
  1034. Assert.AreEqual("X", t.Rows[0].Cells[0].Paragraphs[0].Text);
  1035. Assert.AreEqual("X", t.Rows[2].Cells[0].Paragraphs[0].Text);
  1036. Assert.AreEqual("X", t.Rows[0].Cells[2].Paragraphs[0].Text);
  1037. Assert.AreEqual("X", t.Rows[2].Cells[2].Paragraphs[0].Text);
  1038. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[0].Paragraphs[0].Text));
  1039. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[1].Paragraphs[0].Text));
  1040. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[2].Paragraphs[0].Text));
  1041. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[0].Cells[1].Paragraphs[0].Text));
  1042. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[2].Cells[1].Paragraphs[0].Text));
  1043. }
  1044. }
  1045. [TestMethod]
  1046. public void Test_Document_ApplyTemplate()
  1047. {
  1048. using (MemoryStream documentStream = new MemoryStream())
  1049. {
  1050. using (DocX document = DocX.Create(documentStream))
  1051. {
  1052. document.ApplyTemplate(directory_documents + "Template.dotx");
  1053. document.Save();
  1054. Header firstHeader = document.Headers.first;
  1055. Header oddHeader = document.Headers.odd;
  1056. Header evenHeader = document.Headers.even;
  1057. Footer firstFooter = document.Footers.first;
  1058. Footer oddFooter = document.Footers.odd;
  1059. Footer evenFooter = document.Footers.even;
  1060. Assert.IsTrue(firstHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1061. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  1062. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1063. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  1064. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1065. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  1066. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1067. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  1068. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1069. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  1070. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1071. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  1072. Paragraph firstParagraph = document.Paragraphs[0];
  1073. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  1074. }
  1075. }
  1076. }
  1077. [TestMethod]
  1078. public void Test_ParentContainer_When_Creating_Doc()
  1079. {
  1080. using (DocX document = DocX.Create("Test.docx"))
  1081. {
  1082. document.AddHeaders();
  1083. Paragraph p1 = document.Headers.first.InsertParagraph("Test");
  1084. Assert.IsTrue(p1.ParentContainer == ContainerType.Header);
  1085. }
  1086. }
  1087. [TestMethod]
  1088. public void Test_Section_Count_When_Creating_Doc()
  1089. {
  1090. //This adds a section break - so insert paragraphs, and follow it up by a section break/paragraph
  1091. using (DocX document = DocX.Create("TestSectionCount.docx"))
  1092. {
  1093. document.InsertSection();
  1094. var sections = document.GetSections();
  1095. Assert.AreEqual(sections.Count(), 2);
  1096. }
  1097. }
  1098. [TestMethod]
  1099. public void Test_Sections_And_Paragraphs_When_Creating_Doc()
  1100. {
  1101. //This adds a section break - so insert paragraphs, and follow it up by a section break/paragraph
  1102. using (DocX document = DocX.Create("TestSectionAndParagraph.docx"))
  1103. {
  1104. //Add 2 paras and a break
  1105. document.InsertParagraph("First Para");
  1106. document.InsertParagraph("Second Para");
  1107. document.InsertSection();
  1108. document.InsertParagraph("This is default para");
  1109. var sections = document.GetSections();
  1110. Assert.AreEqual(sections.Count(), 2);
  1111. }
  1112. }
  1113. [TestMethod]
  1114. public void Test_ParentContainer_When_Reading_Doc()
  1115. {
  1116. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  1117. {
  1118. List<Paragraph> paragraphs = document.Paragraphs;
  1119. Paragraph p1 = paragraphs[0];
  1120. Assert.IsTrue(p1.ParentContainer == ContainerType.Cell);
  1121. }
  1122. }
  1123. [TestMethod]
  1124. public void Test_Section_Count_When_Reading_Doc()
  1125. {
  1126. using (DocX document = DocX.Load(directory_documents + "testdoc_SectionsWithSectionBreaks.docx"))
  1127. {
  1128. var sections = document.GetSections();
  1129. Assert.AreEqual(sections.Count(), 4);
  1130. }
  1131. }
  1132. [TestMethod]
  1133. public void Test_Section_Paragraph_Count_Match_When_Reading_Doc()
  1134. {
  1135. using (DocX document = DocX.Load(directory_documents + "testdoc_SectionsWithSectionBreaksMultiParagraph.docx"))
  1136. {
  1137. var sections = document.GetSections();
  1138. Assert.AreEqual(sections[0].SectionParagraphs.Count, 2);
  1139. Assert.AreEqual(sections[1].SectionParagraphs.Count, 1);
  1140. Assert.AreEqual(sections[2].SectionParagraphs.Count, 2);
  1141. Assert.AreEqual(sections[3].SectionParagraphs.Count, 1);
  1142. }
  1143. }
  1144. [TestMethod]
  1145. public void Test_Section_Paragraph_Content_Match_When_Reading_Doc()
  1146. {
  1147. using (DocX document = DocX.Load(directory_documents + "testdoc_SectionsWithSectionBreaks.docx"))
  1148. {
  1149. var sections = document.GetSections();
  1150. Assert.IsTrue(sections[0].SectionParagraphs[0].Text.Contains("Section 1"));
  1151. Assert.IsTrue(sections[1].SectionParagraphs[0].Text.Contains("Section 2"));
  1152. Assert.IsTrue(sections[2].SectionParagraphs[0].Text.Contains("Section 3"));
  1153. Assert.IsTrue(sections[3].SectionParagraphs[0].Text.Contains("Section 4"));
  1154. }
  1155. }
  1156. [TestMethod]
  1157. public void Test_Ordered_List_When_Reading_Doc()
  1158. {
  1159. using (DocX document = DocX.Load(directory_documents + "testdoc_OrderedList.docx"))
  1160. {
  1161. var sections = document.GetSections();
  1162. Assert.IsTrue(sections[0].SectionParagraphs[0].IsListItem);
  1163. Assert.IsTrue(sections[0].SectionParagraphs[1].IsListItem);
  1164. Assert.IsTrue(sections[0].SectionParagraphs[2].IsListItem);
  1165. Assert.AreEqual(sections[0].SectionParagraphs[0].ListItemType, ListItemType.Numbered);
  1166. Assert.AreEqual(sections[0].SectionParagraphs[1].ListItemType, ListItemType.Numbered);
  1167. Assert.AreEqual(sections[0].SectionParagraphs[2].ListItemType, ListItemType.Numbered);
  1168. }
  1169. }
  1170. [TestMethod]
  1171. public void Test_Unordered_List_When_Reading_Doc()
  1172. {
  1173. using (DocX document = DocX.Load(directory_documents + "testdoc_UnorderedList.docx"))
  1174. {
  1175. var sections = document.GetSections();
  1176. Assert.IsTrue(sections[0].SectionParagraphs[0].IsListItem);
  1177. Assert.IsTrue(sections[0].SectionParagraphs[1].IsListItem);
  1178. Assert.IsTrue(sections[0].SectionParagraphs[2].IsListItem);
  1179. Assert.AreEqual(sections[0].SectionParagraphs[0].ListItemType, ListItemType.Bulleted);
  1180. Assert.AreEqual(sections[0].SectionParagraphs[1].ListItemType, ListItemType.Bulleted);
  1181. Assert.AreEqual(sections[0].SectionParagraphs[2].ListItemType, ListItemType.Bulleted);
  1182. }
  1183. }
  1184. [TestMethod]
  1185. public void Test_Ordered_Unordered_Lists_When_Reading_Doc()
  1186. {
  1187. using (DocX document = DocX.Load(directory_documents + "testdoc_OrderedUnorderedLists.docx"))
  1188. {
  1189. var sections = document.GetSections();
  1190. Assert.IsTrue(sections[0].SectionParagraphs[0].IsListItem);
  1191. Assert.IsTrue(sections[0].SectionParagraphs[1].IsListItem);
  1192. Assert.IsTrue(sections[0].SectionParagraphs[2].IsListItem);
  1193. Assert.AreEqual(sections[0].SectionParagraphs[0].ListItemType, ListItemType.Numbered);
  1194. Assert.AreEqual(sections[0].SectionParagraphs[1].ListItemType, ListItemType.Numbered);
  1195. Assert.AreEqual(sections[0].SectionParagraphs[2].ListItemType, ListItemType.Numbered);
  1196. Assert.IsTrue(sections[0].SectionParagraphs[3].IsListItem);
  1197. Assert.IsTrue(sections[0].SectionParagraphs[4].IsListItem);
  1198. Assert.IsTrue(sections[0].SectionParagraphs[5].IsListItem);
  1199. Assert.AreEqual(sections[0].SectionParagraphs[3].ListItemType, ListItemType.Bulleted);
  1200. Assert.AreEqual(sections[0].SectionParagraphs[4].ListItemType, ListItemType.Bulleted);
  1201. Assert.AreEqual(sections[0].SectionParagraphs[5].ListItemType, ListItemType.Bulleted);
  1202. }
  1203. }
  1204. [TestMethod]
  1205. public void WhenCreatingAnOrderedListTheListXmlShouldHaveNumberedListItemType()
  1206. {
  1207. using (DocX document = DocX.Create("TestListXmlNumbered.docx"))
  1208. {
  1209. const int level = 0;
  1210. XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  1211. var list = document.AddList("First Item", level, ListItemType.Numbered);
  1212. document.InsertList(list);
  1213. var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr");
  1214. var numId = listNumPropNode.Descendants().First(s => s.Name.LocalName == "numId");
  1215. var abstractNum = list.GetAbstractNum(int.Parse(numId.GetAttribute(w + "val")));
  1216. var lvl = abstractNum.Descendants().First(d => d.Name.LocalName == "lvl" && d.GetAttribute(w + "ilvl").Equals(level.ToString()));
  1217. var numFormat = lvl.Descendants().First(d => d.Name.LocalName == "numFmt");
  1218. Assert.AreEqual(numFormat.GetAttribute(w + "val").ToLower(), "decimal");
  1219. }
  1220. }
  1221. [TestMethod]
  1222. public void WhenCreatingAnUnOrderedListTheListXmlShouldHaveBulletListItemType()
  1223. {
  1224. using (DocX document = DocX.Create("TestListXmlBullet.docx"))
  1225. {
  1226. var list = document.AddList("First Item", 0, ListItemType.Bulleted);
  1227. document.InsertList(list);
  1228. var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr");
  1229. var numId = listNumPropNode.Descendants().First(s => s.Name.LocalName == "numId");
  1230. Assert.AreEqual(numId.Attribute(DocX.w + "val").Value, "1");
  1231. }
  1232. }
  1233. [TestMethod]
  1234. public void WhenCreatingAListWithTextTheListXmlShouldHaveTheCorrectRunItemText()
  1235. {
  1236. using (DocX document = DocX.Create("TestListCreate.docx"))
  1237. {
  1238. const string listText = "RunText";
  1239. var list = document.AddList(listText, 0, ListItemType.Bulleted);
  1240. document.InsertList(list);
  1241. var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr");
  1242. var runTextNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "t");
  1243. Assert.IsNotNull(listNumPropNode);
  1244. Assert.AreEqual(list.Items.First().runs.First().Value, runTextNode.Value);
  1245. Assert.AreEqual(listText, runTextNode.Value);
  1246. }
  1247. }
  1248. [TestMethod]
  1249. public void WhenCreatingAnOrderedListTheListShouldHaveNumberedListItemType()
  1250. {
  1251. using (DocX document = DocX.Create("TestListCreateOrderedList.docx"))
  1252. {
  1253. var list = document.AddList("First Item");
  1254. Assert.AreEqual(list.ListType, ListItemType.Numbered);
  1255. }
  1256. }
  1257. [TestMethod]
  1258. public void WhenCreatingAnUnOrderedListTheListShouldHaveBulletListItemType()
  1259. {
  1260. using (DocX document = DocX.Create("TestListCreateUnorderedList.docx"))
  1261. {
  1262. var list = document.AddList("First Item", 0, ListItemType.Bulleted);
  1263. Assert.AreEqual(list.ListType, ListItemType.Bulleted);
  1264. }
  1265. }
  1266. [TestMethod]
  1267. public void WhenCreatingAListWithTextTheListShouldHaveTheCorrectRunItemText()
  1268. {
  1269. using (DocX document = DocX.Create("TestListCreateRunText.docx"))
  1270. {
  1271. var list = document.AddList("RunText", 0, ListItemType.Bulleted);
  1272. document.InsertList(list);
  1273. Assert.AreEqual(list.Items.First().runs.First().Value, "RunText");
  1274. }
  1275. }
  1276. [TestMethod]
  1277. public void WhenCreatingAListTheNumberingShouldGetSaved()
  1278. {
  1279. }
  1280. [TestMethod]
  1281. public void WhenCreatingAListTheListStyleShouldExistOrBeCreated()
  1282. {
  1283. using (DocX document = DocX.Create("TestListStyle.docx"))
  1284. {
  1285. var style = document.AddStylesForList();
  1286. XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  1287. bool listStyleExists =
  1288. (
  1289. from s in style.Element(w + "styles").Elements()
  1290. let styleId = s.Attribute(XName.Get("styleId", w.NamespaceName))
  1291. where (styleId != null && styleId.Value == "ListParagraph")
  1292. select s
  1293. ).Any();
  1294. Assert.IsTrue(listStyleExists);
  1295. }
  1296. }
  1297. [TestMethod]
  1298. public void ANewListItemShouldCreateAnAbstractNumberingEntry()
  1299. {
  1300. using (DocX document = DocX.Create("TestNumbering.docx"))
  1301. {
  1302. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1303. Assert.IsFalse(numbering.Any());
  1304. document.AddList("List Text");
  1305. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1306. Assert.IsTrue(numbering.Any());
  1307. }
  1308. }
  1309. [TestMethod]
  1310. public void ANewListItemShouldCreateANewNumEntry()
  1311. {
  1312. using (DocX document = DocX.Create("TestNumEntry.docx"))
  1313. {
  1314. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1315. Assert.IsFalse(numbering.Any());
  1316. document.AddList("List Text");
  1317. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1318. Assert.IsTrue(numbering.Any());
  1319. }
  1320. }
  1321. [TestMethod]
  1322. public void CreateNewNumberingNumIdShouldAddNumberingDataToTheDocument()
  1323. {
  1324. using (DocX document = DocX.Create("TestCreateNumbering.docx"))
  1325. {
  1326. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1327. Assert.IsFalse(numbering.Any());
  1328. var list = document.AddList("", 0, ListItemType.Bulleted);
  1329. document.InsertList(list);
  1330. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1331. Assert.IsTrue(numbering.Any());
  1332. }
  1333. }
  1334. [TestMethod]
  1335. public void CreateNewNumberingNumIdShouldAddNumberingAbstractDataToTheDocument()
  1336. {
  1337. using (DocX document = DocX.Create("TestCreateNumberingAbstract.docx"))
  1338. {
  1339. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1340. Assert.IsFalse(numbering.Any());
  1341. var list = document.AddList("", 0, ListItemType.Bulleted);
  1342. document.InsertList(list);
  1343. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1344. Assert.IsTrue(numbering.Any());
  1345. }
  1346. }
  1347. [TestMethod]
  1348. public void IfPreviousElementIsAListThenAddingANewListContinuesThePreviousList()
  1349. {
  1350. using (DocX document = DocX.Create("TestAddListToPreviousList.docx"))
  1351. {
  1352. var list = document.AddList("List Text");
  1353. document.AddListItem(list, "List Text2");
  1354. document.InsertList(list);
  1355. var lvlNodes = document.mainDoc.Descendants().Where(s => s.Name.LocalName == "ilvl").ToList();
  1356. var numIdNodes = document.mainDoc.Descendants().Where(s => s.Name.LocalName == "numId").ToList();
  1357. Assert.AreEqual(lvlNodes.Count(), 2);
  1358. Assert.AreEqual(numIdNodes.Count(), 2);
  1359. var prevLvlNode = lvlNodes[0];
  1360. var newLvlNode = lvlNodes[1];
  1361. Assert.AreEqual(prevLvlNode.Attribute(DocX.w + "val").Value, newLvlNode.Attribute(DocX.w + "val").Value);
  1362. var prevNumIdNode = numIdNodes[0];
  1363. var newNumIdNode = numIdNodes[1];
  1364. Assert.AreEqual(prevNumIdNode.Attribute(DocX.w + "val").Value, newNumIdNode.Attribute(DocX.w + "val").Value);
  1365. document.Save();
  1366. }
  1367. }
  1368. [TestMethod]
  1369. public void WhenADocumentHasListsTheListPropertyReturnsTheCorrectNumberOfLists()
  1370. {
  1371. using (DocX document = DocX.Load(directory_documents + "testdoc_OrderedUnorderedLists.docx"))
  1372. {
  1373. var lists = document.Lists;
  1374. Assert.AreEqual(lists.Count, 2);
  1375. }
  1376. }
  1377. [TestMethod]
  1378. public void WhenADocumentIsCreatedWithAListItemThatHasASpecifiedStartNumber()
  1379. {
  1380. using (DocX document = DocX.Create("CreateListItemFromDifferentStartValue.docx"))
  1381. {
  1382. var list = document.AddList("Test", 0, ListItemType.Numbered, 5);
  1383. document.AddListItem(list, "NewElement");
  1384. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1385. var level = numbering.Descendants().First(el => el.Name.LocalName == "lvl");
  1386. var start = level.Descendants().First(el => el.Name.LocalName == "start");
  1387. Assert.AreEqual(start.GetAttribute(DocX.w + "val"), 5.ToString());
  1388. }
  1389. }
  1390. [TestMethod]
  1391. public void WhenANumberedAndBulletedListIsCreatedThereShouldBeTwoNumberingXmls()
  1392. {
  1393. using (DocX document = DocX.Create("NumberAndBulletListInOne.docx"))
  1394. {
  1395. var numberList = document.AddList("Test");
  1396. document.AddListItem(numberList, "Second Numbered Item");
  1397. var bulletedList = document.AddList("Bullet", 0, ListItemType.Bulleted);
  1398. document.AddListItem(bulletedList, "Second bullet item");
  1399. document.InsertList(numberList);
  1400. document.InsertList(bulletedList);
  1401. var abstractNums = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1402. Assert.AreEqual(abstractNums.Count(), 2);
  1403. }
  1404. }
  1405. [TestMethod]
  1406. public void WhenICreateAnEmptyListAndAddEntriesToIt()
  1407. {
  1408. using (DocX document = DocX.Create("CreateEmptyListAndAddItemsToIt.docx"))
  1409. {
  1410. var list = document.AddList();
  1411. Assert.AreEqual(list.Items.Count, 0);
  1412. document.AddListItem(list, "Test item 1.");
  1413. document.AddListItem(list, "Test item 2.");
  1414. Assert.AreEqual(list.Items.Count, 2);
  1415. }
  1416. }
  1417. [TestMethod]
  1418. public void WhenICreateAHeaderItShouldHaveAStyle()
  1419. {
  1420. using (var document = DocX.Create("CreateHeaderElement.docx"))
  1421. {
  1422. document.InsertParagraph("Header Text 1").StyleName = "Header1";
  1423. Assert.IsNotNull(document.styles.Root.Descendants().FirstOrDefault(d => d.GetAttribute(DocX.w + "styleId").ToLowerInvariant() == "heading1"));
  1424. document.Save();
  1425. }
  1426. }
  1427. [TestMethod]
  1428. public void WhileReadingWhenTextIsBoldItalicUnderlineItShouldReadTheProperFormatting()
  1429. {
  1430. using (DocX document = DocX.Load(directory_documents + "FontFormat.docx"))
  1431. {
  1432. var underlinedTextFormatting = document.Paragraphs[0].MagicText[0].formatting;
  1433. var boldTextFormatting = document.Paragraphs[0].MagicText[2].formatting;
  1434. var italicTextFormatting = document.Paragraphs[0].MagicText[4].formatting;
  1435. var boldItalicUnderlineTextFormatting = document.Paragraphs[0].MagicText[6].formatting;
  1436. Assert.IsTrue(boldTextFormatting.Bold);
  1437. Assert.IsTrue(italicTextFormatting.Italic);
  1438. Assert.AreEqual(underlinedTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1439. Assert.IsTrue(boldItalicUnderlineTextFormatting.Bold);
  1440. Assert.IsTrue(boldItalicUnderlineTextFormatting.Italic);
  1441. Assert.AreEqual(boldItalicUnderlineTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1442. }
  1443. }
  1444. [TestMethod]
  1445. public void WhileWritingWhenTextIsBoldItalicUnderlineItShouldReadTheProperFormatting()
  1446. {
  1447. using (DocX document = DocX.Create("FontFormatWrite.docx"))
  1448. {
  1449. Paragraph p = document.InsertParagraph();
  1450. p.Append("This is bold.").Bold().Append("This is underlined.").UnderlineStyle(UnderlineStyle.singleLine).
  1451. Append("This is italic.").Italic().Append("This is boldItalicUnderlined").Italic().Bold().UnderlineStyle(UnderlineStyle.singleLine);
  1452. var boldTextFormatting = document.Paragraphs[0].MagicText[0].formatting;
  1453. var underlinedTextFormatting = document.Paragraphs[0].MagicText[1].formatting;
  1454. var italicTextFormatting = document.Paragraphs[0].MagicText[2].formatting;
  1455. var boldItalicUnderlineTextFormatting = document.Paragraphs[0].MagicText[3].formatting;
  1456. Assert.IsTrue(boldTextFormatting.Bold);
  1457. Assert.IsTrue(italicTextFormatting.Italic);
  1458. Assert.AreEqual(underlinedTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1459. Assert.IsTrue(boldItalicUnderlineTextFormatting.Bold);
  1460. Assert.IsTrue(boldItalicUnderlineTextFormatting.Italic);
  1461. Assert.AreEqual(boldItalicUnderlineTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1462. }
  1463. }
  1464. [TestMethod]
  1465. public void InsertingANextPageBreakShouldAddADocumentSection()
  1466. {
  1467. using (DocX document = DocX.Create("SectionPageBreak.docx"))
  1468. {
  1469. document.InsertSectionPageBreak();
  1470. var sections = document.GetSections();
  1471. Assert.AreEqual(sections.Count, 2);
  1472. document.Save();
  1473. }
  1474. }
  1475. [TestMethod]
  1476. public void InsertANextPageBreakWithParagraphTextsShouldAddProperParagraphsToProperSections()
  1477. {
  1478. using (DocX document = DocX.Create("SectionPageBreakWithParagraphs.docx"))
  1479. {
  1480. document.InsertParagraph("First paragraph.");
  1481. document.InsertParagraph("Second paragraph.");
  1482. document.InsertSectionPageBreak();
  1483. document.InsertParagraph("Third paragraph.");
  1484. document.InsertParagraph("Fourth paragraph.");
  1485. var sections = document.GetSections();
  1486. Assert.AreEqual(sections.Count, 2);
  1487. Assert.AreEqual(sections[0].SectionParagraphs.Count(p => !string.IsNullOrWhiteSpace(p.Text)), 2);
  1488. Assert.AreEqual(sections[1].SectionParagraphs.Count(p => !string.IsNullOrWhiteSpace(p.Text)), 2);
  1489. document.Save();
  1490. }
  1491. }
  1492. [TestMethod]
  1493. public void WhenAFontFamilyIsSpecifiedForAParagraphItShouldSetTheFontOfTheParagraphTextToTheFontFamily()
  1494. {
  1495. using (DocX document = DocX.Create("FontTest.docx"))
  1496. {
  1497. Paragraph p = document.InsertParagraph();
  1498. p.Append("Hello World").Font(new FontFamily("Century"));
  1499. Assert.AreEqual(p.MagicText[0].formatting.FontFamily.Name, "Century");
  1500. document.Save();
  1501. }
  1502. }
  1503. }
  1504. }