You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocXUnitTests.cs 95KB

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