Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DocXUnitTests.cs 86KB

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