您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UnitTest1.cs 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Novacode;
  7. using System.Reflection;
  8. using System.IO;
  9. using System.Drawing;
  10. using System.Drawing.Imaging;
  11. using System.Xml.Linq;
  12. using System.IO.Packaging;
  13. using System.Text.RegularExpressions;
  14. namespace UnitTests
  15. {
  16. /// <summary>
  17. /// Summary description for UnitTest1
  18. /// </summary>
  19. [TestClass]
  20. public class UnitTest1
  21. {
  22. // Get the fullpath to the executing assembly.
  23. string directory_executing_assembly;
  24. string directory_documents;
  25. string file_temp = "temp.docx";
  26. const string package_part_document = "/word/document.xml";
  27. public UnitTest1()
  28. {
  29. directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
  30. // The documents directory
  31. List<string> steps = directory_executing_assembly.Split('\\').ToList();
  32. steps.RemoveRange(steps.Count() - 3, 3);
  33. directory_documents = String.Join("\\", steps) + "\\documents\\";
  34. }
  35. [TestMethod]
  36. public void Test_Pattern_Replacement()
  37. {
  38. Dictionary<string, string> testPatterns = new Dictionary<string, string>()
  39. {
  40. {"COURT NAME","Fred Frump"},
  41. {"Case Number","cr-md-2011-1234567"}
  42. };
  43. using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
  44. {
  45. foreach (var t in replaceDoc.Tables)
  46. { // each table has 1 row and 3 columns
  47. Assert.IsTrue(t.Rows[0].Cells.Count == 3);
  48. Assert.IsTrue(t.ColumnCount == 3);
  49. Assert.IsTrue(t.Rows.Count == 1);
  50. Assert.IsTrue(t.RowCount == 1);
  51. }
  52. // Make sure the origional strings are in the document.
  53. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
  54. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
  55. // There are only two patterns, even though each pattern is used more than once
  56. Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
  57. // Make sure the new strings are not in the document.
  58. Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
  59. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
  60. // Do the replacing
  61. foreach (var p in testPatterns)
  62. replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
  63. // Make sure the origional string are no longer in the document.
  64. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
  65. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
  66. // Make sure the new strings are now in the document.
  67. Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
  68. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
  69. // Make sure the replacement worked.
  70. 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");
  71. }
  72. }
  73. [TestMethod]
  74. public void Test_CustomProperty_Add()
  75. {
  76. // Load a document.
  77. using (DocX document = DocX.Create("CustomProperty_Add.docx"))
  78. {
  79. Assert.IsTrue(document.CustomProperties.Count == 0);
  80. document.AddCustomProperty(new CustomProperty("fname", "cathal"));
  81. Assert.IsTrue(document.CustomProperties.Count == 1);
  82. Assert.IsTrue(document.CustomProperties.ContainsKey("fname"));
  83. Assert.IsTrue((String)document.CustomProperties["fname"].Value == "cathal");
  84. document.AddCustomProperty(new CustomProperty("age", 24));
  85. Assert.IsTrue(document.CustomProperties.Count == 2);
  86. Assert.IsTrue(document.CustomProperties.ContainsKey("age"));
  87. Assert.IsTrue((int)document.CustomProperties["age"].Value == 24);
  88. document.AddCustomProperty(new CustomProperty("male", true));
  89. Assert.IsTrue(document.CustomProperties.Count == 3);
  90. Assert.IsTrue(document.CustomProperties.ContainsKey("male"));
  91. Assert.IsTrue((bool)document.CustomProperties["male"].Value == true);
  92. document.AddCustomProperty(new CustomProperty("newyear2012", new DateTime(2012, 1, 1)));
  93. Assert.IsTrue(document.CustomProperties.Count == 4);
  94. Assert.IsTrue(document.CustomProperties.ContainsKey("newyear2012"));
  95. Assert.IsTrue((DateTime)document.CustomProperties["newyear2012"].Value == new DateTime(2012, 1, 1));
  96. document.AddCustomProperty(new CustomProperty("fav_num", 3.141592));
  97. Assert.IsTrue(document.CustomProperties.Count == 5);
  98. Assert.IsTrue(document.CustomProperties.ContainsKey("fav_num"));
  99. Assert.IsTrue((double)document.CustomProperties["fav_num"].Value == 3.141592);
  100. }
  101. }
  102. [TestMethod]
  103. public void Test_EverybodyHasAHome_Loaded()
  104. {
  105. // Load a document.
  106. using (DocX document = DocX.Load(directory_documents + "EverybodyHasAHome.docx"))
  107. {
  108. // Main document tests.
  109. string document_xml_file = document.mainPart.Uri.OriginalString;
  110. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  111. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  112. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  113. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  114. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  115. // header first
  116. Header header_first = document.Headers.first;
  117. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  118. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  119. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  120. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  121. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  122. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  123. // header odd
  124. Header header_odd = document.Headers.odd;
  125. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  126. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  127. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  128. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  129. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  130. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  131. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  132. // header even
  133. Header header_even = document.Headers.even;
  134. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  135. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  136. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  137. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  138. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  139. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  140. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  141. // footer first
  142. Footer footer_first = document.Footers.first;
  143. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  144. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  145. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  146. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  147. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  148. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  149. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  150. // footer odd
  151. Footer footer_odd = document.Footers.odd;
  152. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  153. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  154. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  155. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  156. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  157. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  158. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  159. // footer even
  160. Footer footer_even = document.Footers.even;
  161. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  162. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  163. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  164. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  165. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  166. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  167. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  168. }
  169. }
  170. [TestMethod]
  171. public void Test_Insert_Picture_ParagraphBeforeSelf()
  172. {
  173. // Create test document.
  174. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  175. {
  176. // Add an Image to this document.
  177. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  178. // Create a Picture from this Image.
  179. Picture pic = img.CreatePicture();
  180. Assert.IsNotNull(pic);
  181. // Main document.
  182. Paragraph p0 = document.InsertParagraph("Hello");
  183. Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
  184. p1.InsertPicture(pic, 3);
  185. // Save this document.
  186. document.Save();
  187. }
  188. }
  189. [TestMethod]
  190. public void Test_Insert_Picture_ParagraphAfterSelf()
  191. {
  192. // Create test document.
  193. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  194. {
  195. // Add an Image to this document.
  196. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  197. // Create a Picture from this Image.
  198. Picture pic = img.CreatePicture();
  199. Assert.IsNotNull(pic);
  200. // Main document.
  201. Paragraph p0 = document.InsertParagraph("Hello");
  202. Paragraph p1 = p0.InsertParagraphAfterSelf("again");
  203. p1.InsertPicture(pic, 3);
  204. // Save this document.
  205. document.Save();
  206. }
  207. }
  208. [TestMethod]
  209. public void Test_EverybodyHasAHome_Created()
  210. {
  211. // Create a new document.
  212. using (DocX document = DocX.Create("Test.docx"))
  213. {
  214. // Create a Table.
  215. Table t = document.AddTable(3, 3);
  216. t.Design = TableDesign.TableGrid;
  217. // Insert a Paragraph and a Table into the main document.
  218. document.InsertParagraph();
  219. document.InsertTable(t);
  220. // Insert a Paragraph and a Table into every Header.
  221. document.AddHeaders();
  222. document.Headers.odd.InsertParagraph();
  223. document.Headers.odd.InsertTable(t);
  224. document.Headers.even.InsertParagraph();
  225. document.Headers.even.InsertTable(t);
  226. document.Headers.first.InsertParagraph();
  227. document.Headers.first.InsertTable(t);
  228. // Insert a Paragraph and a Table into every Footer.
  229. document.AddFooters();
  230. document.Footers.odd.InsertParagraph();
  231. document.Footers.odd.InsertTable(t);
  232. document.Footers.even.InsertParagraph();
  233. document.Footers.even.InsertTable(t);
  234. document.Footers.first.InsertParagraph();
  235. document.Footers.first.InsertTable(t);
  236. // Main document tests.
  237. string document_xml_file = document.mainPart.Uri.OriginalString;
  238. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  239. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  240. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  241. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  242. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  243. // header first
  244. Header header_first = document.Headers.first;
  245. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  246. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  247. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  248. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  249. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  250. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  251. // header odd
  252. Header header_odd = document.Headers.odd;
  253. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  254. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  255. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  256. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  257. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  258. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  259. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  260. // header even
  261. Header header_even = document.Headers.even;
  262. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  263. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  264. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  265. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  266. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  267. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  268. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  269. // footer first
  270. Footer footer_first = document.Footers.first;
  271. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  272. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  273. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  274. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  275. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  276. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  277. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  278. // footer odd
  279. Footer footer_odd = document.Footers.odd;
  280. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  281. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  282. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  283. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  284. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  285. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  286. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  287. // footer even
  288. Footer footer_even = document.Footers.even;
  289. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  290. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  291. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  292. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  293. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  294. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  295. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  296. }
  297. }
  298. [TestMethod]
  299. public void Test_Document_AddImage_FromDisk()
  300. {
  301. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  302. {
  303. // Add a png to into this document
  304. Novacode.Image png = document.AddImage(directory_documents + "purple.png");
  305. Assert.IsTrue(document.Images.Count == 1);
  306. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
  307. // Add a tiff into to this document
  308. Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
  309. Assert.IsTrue(document.Images.Count == 2);
  310. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
  311. // Add a gif into to this document
  312. Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
  313. Assert.IsTrue(document.Images.Count == 3);
  314. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
  315. // Add a jpg into to this document
  316. Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
  317. Assert.IsTrue(document.Images.Count == 4);
  318. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
  319. // Add a bitmap to this document
  320. Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
  321. Assert.IsTrue(document.Images.Count == 5);
  322. // Word does not allow bmp make sure it was inserted as a png.
  323. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
  324. }
  325. }
  326. [TestMethod]
  327. public void Test_Document_AddImage_FromStream()
  328. {
  329. using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
  330. {
  331. // DocX will always insert Images that come from Streams as jpeg.
  332. // Add a png to into this document
  333. Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
  334. Assert.IsTrue(document.Images.Count == 1);
  335. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
  336. // Add a tiff into to this document
  337. Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
  338. Assert.IsTrue(document.Images.Count == 2);
  339. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
  340. // Add a gif into to this document
  341. Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
  342. Assert.IsTrue(document.Images.Count == 3);
  343. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
  344. // Add a jpg into to this document
  345. Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
  346. Assert.IsTrue(document.Images.Count == 4);
  347. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
  348. // Add a bitmap to this document
  349. Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
  350. Assert.IsTrue(document.Images.Count == 5);
  351. // Word does not allow bmp make sure it was inserted as a png.
  352. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
  353. }
  354. }
  355. [TestMethod]
  356. public void Test_Tables()
  357. {
  358. using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
  359. {
  360. // There is only one Paragraph at the document level.
  361. Assert.IsTrue(document.Paragraphs.Count() == 13);
  362. // There is only one Table in this document.
  363. Assert.IsTrue(document.Tables.Count() == 1);
  364. // Extract the only Table.
  365. Table t0 = document.Tables[0];
  366. // This table has 12 Paragraphs.
  367. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  368. }
  369. }
  370. [TestMethod]
  371. public void Test_Images()
  372. {
  373. using (DocX document = DocX.Load(directory_documents + "Images.docx"))
  374. {
  375. // Extract Images from Document.
  376. List<Novacode.Image> document_images = document.Images;
  377. // Make sure there are 3 Images in this document.
  378. Assert.IsTrue(document_images.Count() == 3);
  379. // Extract the headers from this document.
  380. Headers headers = document.Headers;
  381. Header header_first = headers.first;
  382. Header header_odd = headers.odd;
  383. Header header_even = headers.even;
  384. #region Header_First
  385. // Extract Images from the first Header.
  386. List<Novacode.Image> header_first_images = header_first.Images;
  387. // Make sure there is 1 Image in the first header.
  388. Assert.IsTrue(header_first_images.Count() == 1);
  389. #endregion
  390. #region Header_Odd
  391. // Extract Images from the odd Header.
  392. List<Novacode.Image> header_odd_images = header_odd.Images;
  393. // Make sure there is 1 Image in the first header.
  394. Assert.IsTrue(header_odd_images.Count() == 1);
  395. #endregion
  396. #region Header_Even
  397. // Extract Images from the odd Header.
  398. List<Novacode.Image> header_even_images = header_even.Images;
  399. // Make sure there is 1 Image in the first header.
  400. Assert.IsTrue(header_even_images.Count() == 1);
  401. #endregion
  402. }
  403. }
  404. [TestMethod]
  405. public void Test_Insert_Picture()
  406. {
  407. // Load test document.
  408. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  409. {
  410. // Add Headers and Footers into this document.
  411. document.AddHeaders();
  412. document.AddFooters();
  413. document.DifferentFirstPage = true;
  414. document.DifferentOddAndEvenPages = true;
  415. // Add an Image to this document.
  416. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  417. // Create a Picture from this Image.
  418. Picture pic = img.CreatePicture();
  419. // Main document.
  420. Paragraph p0 = document.InsertParagraph("Hello");
  421. p0.InsertPicture(pic, 3);
  422. // Header first.
  423. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  424. p1.InsertPicture(pic, 2);
  425. // Header odd.
  426. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  427. p2.InsertPicture(pic, 2);
  428. // Header even.
  429. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  430. p3.InsertPicture(pic, 2);
  431. // Footer first.
  432. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  433. p4.InsertPicture(pic, 2);
  434. // Footer odd.
  435. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  436. p5.InsertPicture(pic, 2);
  437. // Footer even.
  438. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  439. p6.InsertPicture(pic, 2);
  440. // Save this document.
  441. document.Save();
  442. }
  443. }
  444. [TestMethod]
  445. public void Test_Insert_Hyperlink()
  446. {
  447. // Load test document.
  448. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  449. {
  450. // Add Headers and Footers into this document.
  451. document.AddHeaders();
  452. document.AddFooters();
  453. document.DifferentFirstPage = true;
  454. document.DifferentOddAndEvenPages = true;
  455. // Add a Hyperlink into this document.
  456. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  457. // Main document.
  458. Paragraph p0 = document.InsertParagraph("Hello");
  459. p0.InsertHyperlink(h, 3);
  460. // Header first.
  461. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  462. p1.InsertHyperlink(h, 3);
  463. // Header odd.
  464. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  465. p2.InsertHyperlink(h, 3);
  466. // Header even.
  467. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  468. p3.InsertHyperlink(h, 3);
  469. // Footer first.
  470. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  471. p4.InsertHyperlink(h, 3);
  472. // Footer odd.
  473. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  474. p5.InsertHyperlink(h, 3);
  475. // Footer even.
  476. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  477. p6.InsertHyperlink(h, 3);
  478. // Save this document.
  479. document.Save();
  480. }
  481. }
  482. [TestMethod]
  483. public void Test_Get_Set_Hyperlink()
  484. {
  485. // Load test document.
  486. using (DocX document = DocX.Load(directory_documents + "Hyperlinks.docx"))
  487. {
  488. // Hyperlinks in the document.
  489. Assert.IsTrue(document.Hyperlinks.Count == 3);
  490. Assert.IsTrue(document.Hyperlinks[0].Text == "page1");
  491. Assert.IsTrue(document.Hyperlinks[0].Uri.AbsoluteUri == "http://www.page1.com/");
  492. Assert.IsTrue(document.Hyperlinks[1].Text == "page2");
  493. Assert.IsTrue(document.Hyperlinks[1].Uri.AbsoluteUri == "http://www.page2.com/");
  494. Assert.IsTrue(document.Hyperlinks[2].Text == "page3");
  495. Assert.IsTrue(document.Hyperlinks[2].Uri.AbsoluteUri == "http://www.page3.com/");
  496. // Change the Hyperlinks and check that it has in fact changed.
  497. document.Hyperlinks[0].Text = "somethingnew";
  498. document.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  499. Assert.IsTrue(document.Hyperlinks[0].Text == "somethingnew");
  500. Assert.IsTrue(document.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  501. document.Hyperlinks[1].Text = "somethingnew";
  502. document.Hyperlinks[1].Uri = new Uri("http://www.google.com/");
  503. Assert.IsTrue(document.Hyperlinks[1].Text == "somethingnew");
  504. Assert.IsTrue(document.Hyperlinks[1].Uri.AbsoluteUri == "http://www.google.com/");
  505. document.Hyperlinks[2].Text = "somethingnew";
  506. document.Hyperlinks[2].Uri = new Uri("http://www.google.com/");
  507. Assert.IsTrue(document.Hyperlinks[2].Text == "somethingnew");
  508. Assert.IsTrue(document.Hyperlinks[2].Uri.AbsoluteUri == "http://www.google.com/");
  509. Assert.IsTrue(document.Headers.first.Hyperlinks.Count == 1);
  510. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Text == "header-first");
  511. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-first.com/");
  512. // Change the Hyperlinks and check that it has in fact changed.
  513. document.Headers.first.Hyperlinks[0].Text = "somethingnew";
  514. document.Headers.first.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  515. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Text == "somethingnew");
  516. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  517. Assert.IsTrue(document.Headers.odd.Hyperlinks.Count == 1);
  518. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Text == "header-odd");
  519. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-odd.com/");
  520. // Change the Hyperlinks and check that it has in fact changed.
  521. document.Headers.odd.Hyperlinks[0].Text = "somethingnew";
  522. document.Headers.odd.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  523. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Text == "somethingnew");
  524. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  525. Assert.IsTrue(document.Headers.even.Hyperlinks.Count == 1);
  526. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Text == "header-even");
  527. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-even.com/");
  528. // Change the Hyperlinks and check that it has in fact changed.
  529. document.Headers.even.Hyperlinks[0].Text = "somethingnew";
  530. document.Headers.even.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  531. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Text == "somethingnew");
  532. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  533. Assert.IsTrue(document.Footers.first.Hyperlinks.Count == 1);
  534. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Text == "footer-first");
  535. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-first.com/");
  536. // Change the Hyperlinks and check that it has in fact changed.
  537. document.Footers.first.Hyperlinks[0].Text = "somethingnew";
  538. document.Footers.first.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  539. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Text == "somethingnew");
  540. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  541. Assert.IsTrue(document.Footers.odd.Hyperlinks.Count == 1);
  542. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Text == "footer-odd");
  543. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-odd.com/");
  544. // Change the Hyperlinks and check that it has in fact changed.
  545. document.Footers.odd.Hyperlinks[0].Text = "somethingnew";
  546. document.Footers.odd.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  547. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Text == "somethingnew");
  548. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  549. Assert.IsTrue(document.Footers.even.Hyperlinks.Count == 1);
  550. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Text == "footer-even");
  551. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-even.com/");
  552. // Change the Hyperlinks and check that it has in fact changed.
  553. document.Footers.even.Hyperlinks[0].Text = "somethingnew";
  554. document.Footers.even.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  555. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Text == "somethingnew");
  556. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  557. }
  558. }
  559. [TestMethod]
  560. public void Test_Append_Hyperlink()
  561. {
  562. // Load test document.
  563. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  564. {
  565. // Add Headers and Footers into this document.
  566. document.AddHeaders();
  567. document.AddFooters();
  568. document.DifferentFirstPage = true;
  569. document.DifferentOddAndEvenPages = true;
  570. // Add a Hyperlink to this document.
  571. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  572. // Main document.
  573. Paragraph p0 = document.InsertParagraph("----");
  574. p0.AppendHyperlink(h);
  575. Assert.IsTrue(p0.Text == "----google");
  576. // Header first.
  577. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  578. p1.AppendHyperlink(h);
  579. Assert.IsTrue(p1.Text == "----google");
  580. // Header odd.
  581. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  582. p2.AppendHyperlink(h);
  583. Assert.IsTrue(p2.Text == "----google");
  584. // Header even.
  585. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  586. p3.AppendHyperlink(h);
  587. Assert.IsTrue(p3.Text == "----google");
  588. // Footer first.
  589. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  590. p4.AppendHyperlink(h);
  591. Assert.IsTrue(p4.Text == "----google");
  592. // Footer odd.
  593. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  594. p5.AppendHyperlink(h);
  595. Assert.IsTrue(p5.Text == "----google");
  596. // Footer even.
  597. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  598. p6.AppendHyperlink(h);
  599. Assert.IsTrue(p6.Text == "----google");
  600. // Save the document.
  601. document.Save();
  602. }
  603. }
  604. [TestMethod]
  605. public void Test_Append_Picture()
  606. {
  607. // Create test document.
  608. using (DocX document = DocX.Create(directory_documents + "Test.docx"))
  609. {
  610. // Add Headers and Footers into this document.
  611. document.AddHeaders();
  612. document.AddFooters();
  613. document.DifferentFirstPage = true;
  614. document.DifferentOddAndEvenPages = true;
  615. // Add an Image to this document.
  616. Novacode.Image img = document.AddImage(directory_documents + "purple.png");
  617. // Create a Picture from this Image.
  618. Picture pic = img.CreatePicture();
  619. // Main document.
  620. Paragraph p0 = document.InsertParagraph();
  621. p0.AppendPicture(pic);
  622. // Header first.
  623. Paragraph p1 = document.Headers.first.InsertParagraph();
  624. p1.AppendPicture(pic);
  625. // Header odd.
  626. Paragraph p2 = document.Headers.odd.InsertParagraph();
  627. p2.AppendPicture(pic);
  628. // Header even.
  629. Paragraph p3 = document.Headers.even.InsertParagraph();
  630. p3.AppendPicture(pic);
  631. // Footer first.
  632. Paragraph p4 = document.Footers.first.InsertParagraph();
  633. p4.AppendPicture(pic);
  634. // Footer odd.
  635. Paragraph p5 = document.Footers.odd.InsertParagraph();
  636. p5.AppendPicture(pic);
  637. // Footer even.
  638. Paragraph p6 = document.Footers.even.InsertParagraph();
  639. p6.AppendPicture(pic);
  640. // Save the document.
  641. document.Save();
  642. }
  643. }
  644. [TestMethod]
  645. public void Test_Move_Picture_Load()
  646. {
  647. // Load test document.
  648. using (DocX document = DocX.Load(directory_documents + "MovePicture.docx"))
  649. {
  650. // Extract the first Picture from the first Paragraph.
  651. Picture picture = document.Paragraphs.First().Pictures.First();
  652. // Move it into the first Header.
  653. Header header_first = document.Headers.first;
  654. header_first.Paragraphs.First().AppendPicture(picture);
  655. // Move it into the even Header.
  656. Header header_even = document.Headers.even;
  657. header_even.Paragraphs.First().AppendPicture(picture);
  658. // Move it into the odd Header.
  659. Header header_odd = document.Headers.odd;
  660. header_odd.Paragraphs.First().AppendPicture(picture);
  661. // Move it into the first Footer.
  662. Footer footer_first = document.Footers.first;
  663. footer_first.Paragraphs.First().AppendPicture(picture);
  664. // Move it into the even Footer.
  665. Footer footer_even = document.Footers.even;
  666. footer_even.Paragraphs.First().AppendPicture(picture);
  667. // Move it into the odd Footer.
  668. Footer footer_odd = document.Footers.odd;
  669. footer_odd.Paragraphs.First().AppendPicture(picture);
  670. // Save this as MovedPicture.docx
  671. document.SaveAs(directory_documents + "MovedPicture.docx");
  672. }
  673. }
  674. [TestMethod]
  675. public void Test_Paragraph_InsertHyperlink()
  676. {
  677. // Create a new document
  678. using (DocX document = DocX.Create("Test.docx"))
  679. {
  680. // Add a Hyperlink to this document.
  681. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  682. // Simple
  683. Paragraph p1 = document.InsertParagraph("AC");
  684. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  685. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  686. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  687. // Difficult
  688. Paragraph p2 = document.InsertParagraph("\tA\tC\t");
  689. p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
  690. p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
  691. p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
  692. // Contrived
  693. // Add a contrived Hyperlink to this document.
  694. Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
  695. Paragraph p3 = document.InsertParagraph("\tA\tC\t");
  696. p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
  697. p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
  698. p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
  699. }
  700. }
  701. [TestMethod]
  702. public void Test_Paragraph_RemoveHyperlink()
  703. {
  704. // Create a new document
  705. using (DocX document = DocX.Create("Test.docx"))
  706. {
  707. // Add a Hyperlink to this document.
  708. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  709. // Simple
  710. Paragraph p1 = document.InsertParagraph("AC");
  711. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  712. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  713. p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
  714. // Try and remove a Hyperlink using a negative index.
  715. // This should throw an exception.
  716. try
  717. {
  718. p1.RemoveHyperlink(-1);
  719. Assert.Fail();
  720. }
  721. catch (ArgumentException) { }
  722. catch (Exception) { Assert.Fail(); }
  723. // Try and remove a Hyperlink at an index greater than the last.
  724. // This should throw an exception.
  725. try
  726. {
  727. p1.RemoveHyperlink(3);
  728. Assert.Fail();
  729. }
  730. catch (ArgumentException) { }
  731. catch (Exception) { Assert.Fail(); }
  732. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  733. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  734. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  735. }
  736. }
  737. [TestMethod]
  738. public void Test_Paragraph_ReplaceText()
  739. {
  740. // Create a new document
  741. using (DocX document = DocX.Create("Test.docx"))
  742. {
  743. // Simple
  744. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  745. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  746. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  747. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  748. // Try and replace text that dosen't exist in the Paragraph.
  749. string old = p1.Text;
  750. p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
  751. // Difficult
  752. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  753. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  754. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  755. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  756. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  757. }
  758. }
  759. [TestMethod]
  760. public void Test_Paragraph_RemoveText()
  761. {
  762. // Create a new document
  763. using (DocX document = DocX.Create("Test.docx"))
  764. {
  765. // Simple
  766. //<p>
  767. // <r><t>HellWorld</t></r>
  768. //</p>
  769. Paragraph p1 = document.InsertParagraph("HelloWorld");
  770. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  771. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  772. p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
  773. // Try and remove text at an index greater than the last.
  774. // This should throw an exception.
  775. try
  776. {
  777. p1.RemoveText(p1.Text.Length, 1);
  778. Assert.Fail();
  779. }
  780. catch (ArgumentOutOfRangeException) { }
  781. catch (Exception) { Assert.Fail(); }
  782. // Try and remove text at a negative index.
  783. // This should throw an exception.
  784. try
  785. {
  786. p1.RemoveText(-1, 1);
  787. Assert.Fail();
  788. }
  789. catch (ArgumentOutOfRangeException) { }
  790. catch (Exception) { Assert.Fail(); }
  791. // Difficult
  792. //<p>
  793. // <r><t>A</t></r>
  794. // <r><t>B</t></r>
  795. // <r><t>C</t></r>
  796. //</p>
  797. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  798. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  799. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  800. p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
  801. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  802. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  803. // Contrived 1
  804. //<p>
  805. // <r>
  806. // <t>A</t>
  807. // <t>B</t>
  808. // <t>C</t>
  809. // </r>
  810. //</p>
  811. Paragraph p3 = document.InsertParagraph("");
  812. p3.Xml = XElement.Parse
  813. (
  814. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  815. <w:pPr />
  816. <w:r>
  817. <w:rPr />
  818. <w:t>A</w:t>
  819. <w:t>B</w:t>
  820. <w:t>C</w:t>
  821. </w:r>
  822. </w:p>"
  823. );
  824. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  825. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  826. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  827. // Contrived 2
  828. //<p>
  829. // <r>
  830. // <t>A</t>
  831. // <t>B</t>
  832. // <t>C</t>
  833. // </r>
  834. //</p>
  835. Paragraph p4 = document.InsertParagraph("");
  836. p4.Xml = XElement.Parse
  837. (
  838. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  839. <w:pPr />
  840. <w:r>
  841. <w:rPr />
  842. <tab />
  843. <w:t>A</w:t>
  844. <tab />
  845. </w:r>
  846. <w:r>
  847. <w:rPr />
  848. <tab />
  849. <w:t>B</w:t>
  850. <tab />
  851. </w:r>
  852. </w:p>"
  853. );
  854. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  855. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  856. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  857. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  858. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  859. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  860. }
  861. }
  862. [TestMethod]
  863. public void Test_Paragraph_InsertText()
  864. {
  865. // Create a new document
  866. using (DocX document = DocX.Create("Test.docx"))
  867. {
  868. // Simple
  869. //<p>
  870. // <r><t>HelloWorld</t></r>
  871. //</p>
  872. Paragraph p1 = document.InsertParagraph("HelloWorld");
  873. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  874. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  875. p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  876. // Try and insert text at an index greater than the last + 1.
  877. // This should throw an exception.
  878. try
  879. {
  880. p1.InsertText(p1.Text.Length + 1, "-");
  881. Assert.Fail();
  882. }
  883. catch (ArgumentOutOfRangeException) { }
  884. catch (Exception) { Assert.Fail(); }
  885. // Try and insert text at a negative index.
  886. // This should throw an exception.
  887. try
  888. {
  889. p1.InsertText(-1, "-");
  890. Assert.Fail();
  891. }
  892. catch (ArgumentOutOfRangeException) { }
  893. catch (Exception) { Assert.Fail(); }
  894. // Difficult
  895. //<p>
  896. // <r><t>A</t></r>
  897. // <r><t>B</t></r>
  898. // <r><t>C</t></r>
  899. //</p>
  900. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  901. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  902. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  903. p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  904. p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  905. // Contrived 1
  906. //<p>
  907. // <r>
  908. // <t>A</t>
  909. // <t>B</t>
  910. // <t>C</t>
  911. // </r>
  912. //</p>
  913. Paragraph p3 = document.InsertParagraph("");
  914. p3.Xml = XElement.Parse
  915. (
  916. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  917. <w:pPr />
  918. <w:r>
  919. <w:rPr />
  920. <w:t>A</w:t>
  921. <w:t>B</w:t>
  922. <w:t>C</w:t>
  923. </w:r>
  924. </w:p>"
  925. );
  926. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  927. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  928. p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  929. p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  930. // Contrived 2
  931. //<p>
  932. // <r>
  933. // <t>A</t>
  934. // <t>B</t>
  935. // <t>C</t>
  936. // </r>
  937. //</p>
  938. Paragraph p4 = document.InsertParagraph("");
  939. p4.Xml = XElement.Parse
  940. (
  941. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  942. <w:pPr />
  943. <w:r>
  944. <w:rPr />
  945. <w:t>A</w:t>
  946. <w:t>B</w:t>
  947. <w:t>C</w:t>
  948. </w:r>
  949. </w:p>"
  950. );
  951. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  952. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  953. p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  954. p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  955. }
  956. }
  957. [TestMethod]
  958. public void Test_Document_Paragraphs()
  959. {
  960. // Load the document 'Paragraphs.docx'
  961. using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
  962. {
  963. // Extract the Paragraphs from this document.
  964. List<Paragraph> paragraphs = document.Paragraphs;
  965. // There should be 3 Paragraphs in this document.
  966. Assert.IsTrue(paragraphs.Count() == 3);
  967. // Extract the 3 Paragraphs.
  968. Paragraph p1 = paragraphs[0];
  969. Paragraph p2 = paragraphs[1];
  970. Paragraph p3 = paragraphs[2];
  971. // Extract their Text properties.
  972. string p1_text = p1.Text;
  973. string p2_text = p2.Text;
  974. string p3_text = p3.Text;
  975. // Test their Text properties against absolutes.
  976. Assert.IsTrue(p1_text == "Paragraph 1");
  977. Assert.IsTrue(p2_text == "Paragraph 2");
  978. Assert.IsTrue(p3_text == "Paragraph 3");
  979. // Its important that each Paragraph knows the PackagePart it belongs to.
  980. document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
  981. // Test the saving of the document.
  982. document.SaveAs(file_temp);
  983. }
  984. // Delete the tempory file.
  985. File.Delete(file_temp);
  986. }
  987. [TestMethod]
  988. public void Test_Table_mainPart_bug9526()
  989. {
  990. using (DocX document = DocX.Create("test.docx"))
  991. {
  992. Hyperlink h = document.AddHyperlink("follow me", new Uri("http://www.google.com"));
  993. Table t = document.AddTable(2, 3);
  994. int cc = t.ColumnCount;
  995. Paragraph p = t.Rows[0].Cells[0].Paragraphs[0];
  996. p.AppendHyperlink(h);
  997. }
  998. }
  999. [TestMethod]
  1000. public void Test_Table_InsertRowAndColumn()
  1001. {
  1002. // Create a table
  1003. using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
  1004. {
  1005. // Add a Table to a document.
  1006. Table t = document.AddTable(2, 2);
  1007. t.Design = TableDesign.TableGrid;
  1008. t.Rows[0].Cells[0].Paragraphs[0].InsertText("X");
  1009. t.Rows[0].Cells[1].Paragraphs[0].InsertText("X");
  1010. t.Rows[1].Cells[0].Paragraphs[0].InsertText("X");
  1011. t.Rows[1].Cells[1].Paragraphs[0].InsertText("X");
  1012. // Insert the Table into the main section of the document.
  1013. Table t1 = document.InsertTable(t);
  1014. // ... and add a column and a row
  1015. t1.InsertRow(1);
  1016. t1.InsertColumn(1);
  1017. // Save the document.
  1018. document.Save();
  1019. }
  1020. // Check table
  1021. using (DocX document = DocX.Load(directory_documents + "Tables2.docx"))
  1022. {
  1023. // Get a table from a document
  1024. Table t = document.Tables[0];
  1025. // Check that the table is equal this:
  1026. // X - X
  1027. // - - -
  1028. // X - X
  1029. Assert.AreEqual("X", t.Rows[0].Cells[0].Paragraphs[0].Text);
  1030. Assert.AreEqual("X", t.Rows[2].Cells[0].Paragraphs[0].Text);
  1031. Assert.AreEqual("X", t.Rows[0].Cells[2].Paragraphs[0].Text);
  1032. Assert.AreEqual("X", t.Rows[2].Cells[2].Paragraphs[0].Text);
  1033. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[0].Paragraphs[0].Text));
  1034. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[1].Paragraphs[0].Text));
  1035. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[2].Paragraphs[0].Text));
  1036. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[0].Cells[1].Paragraphs[0].Text));
  1037. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[2].Cells[1].Paragraphs[0].Text));
  1038. }
  1039. }
  1040. [TestMethod]
  1041. public void Test_Document_ApplyTemplate()
  1042. {
  1043. using (MemoryStream documentStream = new MemoryStream())
  1044. {
  1045. using (DocX document = DocX.Create(documentStream))
  1046. {
  1047. document.ApplyTemplate(directory_documents + "Template.dotx");
  1048. document.Save();
  1049. Header firstHeader = document.Headers.first;
  1050. Header oddHeader = document.Headers.odd;
  1051. Header evenHeader = document.Headers.even;
  1052. Footer firstFooter = document.Footers.first;
  1053. Footer oddFooter = document.Footers.odd;
  1054. Footer evenFooter = document.Footers.even;
  1055. Assert.IsTrue(firstHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1056. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  1057. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1058. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  1059. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1060. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  1061. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1062. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  1063. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1064. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  1065. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1066. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  1067. Paragraph firstParagraph = document.Paragraphs[0];
  1068. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }