Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DocXUnitTests.cs 84KB

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