Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocXUnitTests.cs 88KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  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. 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. [TestMethod]
  1505. public void Test_Paragraph_RemoveTextManyLetters()
  1506. {
  1507. using (DocX document = DocX.Create(@"docs\HelloWorldRemovingManyLetters.docx"))
  1508. {
  1509. Paragraph p3 = document.InsertParagraph("");
  1510. p3.Xml = XElement.Parse(
  1511. @"<w:p xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
  1512. <w:pPr>
  1513. <w:ind />
  1514. </w:pPr>
  1515. <w:r>
  1516. <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>
  1517. </w:r>
  1518. <w:r>
  1519. <w:rPr>
  1520. <w:lang w:val=""pl-PL"" />
  1521. </w:rPr>
  1522. <w:t xml:space=""preserve"">This survey should take </w:t>
  1523. </w:r>
  1524. <w:r>
  1525. <w:t>"" + f('xMinutes').get() + ""</w:t>
  1526. </w:r>
  1527. <w:r>
  1528. <w:rPr>
  1529. <w:lang w:val=""pl-PL"" />
  1530. </w:rPr>
  1531. <w:t xml:space=""preserve""> minutes. </w:t>
  1532. </w:r>
  1533. <w:r>
  1534. <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>
  1535. </w:r>
  1536. </w:p>");
  1537. int l1 = p3.Text.Length; //960
  1538. p3.RemoveText(318, 99);
  1539. int l2 = p3.Text.Length; //should be 861
  1540. Assert.AreEqual(l1 - 99, l2);
  1541. }
  1542. }
  1543. }
  1544. }