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

DocXUnitTests.cs 137KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. using Novacode;
  10. using NUnit.Framework;
  11. using DocXFormatting = Novacode.Formatting;
  12. using WindowsColor = System.Drawing.Color;
  13. namespace UnitTests
  14. {
  15. /// <summary>
  16. /// Summary description for DocXUnitTest
  17. /// </summary>
  18. [TestFixture]
  19. public class DocXUnitTests
  20. {
  21. private readonly string _directoryDocuments;
  22. private readonly string _directoryWithFiles;
  23. private static readonly Border BlankBorder = new Border(BorderStyle.Tcbs_none, 0, 0, WindowsColor.White);
  24. const string package_part_document = "/word/document.xml";
  25. public DocXUnitTests()
  26. {
  27. _directoryDocuments = Path.Combine(Path.GetTempPath(), "DocXTests", "documents");
  28. Setup(_directoryDocuments); // prepare temp documents directory
  29. _directoryWithFiles = TestHelper.DirectoryWithFiles;
  30. }
  31. private static void Setup(string path)
  32. {
  33. if (!Directory.Exists(path))
  34. {
  35. Directory.CreateDirectory(path);
  36. }
  37. }
  38. [Test]
  39. public void LargeTable()
  40. {
  41. using (var output = File.Open(Path.Combine(_directoryWithFiles, "LargeTable.docx"), FileMode.Create))
  42. {
  43. using (var doc = DocX.Create(output))
  44. {
  45. var tbl = doc.InsertTable(1, 18);
  46. var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight;
  47. var colWidth = wholeWidth / tbl.ColumnCount;
  48. var colWidths = new int[tbl.ColumnCount];
  49. tbl.AutoFit = AutoFit.Contents;
  50. var r = tbl.Rows[0];
  51. var cx = 0;
  52. foreach (var cell in r.Cells)
  53. {
  54. cell.Paragraphs.First().Append("Col " + cx);
  55. cell.MarginBottom = 0;
  56. cell.MarginLeft = 0;
  57. cell.MarginRight = 0;
  58. cell.MarginTop = 0;
  59. cx++;
  60. }
  61. tbl.SetBorder(TableBorderType.Bottom, BlankBorder);
  62. tbl.SetBorder(TableBorderType.Left, BlankBorder);
  63. tbl.SetBorder(TableBorderType.Right, BlankBorder);
  64. tbl.SetBorder(TableBorderType.Top, BlankBorder);
  65. tbl.SetBorder(TableBorderType.InsideV, BlankBorder);
  66. tbl.SetBorder(TableBorderType.InsideH, BlankBorder);
  67. doc.Save();
  68. }
  69. }
  70. }
  71. [Test]
  72. public void TableWithSpecifiedWidths()
  73. {
  74. using (var output = File.Open(Path.Combine(_directoryWithFiles, "TableSpecifiedWidths.docx"), FileMode.Create))
  75. {
  76. using (var doc = DocX.Create(output))
  77. {
  78. var widths = new float[] { 200f, 100f, 300f };
  79. var tbl = doc.InsertTable(1, widths.Length);
  80. tbl.SetWidths(widths);
  81. var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight;
  82. tbl.AutoFit = AutoFit.Contents;
  83. var r = tbl.Rows[0];
  84. var cx = 0;
  85. foreach (var cell in r.Cells)
  86. {
  87. cell.Paragraphs.First().Append("Col " + cx);
  88. cell.MarginBottom = 0;
  89. cell.MarginLeft = 0;
  90. cell.MarginRight = 0;
  91. cell.MarginTop = 0;
  92. cx++;
  93. }
  94. //add new rows
  95. for (var x = 0; x < 5; x++)
  96. {
  97. r = tbl.InsertRow();
  98. cx = 0;
  99. foreach (var cell in r.Cells)
  100. {
  101. cell.Paragraphs.First().Append("Col " + cx);
  102. cell.MarginBottom = 0;
  103. cell.MarginLeft = 0;
  104. cell.MarginRight = 0;
  105. cell.MarginTop = 0;
  106. cx++;
  107. }
  108. }
  109. tbl.SetBorder(TableBorderType.Bottom, BlankBorder);
  110. tbl.SetBorder(TableBorderType.Left, BlankBorder);
  111. tbl.SetBorder(TableBorderType.Right, BlankBorder);
  112. tbl.SetBorder(TableBorderType.Top, BlankBorder);
  113. tbl.SetBorder(TableBorderType.InsideV, BlankBorder);
  114. tbl.SetBorder(TableBorderType.InsideH, BlankBorder);
  115. doc.Save();
  116. }
  117. }
  118. }
  119. public string ReplaceFunc(string findStr)
  120. {
  121. var testPatterns = new Dictionary<string, string>
  122. {
  123. {"COURT NAME","Fred Frump"},
  124. {"Case Number","cr-md-2011-1234567"}
  125. };
  126. if (testPatterns.ContainsKey(findStr))
  127. {
  128. return testPatterns[findStr];
  129. }
  130. return findStr;
  131. }
  132. [Test]
  133. public void RegexTest()
  134. {
  135. var findPattern = "<(.*?)>";
  136. var sample = "<Match This> text";
  137. var matchCollection = Regex.Matches(sample, findPattern, RegexOptions.IgnoreCase);
  138. }
  139. [Test]
  140. public void Test_Pattern_Replacement()
  141. {
  142. var testPatterns = new Dictionary<string, string>
  143. {
  144. {"COURT NAME","Fred Frump"},
  145. {"Case Number","cr-md-2011-1234567"}
  146. };
  147. using (DocX replaceDoc = DocX.Load(Path.Combine(_directoryWithFiles, "ReplaceTests.docx")))
  148. {
  149. foreach (var t in replaceDoc.Tables)
  150. { // each table has 1 row and 3 columns
  151. Assert.IsTrue(t.Rows[0].Cells.Count == 3);
  152. Assert.IsTrue(t.ColumnCount == 3);
  153. Assert.IsTrue(t.Rows.Count == 1);
  154. Assert.IsTrue(t.RowCount == 1);
  155. }
  156. // Make sure the origional strings are in the document.
  157. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
  158. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
  159. // There are only two patterns, even though each pattern is used more than once
  160. Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
  161. // Make sure the new strings are not in the document.
  162. Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
  163. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
  164. // Do the replacing
  165. foreach (var p in testPatterns)
  166. {
  167. replaceDoc.ReplaceText("<(.*?)>", ReplaceFunc, false, RegexOptions.IgnoreCase);
  168. //replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
  169. }
  170. // Make sure the origional string are no longer in the document.
  171. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
  172. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
  173. // Make sure the new strings are now in the document.
  174. Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
  175. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
  176. // Make sure the replacement worked.
  177. 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");
  178. }
  179. }
  180. [Test]
  181. public void Test_CustomProperty_Add()
  182. {
  183. // Load a document.
  184. using (DocX document = DocX.Create("CustomProperty_Add.docx"))
  185. {
  186. Assert.IsTrue(document.CustomProperties.Count == 0);
  187. document.AddCustomProperty(new CustomProperty("fname", "cathal"));
  188. Assert.IsTrue(document.CustomProperties.Count == 1);
  189. Assert.IsTrue(document.CustomProperties.ContainsKey("fname"));
  190. Assert.IsTrue((string)document.CustomProperties["fname"].Value == "cathal");
  191. document.AddCustomProperty(new CustomProperty("age", 24));
  192. Assert.IsTrue(document.CustomProperties.Count == 2);
  193. Assert.IsTrue(document.CustomProperties.ContainsKey("age"));
  194. Assert.IsTrue((int)document.CustomProperties["age"].Value == 24);
  195. document.AddCustomProperty(new CustomProperty("male", true));
  196. Assert.IsTrue(document.CustomProperties.Count == 3);
  197. Assert.IsTrue(document.CustomProperties.ContainsKey("male"));
  198. Assert.IsTrue((bool)document.CustomProperties["male"].Value);
  199. document.AddCustomProperty(new CustomProperty("newyear2012", new DateTime(2012, 1, 1)));
  200. Assert.IsTrue(document.CustomProperties.Count == 4);
  201. Assert.IsTrue(document.CustomProperties.ContainsKey("newyear2012"));
  202. Assert.IsTrue((DateTime)document.CustomProperties["newyear2012"].Value == new DateTime(2012, 1, 1));
  203. document.AddCustomProperty(new CustomProperty("fav_num", 3.141592));
  204. Assert.IsTrue(document.CustomProperties.Count == 5);
  205. Assert.IsTrue(document.CustomProperties.ContainsKey("fav_num"));
  206. Assert.IsTrue((double)document.CustomProperties["fav_num"].Value == 3.141592);
  207. }
  208. }
  209. [Test]
  210. public void Test_EverybodyHasAHome_Loaded()
  211. {
  212. // Load a document.
  213. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "EverybodyHasAHome.docx")))
  214. {
  215. // Main document tests.
  216. string document_xml_file = document.mainPart.Uri.OriginalString;
  217. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  218. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  219. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  220. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  221. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  222. // header first
  223. Header header_first = document.Headers.first;
  224. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  225. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  226. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  227. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  228. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  229. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  230. // header odd
  231. Header header_odd = document.Headers.odd;
  232. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  233. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  234. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  235. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  236. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  237. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  238. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  239. // header even
  240. Header header_even = document.Headers.even;
  241. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  242. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  243. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  244. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  245. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  246. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  247. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  248. // footer first
  249. Footer footer_first = document.Footers.first;
  250. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  251. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  252. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  253. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  254. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  255. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  256. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  257. // footer odd
  258. Footer footer_odd = document.Footers.odd;
  259. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  260. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  261. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  262. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  263. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  264. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  265. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  266. // footer even
  267. Footer footer_even = document.Footers.even;
  268. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  269. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  270. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  271. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  272. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  273. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  274. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  275. }
  276. }
  277. [Test]
  278. public void Test_Insert_Picture_ParagraphBeforeSelf()
  279. {
  280. // Create test document.
  281. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx")))
  282. {
  283. // Add an Image to this document.
  284. Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png"));
  285. // Create a Picture from this Image.
  286. Picture pic = img.CreatePicture();
  287. Assert.IsNotNull(pic);
  288. // Main document.
  289. Paragraph p0 = document.InsertParagraph("Hello");
  290. Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
  291. p1.InsertPicture(pic, 3);
  292. // Save this document.
  293. document.Save();
  294. }
  295. }
  296. [Test]
  297. public void Test_Insert_Picture_ParagraphAfterSelf()
  298. {
  299. // Create test document.
  300. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx")))
  301. {
  302. // Add an Image to this document.
  303. Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png"));
  304. // Create a Picture from this Image.
  305. Picture pic = img.CreatePicture();
  306. Assert.IsNotNull(pic);
  307. // Main document.
  308. Paragraph p0 = document.InsertParagraph("Hello");
  309. Paragraph p1 = p0.InsertParagraphAfterSelf("again");
  310. p1.InsertPicture(pic, 3);
  311. // Save this document.
  312. document.Save();
  313. }
  314. }
  315. [Test]
  316. public void Test_EverybodyHasAHome_Created()
  317. {
  318. // Create a new document.
  319. using (DocX document = DocX.Create("Test.docx"))
  320. {
  321. // Create a Table.
  322. Table t = document.AddTable(3, 3);
  323. t.Design = TableDesign.TableGrid;
  324. // Insert a Paragraph and a Table into the main document.
  325. document.InsertParagraph();
  326. document.InsertTable(t);
  327. // Insert a Paragraph and a Table into every Header.
  328. document.AddHeaders();
  329. document.Headers.odd.InsertParagraph();
  330. document.Headers.odd.InsertTable(t);
  331. document.Headers.even.InsertParagraph();
  332. document.Headers.even.InsertTable(t);
  333. document.Headers.first.InsertParagraph();
  334. document.Headers.first.InsertTable(t);
  335. // Insert a Paragraph and a Table into every Footer.
  336. document.AddFooters();
  337. document.Footers.odd.InsertParagraph();
  338. document.Footers.odd.InsertTable(t);
  339. document.Footers.even.InsertParagraph();
  340. document.Footers.even.InsertTable(t);
  341. document.Footers.first.InsertParagraph();
  342. document.Footers.first.InsertTable(t);
  343. // Main document tests.
  344. string document_xml_file = document.mainPart.Uri.OriginalString;
  345. Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  346. Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  347. Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  348. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  349. Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
  350. // header first
  351. Header header_first = document.Headers.first;
  352. string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
  353. Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  354. Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  355. Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  356. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  357. Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
  358. // header odd
  359. Header header_odd = document.Headers.odd;
  360. string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
  361. Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  362. Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  363. Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  364. Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  365. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  366. Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
  367. // header even
  368. Header header_even = document.Headers.even;
  369. string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
  370. Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  371. Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  372. Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  373. Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  374. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  375. Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
  376. // footer first
  377. Footer footer_first = document.Footers.first;
  378. string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
  379. Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  380. Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  381. Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  382. Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  383. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  384. Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
  385. // footer odd
  386. Footer footer_odd = document.Footers.odd;
  387. string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
  388. Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  389. Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  390. Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  391. Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  392. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  393. Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
  394. // footer even
  395. Footer footer_even = document.Footers.even;
  396. string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
  397. Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  398. Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  399. Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  400. Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  401. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  402. Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
  403. }
  404. }
  405. [Test]
  406. public void Test_Document_AddImage_FromDisk()
  407. {
  408. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "test_add_images.docx")))
  409. {
  410. // Add a png to into this document
  411. Image png = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png"));
  412. Assert.IsTrue(document.Images.Count == 1);
  413. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
  414. // Add a tiff into to this document
  415. Image tif = document.AddImage(Path.Combine(_directoryWithFiles, "yellow.tif"));
  416. Assert.IsTrue(document.Images.Count == 2);
  417. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
  418. // Add a gif into to this document
  419. Image gif = document.AddImage(Path.Combine(_directoryWithFiles, "orange.gif"));
  420. Assert.IsTrue(document.Images.Count == 3);
  421. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
  422. // Add a jpg into to this document
  423. Image jpg = document.AddImage(Path.Combine(_directoryWithFiles, "green.jpg"));
  424. Assert.IsTrue(document.Images.Count == 4);
  425. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
  426. // Add a bitmap to this document
  427. Image bitmap = document.AddImage(Path.Combine(_directoryWithFiles, "red.bmp"));
  428. Assert.IsTrue(document.Images.Count == 5);
  429. // Word does not allow bmp make sure it was inserted as a png.
  430. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
  431. }
  432. }
  433. [Test]
  434. public void Test_Document_AddImage_FromStream()
  435. {
  436. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "test_add_images.docx")))
  437. {
  438. // DocX will always insert Images that come from Streams as jpeg.
  439. // Add a png to into this document
  440. Image png = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "purple.png"), FileMode.Open));
  441. Assert.IsTrue(document.Images.Count == 1);
  442. Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
  443. // Add a tiff into to this document
  444. Image tif = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "yellow.tif"), FileMode.Open));
  445. Assert.IsTrue(document.Images.Count == 2);
  446. Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
  447. // Add a gif into to this document
  448. Image gif = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "orange.gif"), FileMode.Open));
  449. Assert.IsTrue(document.Images.Count == 3);
  450. Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
  451. // Add a jpg into to this document
  452. Image jpg = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "green.jpg"), FileMode.Open));
  453. Assert.IsTrue(document.Images.Count == 4);
  454. Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
  455. // Add a bitmap to this document
  456. Image bitmap = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "red.bmp"), FileMode.Open));
  457. Assert.IsTrue(document.Images.Count == 5);
  458. // Word does not allow bmp make sure it was inserted as a png.
  459. Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
  460. }
  461. }
  462. [Test]
  463. public void Test_Tables()
  464. {
  465. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Tables.docx")))
  466. {
  467. // There is only one Paragraph at the document level.
  468. Assert.IsTrue(document.Paragraphs.Count() == 13);
  469. // There is only one Table in this document.
  470. Assert.IsTrue(document.Tables.Count() == 1);
  471. // Extract the only Table.
  472. Table t0 = document.Tables[0];
  473. // This table has 12 Paragraphs.
  474. Assert.IsTrue(t0.Paragraphs.Count() == 12);
  475. }
  476. }
  477. [Test]
  478. public void Test_Images()
  479. {
  480. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Images.docx")))
  481. {
  482. // Extract Images from Document.
  483. List<Image> document_images = document.Images;
  484. // Make sure there are 3 Images in this document.
  485. Assert.IsTrue(document_images.Count() == 3);
  486. // Extract the headers from this document.
  487. Headers headers = document.Headers;
  488. Header header_first = headers.first;
  489. Header header_odd = headers.odd;
  490. Header header_even = headers.even;
  491. #region Header_First
  492. // Extract Images from the first Header.
  493. List<Image> header_first_images = header_first.Images;
  494. // Make sure there is 1 Image in the first header.
  495. Assert.IsTrue(header_first_images.Count() == 1);
  496. #endregion
  497. #region Header_Odd
  498. // Extract Images from the odd Header.
  499. List<Image> header_odd_images = header_odd.Images;
  500. // Make sure there is 1 Image in the first header.
  501. Assert.IsTrue(header_odd_images.Count() == 1);
  502. #endregion
  503. #region Header_Even
  504. // Extract Images from the odd Header.
  505. List<Image> header_even_images = header_even.Images;
  506. // Make sure there is 1 Image in the first header.
  507. Assert.IsTrue(header_even_images.Count() == 1);
  508. #endregion
  509. }
  510. }
  511. [Test]
  512. public void Test_Insert_Picture()
  513. {
  514. // Load test document.
  515. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx")))
  516. {
  517. // Add Headers and Footers into this document.
  518. document.AddHeaders();
  519. document.AddFooters();
  520. document.DifferentFirstPage = true;
  521. document.DifferentOddAndEvenPages = true;
  522. // Add an Image to this document.
  523. Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png"));
  524. // Create a Picture from this Image.
  525. Picture pic = img.CreatePicture();
  526. // Main document.
  527. Paragraph p0 = document.InsertParagraph("Hello");
  528. p0.InsertPicture(pic, 3);
  529. // Header first.
  530. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  531. p1.InsertPicture(pic, 2);
  532. // Header odd.
  533. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  534. p2.InsertPicture(pic, 2);
  535. // Header even.
  536. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  537. p3.InsertPicture(pic, 2);
  538. // Footer first.
  539. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  540. p4.InsertPicture(pic, 2);
  541. // Footer odd.
  542. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  543. p5.InsertPicture(pic, 2);
  544. // Footer even.
  545. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  546. p6.InsertPicture(pic, 2);
  547. // Save this document.
  548. document.Save();
  549. }
  550. }
  551. [Test]
  552. public void Test_Insert_Hyperlink()
  553. {
  554. // Load test document.
  555. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx")))
  556. {
  557. // Add Headers and Footers into this document.
  558. document.AddHeaders();
  559. document.AddFooters();
  560. document.DifferentFirstPage = true;
  561. document.DifferentOddAndEvenPages = true;
  562. // Add a Hyperlink into this document.
  563. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  564. // Main document.
  565. Paragraph p0 = document.InsertParagraph("Hello");
  566. p0.InsertHyperlink(h, 3);
  567. // Header first.
  568. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  569. p1.InsertHyperlink(h, 3);
  570. // Header odd.
  571. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  572. p2.InsertHyperlink(h, 3);
  573. // Header even.
  574. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  575. p3.InsertHyperlink(h, 3);
  576. // Footer first.
  577. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  578. p4.InsertHyperlink(h, 3);
  579. // Footer odd.
  580. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  581. p5.InsertHyperlink(h, 3);
  582. // Footer even.
  583. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  584. p6.InsertHyperlink(h, 3);
  585. // Save this document.
  586. document.Save();
  587. }
  588. }
  589. [Test]
  590. public void Test_Get_Set_Hyperlink()
  591. {
  592. // Load test document.
  593. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Hyperlinks.docx")))
  594. {
  595. // Hyperlinks in the document.
  596. Assert.IsTrue(document.Hyperlinks.Count == 3);
  597. Assert.IsTrue(document.Hyperlinks[0].Text == "page1");
  598. Assert.IsTrue(document.Hyperlinks[0].Uri.AbsoluteUri == "http://www.page1.com/");
  599. Assert.IsTrue(document.Hyperlinks[1].Text == "page2");
  600. Assert.IsTrue(document.Hyperlinks[1].Uri.AbsoluteUri == "http://www.page2.com/");
  601. Assert.IsTrue(document.Hyperlinks[2].Text == "page3");
  602. Assert.IsTrue(document.Hyperlinks[2].Uri.AbsoluteUri == "http://www.page3.com/");
  603. // Change the Hyperlinks and check that it has in fact changed.
  604. document.Hyperlinks[0].Text = "somethingnew";
  605. document.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  606. Assert.IsTrue(document.Hyperlinks[0].Text == "somethingnew");
  607. Assert.IsTrue(document.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  608. document.Hyperlinks[1].Text = "somethingnew";
  609. document.Hyperlinks[1].Uri = new Uri("http://www.google.com/");
  610. Assert.IsTrue(document.Hyperlinks[1].Text == "somethingnew");
  611. Assert.IsTrue(document.Hyperlinks[1].Uri.AbsoluteUri == "http://www.google.com/");
  612. document.Hyperlinks[2].Text = "somethingnew";
  613. document.Hyperlinks[2].Uri = new Uri("http://www.google.com/");
  614. Assert.IsTrue(document.Hyperlinks[2].Text == "somethingnew");
  615. Assert.IsTrue(document.Hyperlinks[2].Uri.AbsoluteUri == "http://www.google.com/");
  616. Assert.IsTrue(document.Headers.first.Hyperlinks.Count == 1);
  617. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Text == "header-first");
  618. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-first.com/");
  619. // Change the Hyperlinks and check that it has in fact changed.
  620. document.Headers.first.Hyperlinks[0].Text = "somethingnew";
  621. document.Headers.first.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  622. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Text == "somethingnew");
  623. Assert.IsTrue(document.Headers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  624. Assert.IsTrue(document.Headers.odd.Hyperlinks.Count == 1);
  625. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Text == "header-odd");
  626. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-odd.com/");
  627. // Change the Hyperlinks and check that it has in fact changed.
  628. document.Headers.odd.Hyperlinks[0].Text = "somethingnew";
  629. document.Headers.odd.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  630. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Text == "somethingnew");
  631. Assert.IsTrue(document.Headers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  632. Assert.IsTrue(document.Headers.even.Hyperlinks.Count == 1);
  633. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Text == "header-even");
  634. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.header-even.com/");
  635. // Change the Hyperlinks and check that it has in fact changed.
  636. document.Headers.even.Hyperlinks[0].Text = "somethingnew";
  637. document.Headers.even.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  638. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Text == "somethingnew");
  639. Assert.IsTrue(document.Headers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  640. Assert.IsTrue(document.Footers.first.Hyperlinks.Count == 1);
  641. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Text == "footer-first");
  642. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-first.com/");
  643. // Change the Hyperlinks and check that it has in fact changed.
  644. document.Footers.first.Hyperlinks[0].Text = "somethingnew";
  645. document.Footers.first.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  646. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Text == "somethingnew");
  647. Assert.IsTrue(document.Footers.first.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  648. Assert.IsTrue(document.Footers.odd.Hyperlinks.Count == 1);
  649. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Text == "footer-odd");
  650. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-odd.com/");
  651. // Change the Hyperlinks and check that it has in fact changed.
  652. document.Footers.odd.Hyperlinks[0].Text = "somethingnew";
  653. document.Footers.odd.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  654. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Text == "somethingnew");
  655. Assert.IsTrue(document.Footers.odd.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  656. Assert.IsTrue(document.Footers.even.Hyperlinks.Count == 1);
  657. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Text == "footer-even");
  658. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.footer-even.com/");
  659. // Change the Hyperlinks and check that it has in fact changed.
  660. document.Footers.even.Hyperlinks[0].Text = "somethingnew";
  661. document.Footers.even.Hyperlinks[0].Uri = new Uri("http://www.google.com/");
  662. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Text == "somethingnew");
  663. Assert.IsTrue(document.Footers.even.Hyperlinks[0].Uri.AbsoluteUri == "http://www.google.com/");
  664. }
  665. }
  666. [Test]
  667. public void Test_Append_Hyperlink()
  668. {
  669. // Load test document.
  670. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx")))
  671. {
  672. // Add Headers and Footers into this document.
  673. document.AddHeaders();
  674. document.AddFooters();
  675. document.DifferentFirstPage = true;
  676. document.DifferentOddAndEvenPages = true;
  677. // Add a Hyperlink to this document.
  678. Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
  679. // Main document.
  680. Paragraph p0 = document.InsertParagraph("----");
  681. p0.AppendHyperlink(h);
  682. Assert.IsTrue(p0.Text == "----google");
  683. // Header first.
  684. Paragraph p1 = document.Headers.first.InsertParagraph("----");
  685. p1.AppendHyperlink(h);
  686. Assert.IsTrue(p1.Text == "----google");
  687. // Header odd.
  688. Paragraph p2 = document.Headers.odd.InsertParagraph("----");
  689. p2.AppendHyperlink(h);
  690. Assert.IsTrue(p2.Text == "----google");
  691. // Header even.
  692. Paragraph p3 = document.Headers.even.InsertParagraph("----");
  693. p3.AppendHyperlink(h);
  694. Assert.IsTrue(p3.Text == "----google");
  695. // Footer first.
  696. Paragraph p4 = document.Footers.first.InsertParagraph("----");
  697. p4.AppendHyperlink(h);
  698. Assert.IsTrue(p4.Text == "----google");
  699. // Footer odd.
  700. Paragraph p5 = document.Footers.odd.InsertParagraph("----");
  701. p5.AppendHyperlink(h);
  702. Assert.IsTrue(p5.Text == "----google");
  703. // Footer even.
  704. Paragraph p6 = document.Footers.even.InsertParagraph("----");
  705. p6.AppendHyperlink(h);
  706. Assert.IsTrue(p6.Text == "----google");
  707. // Save the document.
  708. document.Save();
  709. }
  710. }
  711. [Test]
  712. public void Test_Append_Picture()
  713. {
  714. // Create test document.
  715. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx")))
  716. {
  717. // Add Headers and Footers into this document.
  718. document.AddHeaders();
  719. document.AddFooters();
  720. document.DifferentFirstPage = true;
  721. document.DifferentOddAndEvenPages = true;
  722. // Add an Image to this document.
  723. Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png"));
  724. // Create a Picture from this Image.
  725. Picture pic = img.CreatePicture();
  726. // Main document.
  727. Paragraph p0 = document.InsertParagraph();
  728. p0.AppendPicture(pic);
  729. // Header first.
  730. Paragraph p1 = document.Headers.first.InsertParagraph();
  731. p1.AppendPicture(pic);
  732. // Header odd.
  733. Paragraph p2 = document.Headers.odd.InsertParagraph();
  734. p2.AppendPicture(pic);
  735. // Header even.
  736. Paragraph p3 = document.Headers.even.InsertParagraph();
  737. p3.AppendPicture(pic);
  738. // Footer first.
  739. Paragraph p4 = document.Footers.first.InsertParagraph();
  740. p4.AppendPicture(pic);
  741. // Footer odd.
  742. Paragraph p5 = document.Footers.odd.InsertParagraph();
  743. p5.AppendPicture(pic);
  744. // Footer even.
  745. Paragraph p6 = document.Footers.even.InsertParagraph();
  746. p6.AppendPicture(pic);
  747. // Save the document.
  748. document.Save();
  749. }
  750. }
  751. [Test]
  752. public void Test_Move_Picture_Load()
  753. {
  754. // Load test document.
  755. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "MovePicture.docx")))
  756. {
  757. // Extract the first Picture from the first Paragraph.
  758. Picture picture = document.Paragraphs.First().Pictures.First();
  759. // Move it into the first Header.
  760. Header header_first = document.Headers.first;
  761. header_first.Paragraphs.First().AppendPicture(picture);
  762. // Move it into the even Header.
  763. Header header_even = document.Headers.even;
  764. header_even.Paragraphs.First().AppendPicture(picture);
  765. // Move it into the odd Header.
  766. Header header_odd = document.Headers.odd;
  767. header_odd.Paragraphs.First().AppendPicture(picture);
  768. // Move it into the first Footer.
  769. Footer footer_first = document.Footers.first;
  770. footer_first.Paragraphs.First().AppendPicture(picture);
  771. // Move it into the even Footer.
  772. Footer footer_even = document.Footers.even;
  773. footer_even.Paragraphs.First().AppendPicture(picture);
  774. // Move it into the odd Footer.
  775. Footer footer_odd = document.Footers.odd;
  776. footer_odd.Paragraphs.First().AppendPicture(picture);
  777. // Save this as MovedPicture.docx
  778. document.SaveAs(Path.Combine(_directoryDocuments, "MovedPicture.docx"));
  779. }
  780. }
  781. [Test]
  782. public void Test_Paragraph_InsertHyperlink()
  783. {
  784. // Create a new document
  785. using (DocX document = DocX.Create("Test.docx"))
  786. {
  787. // Add a Hyperlink to this document.
  788. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  789. // Simple
  790. Paragraph p1 = document.InsertParagraph("AC");
  791. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  792. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  793. p1.InsertHyperlink(h, p1.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p1.Text == "linkAlinkClink");
  794. // Difficult
  795. Paragraph p2 = document.InsertParagraph("\tA\tC\t");
  796. p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
  797. p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
  798. p2.InsertHyperlink(h, p2.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
  799. // Contrived
  800. // Add a contrived Hyperlink to this document.
  801. Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
  802. Paragraph p3 = document.InsertParagraph("\tA\tC\t");
  803. p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
  804. p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
  805. p3.InsertHyperlink(h2, p3.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
  806. }
  807. }
  808. [Test]
  809. public void Test_Paragraph_RemoveHyperlink()
  810. {
  811. // Create a new document
  812. using (DocX document = DocX.Create("Test.docx"))
  813. {
  814. // Add a Hyperlink to this document.
  815. Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
  816. // Simple
  817. Paragraph p1 = document.InsertParagraph("AC");
  818. p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
  819. p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
  820. p1.InsertHyperlink(h, p1.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p1.Text == "linkAlinkClink");
  821. // Try and remove a Hyperlink using a negative index.
  822. // This should throw an exception.
  823. try
  824. {
  825. p1.RemoveHyperlink(-1);
  826. Assert.Fail();
  827. }
  828. catch (ArgumentException) { }
  829. catch (Exception) { Assert.Fail(); }
  830. // Try and remove a Hyperlink at an index greater than the last.
  831. // This should throw an exception.
  832. try
  833. {
  834. p1.RemoveHyperlink(3);
  835. Assert.Fail();
  836. }
  837. catch (ArgumentException) { }
  838. catch (Exception) { Assert.Fail(); }
  839. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
  840. p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
  841. p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
  842. }
  843. }
  844. [Test]
  845. public void Test_Paragraph_ReplaceText()
  846. {
  847. // Create a new document
  848. using (DocX document = DocX.Create("Test.docx"))
  849. {
  850. // Simple
  851. Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  852. p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
  853. p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
  854. p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
  855. // Try and replace text that dosen't exist in the Paragraph.
  856. string old = p1.Text;
  857. p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
  858. // Difficult
  859. Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
  860. p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
  861. p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
  862. p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
  863. p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
  864. }
  865. }
  866. [Test]
  867. public void Test_Paragraph_ReplaceTextInGivenFormat()
  868. {
  869. // Load a document.
  870. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "VariousTextFormatting.docx")))
  871. {
  872. // Removing red text highlighted with yellow
  873. var formatting = new DocXFormatting();
  874. formatting.FontColor = WindowsColor.Blue;
  875. // IMPORTANT: default constructor of Formatting sets up language property - set it to NULL to be language independent
  876. var desiredFormat = new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(255, 0, 0), Highlight = Highlight.yellow };
  877. var replaced = string.Empty;
  878. foreach (var p in document.Paragraphs)
  879. {
  880. if (p.Text == "Text highlighted with yellow")
  881. {
  882. p.ReplaceText("Text highlighted with yellow", "New text highlighted with yellow", false, RegexOptions.None, null, desiredFormat, MatchFormattingOptions.ExactMatch);
  883. replaced += p.Text;
  884. }
  885. }
  886. Assert.AreEqual("New text highlighted with yellow", replaced);
  887. // Removing red text with no other formatting (ExactMatch)
  888. desiredFormat = new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(255, 0, 0) };
  889. var count = 0;
  890. foreach (var p in document.Paragraphs)
  891. {
  892. p.ReplaceText("Text", "Replaced text", false, RegexOptions.None, null, desiredFormat, MatchFormattingOptions.ExactMatch);
  893. if (p.Text.StartsWith("Replaced text"))
  894. {
  895. ++count;
  896. }
  897. }
  898. Assert.AreEqual(1, count);
  899. // Removing just red text with any other formatting (SubsetMatch)
  900. desiredFormat = new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(255, 0, 0) };
  901. count = 0;
  902. foreach (var p in document.Paragraphs)
  903. {
  904. p.ReplaceText("Text", "Replaced text", false, RegexOptions.None, null, desiredFormat);
  905. if (p.Text.StartsWith("Replaced text"))
  906. {
  907. ++count;
  908. }
  909. }
  910. Assert.AreEqual(2, count);
  911. }
  912. }
  913. [Test]
  914. public void Test_Paragraph_RemoveText()
  915. {
  916. // Create a new document
  917. using (DocX document = DocX.Create("Test.docx"))
  918. {
  919. // Simple
  920. //<p>
  921. // <r><t>HellWorld</t></r>
  922. //</p>
  923. Paragraph p1 = document.InsertParagraph("HelloWorld");
  924. p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
  925. p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
  926. p1.RemoveText(p1.Text.IndexOf("o", StringComparison.Ordinal), 1); Assert.IsTrue(p1.Text == "ellWorl");
  927. // Try and remove text at an index greater than the last.
  928. // This should throw an exception.
  929. try
  930. {
  931. p1.RemoveText(p1.Text.Length, 1);
  932. Assert.Fail();
  933. }
  934. catch (ArgumentOutOfRangeException) { }
  935. catch (Exception) { Assert.Fail(); }
  936. // Try and remove text at a negative index.
  937. // This should throw an exception.
  938. try
  939. {
  940. p1.RemoveText(-1, 1);
  941. Assert.Fail();
  942. }
  943. catch (ArgumentOutOfRangeException) { }
  944. catch (Exception) { Assert.Fail(); }
  945. // Difficult
  946. //<p>
  947. // <r><t>A</t></r>
  948. // <r><t>B</t></r>
  949. // <r><t>C</t></r>
  950. //</p>
  951. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  952. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
  953. p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
  954. p2.RemoveText(p2.Text.IndexOf("B", StringComparison.Ordinal), 1); Assert.IsTrue(p2.Text == "\t\t");
  955. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
  956. p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
  957. // Contrived 1
  958. //<p>
  959. // <r>
  960. // <t>A</t>
  961. // <t>B</t>
  962. // <t>C</t>
  963. // </r>
  964. //</p>
  965. Paragraph p3 = document.InsertParagraph("");
  966. p3.Xml = XElement.Parse
  967. (
  968. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  969. <w:pPr />
  970. <w:r>
  971. <w:rPr />
  972. <w:t>A</w:t>
  973. <w:t>B</w:t>
  974. <w:t>C</w:t>
  975. </w:r>
  976. </w:p>"
  977. );
  978. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
  979. p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
  980. p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
  981. // Contrived 2
  982. //<p>
  983. // <r>
  984. // <t>A</t>
  985. // <t>B</t>
  986. // <t>C</t>
  987. // </r>
  988. //</p>
  989. Paragraph p4 = document.InsertParagraph("");
  990. p4.Xml = XElement.Parse
  991. (
  992. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  993. <w:pPr />
  994. <w:r>
  995. <w:rPr />
  996. <tab />
  997. <w:t>A</w:t>
  998. <tab />
  999. </w:r>
  1000. <w:r>
  1001. <w:rPr />
  1002. <tab />
  1003. <w:t>B</w:t>
  1004. <tab />
  1005. </w:r>
  1006. </w:p>"
  1007. );
  1008. p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
  1009. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
  1010. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
  1011. p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
  1012. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
  1013. p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
  1014. }
  1015. }
  1016. [Test]
  1017. public void Test_Document_RemoveTextInGivenFormat()
  1018. {
  1019. // Load a document.
  1020. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "VariousTextFormatting.docx")))
  1021. {
  1022. var formatting = new DocXFormatting();
  1023. formatting.FontColor = WindowsColor.Blue;
  1024. // IMPORTANT: default constructor of Formatting sets up language property - set it to NULL to be language independent
  1025. formatting.Language = null;
  1026. var deletedCount = document.RemoveTextInGivenFormat(formatting);
  1027. Assert.AreEqual(2, deletedCount);
  1028. deletedCount = document.RemoveTextInGivenFormat(new DocXFormatting() { Highlight = Highlight.yellow, Language = null });
  1029. Assert.AreEqual(2, deletedCount);
  1030. deletedCount = document.RemoveTextInGivenFormat(new DocXFormatting() { Highlight = Highlight.blue, Language = null, FontColor = WindowsColor.FromArgb(0, 255, 0) });
  1031. Assert.AreEqual(1, deletedCount);
  1032. deletedCount = document.RemoveTextInGivenFormat(new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(123, 123, 123) }, MatchFormattingOptions.ExactMatch);
  1033. Assert.AreEqual(2, deletedCount);
  1034. }
  1035. }
  1036. [Test]
  1037. public void Test_Paragraph_InsertText()
  1038. {
  1039. // Create a new document
  1040. using (DocX document = DocX.Create("Test.docx"))
  1041. {
  1042. // Simple
  1043. //<p>
  1044. // <r><t>HelloWorld</t></r>
  1045. //</p>
  1046. Paragraph p1 = document.InsertParagraph("HelloWorld");
  1047. p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
  1048. p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
  1049. p1.InsertText(p1.Text.IndexOf("W", StringComparison.Ordinal), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
  1050. // Try and insert text at an index greater than the last + 1.
  1051. // This should throw an exception.
  1052. try
  1053. {
  1054. p1.InsertText(p1.Text.Length + 1, "-");
  1055. Assert.Fail();
  1056. }
  1057. catch (ArgumentOutOfRangeException) { }
  1058. catch (Exception) { Assert.Fail(); }
  1059. // Try and insert text at a negative index.
  1060. // This should throw an exception.
  1061. try
  1062. {
  1063. p1.InsertText(-1, "-");
  1064. Assert.Fail();
  1065. }
  1066. catch (ArgumentOutOfRangeException) { }
  1067. catch (Exception) { Assert.Fail(); }
  1068. // Difficult
  1069. //<p>
  1070. // <r><t>A</t></r>
  1071. // <r><t>B</t></r>
  1072. // <r><t>C</t></r>
  1073. //</p>
  1074. Paragraph p2 = document.InsertParagraph("A\tB\tC");
  1075. p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
  1076. p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
  1077. p2.InsertText(p2.Text.IndexOf("B", StringComparison.Ordinal), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
  1078. p2.InsertText(p2.Text.IndexOf("C", StringComparison.Ordinal), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
  1079. // Contrived 1
  1080. //<p>
  1081. // <r>
  1082. // <t>A</t>
  1083. // <t>B</t>
  1084. // <t>C</t>
  1085. // </r>
  1086. //</p>
  1087. Paragraph p3 = document.InsertParagraph("");
  1088. p3.Xml = XElement.Parse
  1089. (
  1090. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  1091. <w:pPr />
  1092. <w:r>
  1093. <w:rPr />
  1094. <w:t>A</w:t>
  1095. <w:t>B</w:t>
  1096. <w:t>C</w:t>
  1097. </w:r>
  1098. </w:p>"
  1099. );
  1100. p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
  1101. p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
  1102. p3.InsertText(p3.Text.IndexOf("B", StringComparison.Ordinal), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
  1103. p3.InsertText(p3.Text.IndexOf("C", StringComparison.Ordinal), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
  1104. // Contrived 2
  1105. //<p>
  1106. // <r>
  1107. // <t>A</t>
  1108. // <t>B</t>
  1109. // <t>C</t>
  1110. // </r>
  1111. //</p>
  1112. Paragraph p4 = document.InsertParagraph("");
  1113. p4.Xml = XElement.Parse
  1114. (
  1115. @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  1116. <w:pPr />
  1117. <w:r>
  1118. <w:rPr />
  1119. <w:t>A</w:t>
  1120. <w:t>B</w:t>
  1121. <w:t>C</w:t>
  1122. </w:r>
  1123. </w:p>"
  1124. );
  1125. p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
  1126. p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
  1127. p4.InsertText(p4.Text.IndexOf("B", StringComparison.Ordinal), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
  1128. p4.InsertText(p4.Text.IndexOf("C", StringComparison.Ordinal), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
  1129. }
  1130. }
  1131. [Test]
  1132. public void Test_Document_Paragraphs()
  1133. {
  1134. string temporaryFilePath = Path.Combine(_directoryDocuments, "temp.docx");
  1135. // Load the document 'Paragraphs.docx'
  1136. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Paragraphs.docx")))
  1137. {
  1138. // Extract the Paragraphs from this document.
  1139. ReadOnlyCollection<Paragraph> paragraphs = document.Paragraphs;
  1140. // There should be 3 Paragraphs in this document.
  1141. Assert.IsTrue(paragraphs.Count() == 3);
  1142. // Extract the 3 Paragraphs.
  1143. Paragraph p1 = paragraphs[0];
  1144. Paragraph p2 = paragraphs[1];
  1145. Paragraph p3 = paragraphs[2];
  1146. // Extract their Text properties.
  1147. string p1_text = p1.Text;
  1148. string p2_text = p2.Text;
  1149. string p3_text = p3.Text;
  1150. // Test their Text properties against absolutes.
  1151. Assert.IsTrue(p1_text == "Paragraph 1");
  1152. Assert.IsTrue(p2_text == "Paragraph 2");
  1153. Assert.IsTrue(p3_text == "Paragraph 3");
  1154. // Its important that each Paragraph knows the PackagePart it belongs to.
  1155. foreach (var paragraph in document.Paragraphs)
  1156. {
  1157. Assert.IsTrue(paragraph.PackagePart.Uri.ToString() == package_part_document);
  1158. }
  1159. // Test the saving of the document.
  1160. document.SaveAs(temporaryFilePath);
  1161. }
  1162. // Delete the tempory file.
  1163. File.Delete(temporaryFilePath);
  1164. }
  1165. [Test]
  1166. public void Test_Table_mainPart_bug9526()
  1167. {
  1168. using (DocX document = DocX.Create("test.docx"))
  1169. {
  1170. Hyperlink h = document.AddHyperlink("follow me", new Uri("http://www.google.com"));
  1171. Table t = document.AddTable(2, 3);
  1172. int cc = t.ColumnCount;
  1173. Paragraph p = t.Rows[0].Cells[0].Paragraphs[0];
  1174. p.AppendHyperlink(h);
  1175. }
  1176. }
  1177. [Test]
  1178. public void Test_Table_RemoveColumnWithMergedCells()
  1179. {
  1180. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1181. {
  1182. //Add A table
  1183. Table t = document.AddTable(2, 3);
  1184. t.Design = TableDesign.TableGrid;
  1185. Table t1 = document.InsertTable(t);
  1186. t1.Rows[0].MergeCells(1, 2);
  1187. t1.RemoveColumn();
  1188. document.Save();
  1189. }
  1190. }
  1191. [Test]
  1192. public void Test_Table_MergedRowCellsMergedWithColumnMergedCells()
  1193. {
  1194. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1195. {
  1196. //Add A table
  1197. Table t = document.AddTable(3, 3);
  1198. t.Design = TableDesign.TableGrid;
  1199. Table t1 = document.InsertTable(t);
  1200. t1.Rows[0].MergeCells(0, 1);
  1201. t1.Rows[1].MergeCells(0, 1);
  1202. t1.MergeCellsInColumn(0, 0, 1);
  1203. document.Save();
  1204. }
  1205. }
  1206. [Test]
  1207. public void Test_Table_RemoveRowWithMergedCells()
  1208. {
  1209. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1210. {
  1211. //Add A table
  1212. Table t = document.AddTable(3, 4);
  1213. t.Design = TableDesign.TableGrid;
  1214. Table t1 = document.InsertTable(t);
  1215. t1.Rows[0].MergeCells(1, 2);
  1216. t1.RemoveRow();
  1217. t1.MergeCellsInColumn(0, 0, 1);
  1218. t1.InsertRow();
  1219. t1.RemoveRow(1);
  1220. document.Save();
  1221. }
  1222. }
  1223. [Test]
  1224. public void Test_Table_InsertRow()
  1225. {
  1226. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1227. {
  1228. //Add A table
  1229. Table t = document.AddTable(2, 3);
  1230. t.Design = TableDesign.TableGrid;
  1231. Table t1 = document.InsertTable(t);
  1232. t1.MergeCellsInColumn(1, 0, 1);
  1233. t1.InsertRow();
  1234. t1.Rows[2].MergeCells(1, 3);
  1235. t1.InsertRow(3);
  1236. document.Save();
  1237. }
  1238. }
  1239. [Test]
  1240. public void Test_Table_AddColumnWithCellDeleted()
  1241. {
  1242. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1243. {
  1244. //Add A table
  1245. Table t = document.AddTable(2, 3);
  1246. t.Design = TableDesign.TableGrid;
  1247. Table t1 = document.InsertTable(t);
  1248. t1.DeleteAndShiftCellsLeft(0, 1);
  1249. // 4 columns
  1250. t1.InsertColumn();
  1251. t1.DeleteAndShiftCellsLeft(0, 1);
  1252. // 5 columns
  1253. t1.InsertColumn();
  1254. t1.DeleteAndShiftCellsLeft(0, 1);
  1255. document.Save();
  1256. }
  1257. }
  1258. [Test]
  1259. public void Test_Table_DeleteCellInRow()
  1260. {
  1261. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1262. {
  1263. //Add A table
  1264. Table t = document.AddTable(2, 2);
  1265. t.Design = TableDesign.TableGrid;
  1266. Table t1 = document.InsertTable(t);
  1267. t1.DeleteAndShiftCellsLeft(0, 1);
  1268. document.Save();
  1269. document.Save();
  1270. }
  1271. }
  1272. [Test]
  1273. public void Test_Table_InsertColumnWithMergedCells()
  1274. {
  1275. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables3.docx")))
  1276. {
  1277. //Add A table
  1278. Table t = document.AddTable(2, 2);
  1279. t.Design = TableDesign.TableGrid;
  1280. Table t1 = document.InsertTable(t);
  1281. t1.InsertColumn(2, true);
  1282. t1.InsertColumn(2, true);
  1283. t1.InsertColumn(2, true);
  1284. t1.InsertColumn(2, true);
  1285. t1.Rows[0].MergeCells(1, 4);
  1286. Assert.AreEqual(t1.Rows[1].Cells.Count, 6);
  1287. Assert.AreEqual(t1.ColumnCount, 6);
  1288. foreach (Row r in t1.Rows)
  1289. {
  1290. foreach (Cell c in r.Cells)
  1291. {
  1292. c.Paragraphs[0].InsertText("Hello");
  1293. }
  1294. }
  1295. t1.InsertColumn(6, false);
  1296. t1.InsertColumn();
  1297. t1.InsertColumn(3, true);
  1298. t1.InsertColumn(6, true);
  1299. t1.InsertColumn(6, true);
  1300. t1.InsertColumn(5, true);
  1301. document.Save();
  1302. }
  1303. }
  1304. [Test]
  1305. public void Test_Table_InsertRowAndColumn()
  1306. {
  1307. // Create a table
  1308. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Tables2.docx")))
  1309. {
  1310. // Add a Table to a document.
  1311. Table t = document.AddTable(2, 2);
  1312. t.Design = TableDesign.TableGrid;
  1313. t.Rows[0].Cells[0].Paragraphs[0].InsertText("X");
  1314. t.Rows[0].Cells[1].Paragraphs[0].InsertText("X");
  1315. t.Rows[1].Cells[0].Paragraphs[0].InsertText("X");
  1316. t.Rows[1].Cells[1].Paragraphs[0].InsertText("X");
  1317. // Insert the Table into the main section of the document.
  1318. Table t1 = document.InsertTable(t);
  1319. // ... and add a column and a row
  1320. t1.InsertRow(1);
  1321. t1.InsertColumn(1, true);
  1322. // Save the document.
  1323. document.Save();
  1324. }
  1325. // Check table
  1326. using (DocX document = DocX.Load(Path.Combine(_directoryDocuments, "Tables2.docx")))
  1327. {
  1328. // Get a table from a document
  1329. Table t = document.Tables[0];
  1330. // Check that the table is equal this:
  1331. // X - X
  1332. // - - -
  1333. // X - X
  1334. Assert.AreEqual("X", t.Rows[0].Cells[0].Paragraphs[0].Text);
  1335. Assert.AreEqual("X", t.Rows[2].Cells[0].Paragraphs[0].Text);
  1336. Assert.AreEqual("X", t.Rows[0].Cells[2].Paragraphs[0].Text);
  1337. Assert.AreEqual("X", t.Rows[2].Cells[2].Paragraphs[0].Text);
  1338. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[0].Paragraphs[0].Text));
  1339. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[1].Paragraphs[0].Text));
  1340. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[2].Paragraphs[0].Text));
  1341. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[0].Cells[1].Paragraphs[0].Text));
  1342. Assert.IsTrue(String.IsNullOrEmpty(t.Rows[2].Cells[1].Paragraphs[0].Text));
  1343. }
  1344. }
  1345. [Test]
  1346. public void Test_Table_SetTableDesignNone()
  1347. {
  1348. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "TablesDesign.docx")))
  1349. {
  1350. //Add A table
  1351. Table t = document.AddTable(2, 3);
  1352. Table t1 = document.InsertTable(t);
  1353. t1.Design = TableDesign.None;
  1354. // requires FIX for .None/.Custom set in a row
  1355. // t1.Design = TableDesign.None;
  1356. // t1.Design = TableDesign.Custom;
  1357. t1.Design = TableDesign.Custom;
  1358. t1.Design = TableDesign.ColorfulGrid;
  1359. t1.Design = TableDesign.ColorfulGrid;
  1360. t1.MergeCellsInColumn(1, 0, 1);
  1361. t1.InsertRow();
  1362. t1.Rows[2].MergeCells(1, 3);
  1363. t1.InsertRow(3);
  1364. document.Save();
  1365. }
  1366. }
  1367. [Test]
  1368. public void Test_Document_ApplyTemplate()
  1369. {
  1370. using (MemoryStream documentStream = new MemoryStream())
  1371. {
  1372. using (DocX document = DocX.Create(documentStream))
  1373. {
  1374. document.ApplyTemplate(Path.Combine(_directoryWithFiles, "Template.dotx"));
  1375. document.Save();
  1376. Header firstHeader = document.Headers.first;
  1377. Header oddHeader = document.Headers.odd;
  1378. Header evenHeader = document.Headers.even;
  1379. Footer firstFooter = document.Footers.first;
  1380. Footer oddFooter = document.Footers.odd;
  1381. Footer evenFooter = document.Footers.even;
  1382. Assert.IsTrue(firstHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1383. Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
  1384. Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1385. Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
  1386. Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
  1387. Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
  1388. Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1389. Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
  1390. Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1391. Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
  1392. Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
  1393. Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
  1394. Paragraph firstParagraph = document.Paragraphs[0];
  1395. Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
  1396. }
  1397. }
  1398. }
  1399. [Test]
  1400. public void When_opening_a_template_no_error_should_be_thrown()
  1401. {
  1402. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Template.dotx")))
  1403. {
  1404. Assert.IsTrue(document.Paragraphs.Count > 0);
  1405. }
  1406. }
  1407. [Test]
  1408. public void Saving_and_loading_a_template_should_work()
  1409. {
  1410. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "test template.dotx"), DocumentTypes.Template))
  1411. {
  1412. document.InsertParagraph("hello, this is a paragraph");
  1413. document.Save();
  1414. }
  1415. using (DocX document = DocX.Load(Path.Combine(_directoryDocuments, "test template.dotx")))
  1416. {
  1417. Assert.IsTrue(document.Paragraphs.Count > 0);
  1418. }
  1419. }
  1420. [Test]
  1421. public void Test_ParentContainer_When_Creating_Doc()
  1422. {
  1423. using (DocX document = DocX.Create("Test.docx"))
  1424. {
  1425. document.AddHeaders();
  1426. Paragraph p1 = document.Headers.first.InsertParagraph("Test");
  1427. Assert.IsTrue(p1.ParentContainer == ContainerType.Header);
  1428. }
  1429. }
  1430. [Test]
  1431. public void Test_Section_Count_When_Creating_Doc()
  1432. {
  1433. //This adds a section break - so insert paragraphs, and follow it up by a section break/paragraph
  1434. using (DocX document = DocX.Create("TestSectionCount.docx"))
  1435. {
  1436. document.InsertSection();
  1437. var sections = document.GetSections();
  1438. Assert.AreEqual(sections.Count(), 2);
  1439. }
  1440. }
  1441. [Test]
  1442. public void Test_Sections_And_Paragraphs_When_Creating_Doc()
  1443. {
  1444. //This adds a section break - so insert paragraphs, and follow it up by a section break/paragraph
  1445. using (DocX document = DocX.Create("TestSectionAndParagraph.docx"))
  1446. {
  1447. //Add 2 paras and a break
  1448. document.InsertParagraph("First Para");
  1449. document.InsertParagraph("Second Para");
  1450. document.InsertSection();
  1451. document.InsertParagraph("This is default para");
  1452. var sections = document.GetSections();
  1453. Assert.AreEqual(sections.Count(), 2);
  1454. }
  1455. }
  1456. [Test]
  1457. public void Test_ParentContainer_When_Reading_Doc()
  1458. {
  1459. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Tables.docx")))
  1460. {
  1461. ReadOnlyCollection<Paragraph> paragraphs = document.Paragraphs;
  1462. Paragraph p1 = paragraphs[0];
  1463. Assert.IsTrue(p1.ParentContainer == ContainerType.Cell);
  1464. }
  1465. }
  1466. [Test]
  1467. public void Test_Section_Count_When_Reading_Doc()
  1468. {
  1469. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_SectionsWithSectionBreaks.docx")))
  1470. {
  1471. var sections = document.GetSections();
  1472. Assert.AreEqual(sections.Count(), 4);
  1473. }
  1474. }
  1475. [Test]
  1476. public void Test_Section_Paragraph_Count_Match_When_Reading_Doc()
  1477. {
  1478. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_SectionsWithSectionBreaksMultiParagraph.docx")))
  1479. {
  1480. var sections = document.GetSections();
  1481. Assert.AreEqual(sections[0].SectionParagraphs.Count, 2);
  1482. Assert.AreEqual(sections[1].SectionParagraphs.Count, 1);
  1483. Assert.AreEqual(sections[2].SectionParagraphs.Count, 2);
  1484. Assert.AreEqual(sections[3].SectionParagraphs.Count, 1);
  1485. }
  1486. }
  1487. [Test]
  1488. public void Test_Section_Paragraph_Content_Match_When_Reading_Doc()
  1489. {
  1490. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_SectionsWithSectionBreaks.docx")))
  1491. {
  1492. var sections = document.GetSections();
  1493. Assert.IsTrue(sections[0].SectionParagraphs[0].Text.Contains("Section 1"));
  1494. Assert.IsTrue(sections[1].SectionParagraphs[0].Text.Contains("Section 2"));
  1495. Assert.IsTrue(sections[2].SectionParagraphs[0].Text.Contains("Section 3"));
  1496. Assert.IsTrue(sections[3].SectionParagraphs[0].Text.Contains("Section 4"));
  1497. }
  1498. }
  1499. [Test]
  1500. public void Test_Ordered_List_When_Reading_Doc()
  1501. {
  1502. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_OrderedList.docx")))
  1503. {
  1504. var sections = document.GetSections();
  1505. Assert.IsTrue(sections[0].SectionParagraphs[0].IsListItem);
  1506. Assert.IsTrue(sections[0].SectionParagraphs[1].IsListItem);
  1507. Assert.IsTrue(sections[0].SectionParagraphs[2].IsListItem);
  1508. Assert.AreEqual(sections[0].SectionParagraphs[0].ListItemType, ListItemType.Numbered);
  1509. Assert.AreEqual(sections[0].SectionParagraphs[1].ListItemType, ListItemType.Numbered);
  1510. Assert.AreEqual(sections[0].SectionParagraphs[2].ListItemType, ListItemType.Numbered);
  1511. }
  1512. }
  1513. [Test]
  1514. public void Test_Unordered_List_When_Reading_Doc()
  1515. {
  1516. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_UnorderedList.docx")))
  1517. {
  1518. var sections = document.GetSections();
  1519. Assert.IsTrue(sections[0].SectionParagraphs[0].IsListItem);
  1520. Assert.IsTrue(sections[0].SectionParagraphs[1].IsListItem);
  1521. Assert.IsTrue(sections[0].SectionParagraphs[2].IsListItem);
  1522. Assert.AreEqual(sections[0].SectionParagraphs[0].ListItemType, ListItemType.Bulleted);
  1523. Assert.AreEqual(sections[0].SectionParagraphs[1].ListItemType, ListItemType.Bulleted);
  1524. Assert.AreEqual(sections[0].SectionParagraphs[2].ListItemType, ListItemType.Bulleted);
  1525. }
  1526. }
  1527. [Test]
  1528. public void Test_Ordered_Unordered_Lists_When_Reading_Doc()
  1529. {
  1530. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_OrderedUnorderedLists.docx")))
  1531. {
  1532. var sections = document.GetSections();
  1533. Assert.IsTrue(sections[0].SectionParagraphs[0].IsListItem);
  1534. Assert.IsTrue(sections[0].SectionParagraphs[1].IsListItem);
  1535. Assert.IsTrue(sections[0].SectionParagraphs[2].IsListItem);
  1536. Assert.AreEqual(sections[0].SectionParagraphs[0].ListItemType, ListItemType.Numbered);
  1537. Assert.AreEqual(sections[0].SectionParagraphs[1].ListItemType, ListItemType.Numbered);
  1538. Assert.AreEqual(sections[0].SectionParagraphs[2].ListItemType, ListItemType.Numbered);
  1539. Assert.IsTrue(sections[0].SectionParagraphs[3].IsListItem);
  1540. Assert.IsTrue(sections[0].SectionParagraphs[4].IsListItem);
  1541. Assert.IsTrue(sections[0].SectionParagraphs[5].IsListItem);
  1542. Assert.AreEqual(sections[0].SectionParagraphs[3].ListItemType, ListItemType.Bulleted);
  1543. Assert.AreEqual(sections[0].SectionParagraphs[4].ListItemType, ListItemType.Bulleted);
  1544. Assert.AreEqual(sections[0].SectionParagraphs[5].ListItemType, ListItemType.Bulleted);
  1545. }
  1546. }
  1547. [Test]
  1548. public void WhenCreatingAnOrderedListTheListXmlShouldHaveNumberedListItemType()
  1549. {
  1550. using (DocX document = DocX.Create("TestListXmlNumbered.docx"))
  1551. {
  1552. const int level = 0;
  1553. XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  1554. var list = document.AddList("First Item");
  1555. document.InsertList(list);
  1556. var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr");
  1557. var numId = listNumPropNode.Descendants().First(s => s.Name.LocalName == "numId");
  1558. var abstractNum = list.GetAbstractNum(int.Parse(numId.GetAttribute(w + "val")));
  1559. var lvl = abstractNum.Descendants().First(d => d.Name.LocalName == "lvl" && d.GetAttribute(w + "ilvl").Equals(level.ToString()));
  1560. var numFormat = lvl.Descendants().First(d => d.Name.LocalName == "numFmt");
  1561. Assert.AreEqual(numFormat.GetAttribute(w + "val").ToLower(), "decimal");
  1562. }
  1563. }
  1564. [Test]
  1565. public void WhenCreatingAnUnOrderedListTheListXmlShouldHaveBulletListItemType()
  1566. {
  1567. using (DocX document = DocX.Create("TestListXmlBullet.docx"))
  1568. {
  1569. var list = document.AddList("First Item", 0, ListItemType.Bulleted);
  1570. document.InsertList(list);
  1571. var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr");
  1572. var numId = listNumPropNode.Descendants().First(s => s.Name.LocalName == "numId");
  1573. Assert.AreEqual(numId.Attribute(DocX.w + "val").Value, "1");
  1574. }
  1575. }
  1576. [Test]
  1577. public void WhenCreatingAListWithTextTheListXmlShouldHaveTheCorrectRunItemText()
  1578. {
  1579. using (DocX document = DocX.Create("TestListCreate.docx"))
  1580. {
  1581. const string listText = "RunText";
  1582. var list = document.AddList(listText, 0, ListItemType.Bulleted);
  1583. document.InsertList(list);
  1584. var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr");
  1585. var runTextNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "t");
  1586. Assert.IsNotNull(listNumPropNode);
  1587. Assert.AreEqual(list.Items.First().runs.First().Value, runTextNode.Value);
  1588. Assert.AreEqual(listText, runTextNode.Value);
  1589. }
  1590. }
  1591. [Test]
  1592. public void WhenCreatingAnOrderedListTheListShouldHaveNumberedListItemType()
  1593. {
  1594. using (DocX document = DocX.Create("TestListCreateOrderedList.docx"))
  1595. {
  1596. var list = document.AddList("First Item");
  1597. Assert.AreEqual(list.ListType, ListItemType.Numbered);
  1598. }
  1599. }
  1600. [Test]
  1601. public void WhenCreatingAnUnOrderedListTheListShouldHaveBulletListItemType()
  1602. {
  1603. using (DocX document = DocX.Create("TestListCreateUnorderedList.docx"))
  1604. {
  1605. var list = document.AddList("First Item", 0, ListItemType.Bulleted);
  1606. Assert.AreEqual(list.ListType, ListItemType.Bulleted);
  1607. }
  1608. }
  1609. [Test]
  1610. public void WhenCreatingAListWithTextTheListShouldHaveTheCorrectRunItemText()
  1611. {
  1612. using (DocX document = DocX.Create("TestListCreateRunText.docx"))
  1613. {
  1614. var list = document.AddList("RunText", 0, ListItemType.Bulleted);
  1615. document.InsertList(list);
  1616. Assert.AreEqual(list.Items.First().runs.First().Value, "RunText");
  1617. }
  1618. }
  1619. [Test]
  1620. public void WhenCreatingAListTheListStyleShouldExistOrBeCreated()
  1621. {
  1622. using (DocX document = DocX.Create("TestListStyle.docx"))
  1623. {
  1624. var style = document.AddStylesForList();
  1625. XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  1626. bool listStyleExists =
  1627. (
  1628. from s in style.Element(w + "styles").Elements()
  1629. let styleId = s.Attribute(XName.Get("styleId", w.NamespaceName))
  1630. where (styleId != null && styleId.Value == "ListParagraph")
  1631. select s
  1632. ).Any();
  1633. Assert.IsTrue(listStyleExists);
  1634. }
  1635. }
  1636. [Test]
  1637. public void ANewListItemShouldCreateAnAbstractNumberingEntry()
  1638. {
  1639. using (DocX document = DocX.Create("TestNumbering.docx"))
  1640. {
  1641. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1642. Assert.IsFalse(numbering.Any());
  1643. document.AddList("List Text");
  1644. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1645. Assert.IsTrue(numbering.Any());
  1646. }
  1647. }
  1648. [Test]
  1649. public void ANewListItemShouldCreateANewNumEntry()
  1650. {
  1651. using (DocX document = DocX.Create("TestNumEntry.docx"))
  1652. {
  1653. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1654. Assert.IsFalse(numbering.Any());
  1655. document.AddList("List Text");
  1656. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1657. Assert.IsTrue(numbering.Any());
  1658. }
  1659. }
  1660. [Test]
  1661. public void CreateNewNumberingNumIdShouldAddNumberingDataToTheDocument()
  1662. {
  1663. using (DocX document = DocX.Create("TestCreateNumbering.docx"))
  1664. {
  1665. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1666. Assert.IsFalse(numbering.Any());
  1667. var list = document.AddList("", 0, ListItemType.Bulleted);
  1668. document.InsertList(list);
  1669. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "num");
  1670. Assert.IsTrue(numbering.Any());
  1671. }
  1672. }
  1673. [Test]
  1674. public void CreateNewNumberingNumIdShouldAddNumberingAbstractDataToTheDocument()
  1675. {
  1676. using (DocX document = DocX.Create("TestCreateNumberingAbstract.docx"))
  1677. {
  1678. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1679. Assert.IsFalse(numbering.Any());
  1680. var list = document.AddList("", 0, ListItemType.Bulleted);
  1681. document.InsertList(list);
  1682. numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1683. Assert.IsTrue(numbering.Any());
  1684. }
  1685. }
  1686. [Test]
  1687. public void IfPreviousElementIsAListThenAddingANewListContinuesThePreviousList()
  1688. {
  1689. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "TestAddListToPreviousList.docx")))
  1690. {
  1691. var list = document.AddList("List Text");
  1692. document.AddListItem(list, "List Text2");
  1693. document.InsertList(list);
  1694. var lvlNodes = document.mainDoc.Descendants().Where(s => s.Name.LocalName == "ilvl").ToList();
  1695. var numIdNodes = document.mainDoc.Descendants().Where(s => s.Name.LocalName == "numId").ToList();
  1696. Assert.AreEqual(lvlNodes.Count(), 2);
  1697. Assert.AreEqual(numIdNodes.Count(), 2);
  1698. var prevLvlNode = lvlNodes[0];
  1699. var newLvlNode = lvlNodes[1];
  1700. Assert.AreEqual(prevLvlNode.Attribute(DocX.w + "val").Value, newLvlNode.Attribute(DocX.w + "val").Value);
  1701. var prevNumIdNode = numIdNodes[0];
  1702. var newNumIdNode = numIdNodes[1];
  1703. Assert.AreEqual(prevNumIdNode.Attribute(DocX.w + "val").Value, newNumIdNode.Attribute(DocX.w + "val").Value);
  1704. document.Save();
  1705. }
  1706. }
  1707. [Test]
  1708. public void WhenADocumentHasListsTheListPropertyReturnsTheCorrectNumberOfLists()
  1709. {
  1710. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "testdoc_OrderedUnorderedLists.docx")))
  1711. {
  1712. var lists = document.Lists;
  1713. Assert.AreEqual(lists.Count, 2);
  1714. }
  1715. }
  1716. [Test]
  1717. public void WhenADocumentIsCreatedWithAListItemThatHasASpecifiedStartNumber()
  1718. {
  1719. using (DocX document = DocX.Create("CreateListItemFromDifferentStartValue.docx"))
  1720. {
  1721. var list = document.AddList("Test", 0, ListItemType.Numbered, 5);
  1722. document.AddListItem(list, "NewElement");
  1723. var numbering = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1724. var level = numbering.Descendants().First(el => el.Name.LocalName == "lvl");
  1725. var start = level.Descendants().First(el => el.Name.LocalName == "start");
  1726. Assert.AreEqual(start.GetAttribute(DocX.w + "val"), 5.ToString());
  1727. }
  1728. }
  1729. [Test]
  1730. public void WhenANumberedAndBulletedListIsCreatedThereShouldBeTwoNumberingXmls()
  1731. {
  1732. using (DocX document = DocX.Create("NumberAndBulletListInOne.docx"))
  1733. {
  1734. var numberList = document.AddList("Test");
  1735. document.AddListItem(numberList, "Second Numbered Item");
  1736. var bulletedList = document.AddList("Bullet", 0, ListItemType.Bulleted);
  1737. document.AddListItem(bulletedList, "Second bullet item");
  1738. document.InsertList(numberList);
  1739. document.InsertList(bulletedList);
  1740. var abstractNums = document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum");
  1741. Assert.AreEqual(abstractNums.Count(), 2);
  1742. }
  1743. }
  1744. [Test]
  1745. public void WhenICreateAnEmptyListAndAddEntriesToIt()
  1746. {
  1747. using (DocX document = DocX.Create("CreateEmptyListAndAddItemsToIt.docx"))
  1748. {
  1749. var list = document.AddList();
  1750. Assert.AreEqual(list.Items.Count, 0);
  1751. document.AddListItem(list, "Test item 1.");
  1752. document.AddListItem(list, "Test item 2.");
  1753. Assert.AreEqual(list.Items.Count, 2);
  1754. }
  1755. }
  1756. [Test]
  1757. public void WhenICreateAHeaderItShouldHaveAStyle()
  1758. {
  1759. using (var document = DocX.Create(Path.Combine(_directoryDocuments, "CreateHeaderElement.docx")))
  1760. {
  1761. document.InsertParagraph("Header Text 1").StyleName = "Header1";
  1762. Assert.IsNotNull(document.styles.Root.Descendants().FirstOrDefault(d => d.GetAttribute(DocX.w + "styleId").ToLowerInvariant() == "heading1"));
  1763. document.Save();
  1764. }
  1765. }
  1766. [Test]
  1767. public void ParagraphAppendHyperLink_ParagraphIsListItem_ShouldNotThrow()
  1768. {
  1769. using (var document = DocX.Create(Path.Combine(_directoryDocuments, "HyperlinkList.docx")))
  1770. {
  1771. var list = document.AddList("Item 1");
  1772. document.AddListItem(list, "Item 2");
  1773. document.AddListItem(list, "Item 3");
  1774. Uri uri;
  1775. Uri.TryCreate("http://www.google.com", UriKind.RelativeOrAbsolute, out uri);
  1776. var hLink = document.AddHyperlink("Google", uri);
  1777. var item2 = list.Items[1];
  1778. item2.InsertText("\nMore text\n");
  1779. item2.AppendHyperlink(hLink);
  1780. item2.InsertText("\nEven more text");
  1781. document.InsertList(list);
  1782. document.Save();
  1783. }
  1784. }
  1785. [Test]
  1786. public void WhileReadingWhenTextIsBoldItalicUnderlineItShouldReadTheProperFormatting()
  1787. {
  1788. using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "FontFormat.docx")))
  1789. {
  1790. var underlinedTextFormatting = document.Paragraphs[0].MagicText[0].formatting;
  1791. var boldTextFormatting = document.Paragraphs[0].MagicText[2].formatting;
  1792. var italicTextFormatting = document.Paragraphs[0].MagicText[4].formatting;
  1793. var boldItalicUnderlineTextFormatting = document.Paragraphs[0].MagicText[6].formatting;
  1794. Assert.IsTrue(boldTextFormatting.Bold.HasValue && boldTextFormatting.Bold.Value);
  1795. Assert.IsTrue(italicTextFormatting.Italic.HasValue && italicTextFormatting.Italic.Value);
  1796. Assert.AreEqual(underlinedTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1797. Assert.IsTrue(boldItalicUnderlineTextFormatting.Bold.HasValue && boldItalicUnderlineTextFormatting.Bold.Value);
  1798. Assert.IsTrue(boldItalicUnderlineTextFormatting.Italic.HasValue && boldItalicUnderlineTextFormatting.Italic.Value);
  1799. Assert.AreEqual(boldItalicUnderlineTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1800. }
  1801. }
  1802. [Test]
  1803. public void WhileWritingWhenTextIsBoldItalicUnderlineItShouldReadTheProperFormatting()
  1804. {
  1805. using (DocX document = DocX.Create("FontFormatWrite.docx"))
  1806. {
  1807. Paragraph p = document.InsertParagraph();
  1808. p.Append("This is bold.").Bold().Append("This is underlined.").UnderlineStyle(UnderlineStyle.singleLine).
  1809. Append("This is italic.").Italic().Append("This is boldItalicUnderlined").Italic().Bold().UnderlineStyle(UnderlineStyle.singleLine);
  1810. var boldTextFormatting = document.Paragraphs[0].MagicText[0].formatting;
  1811. var underlinedTextFormatting = document.Paragraphs[0].MagicText[1].formatting;
  1812. var italicTextFormatting = document.Paragraphs[0].MagicText[2].formatting;
  1813. var boldItalicUnderlineTextFormatting = document.Paragraphs[0].MagicText[3].formatting;
  1814. Assert.IsTrue(boldTextFormatting.Bold.HasValue && boldTextFormatting.Bold.Value);
  1815. Assert.IsTrue(italicTextFormatting.Italic.HasValue && italicTextFormatting.Italic.Value);
  1816. Assert.AreEqual(underlinedTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1817. Assert.IsTrue(boldItalicUnderlineTextFormatting.Bold.HasValue && boldItalicUnderlineTextFormatting.Bold.Value);
  1818. Assert.IsTrue(boldItalicUnderlineTextFormatting.Italic.HasValue && boldItalicUnderlineTextFormatting.Italic.Value);
  1819. Assert.AreEqual(boldItalicUnderlineTextFormatting.UnderlineStyle, UnderlineStyle.singleLine);
  1820. }
  1821. }
  1822. [Test]
  1823. public void InsertingANextPageBreakShouldAddADocumentSection()
  1824. {
  1825. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "SectionPageBreak.docx")))
  1826. {
  1827. document.InsertSectionPageBreak();
  1828. var sections = document.GetSections();
  1829. Assert.AreEqual(sections.Count, 2);
  1830. document.Save();
  1831. }
  1832. }
  1833. [Test]
  1834. public void InsertANextPageBreakWithParagraphTextsShouldAddProperParagraphsToProperSections()
  1835. {
  1836. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "SectionPageBreakWithParagraphs.docx")))
  1837. {
  1838. document.InsertParagraph("First paragraph.");
  1839. document.InsertParagraph("Second paragraph.");
  1840. document.InsertSectionPageBreak();
  1841. document.InsertParagraph("Third paragraph.");
  1842. document.InsertParagraph("Fourth paragraph.");
  1843. var sections = document.GetSections();
  1844. Assert.AreEqual(sections.Count, 2);
  1845. Assert.AreEqual(sections[0].SectionParagraphs.Count(p => !string.IsNullOrWhiteSpace(p.Text)), 2);
  1846. Assert.AreEqual(sections[1].SectionParagraphs.Count(p => !string.IsNullOrWhiteSpace(p.Text)), 2);
  1847. document.Save();
  1848. }
  1849. }
  1850. [Test]
  1851. public void WhenAFontFamilyIsSpecifiedForAParagraphItShouldSetTheFontOfTheParagraphTextToTheFontFamily()
  1852. {
  1853. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "FontTest.docx")))
  1854. {
  1855. Paragraph p = document.InsertParagraph();
  1856. var fontFamily = new Font("Symbol");
  1857. p.Append("Hello World").Font(fontFamily);
  1858. Assert.AreEqual(p.MagicText[0].formatting.FontFamily.Name, fontFamily.Name);
  1859. document.Save();
  1860. }
  1861. }
  1862. [Test]
  1863. public void Test_Paragraph_RemoveTextManyLetters()
  1864. {
  1865. using (DocX document = DocX.Create(@"HelloWorldRemovingManyLetters.docx"))
  1866. {
  1867. Paragraph p3 = document.InsertParagraph("");
  1868. p3.Xml = XElement.Parse(
  1869. @"<w:p xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
  1870. <w:pPr>
  1871. <w:ind />
  1872. </w:pPr>
  1873. <w:r>
  1874. <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>
  1875. </w:r>
  1876. <w:r>
  1877. <w:rPr>
  1878. <w:lang w:val=""pl-PL"" />
  1879. </w:rPr>
  1880. <w:t xml:space=""preserve"">This survey should take </w:t>
  1881. </w:r>
  1882. <w:r>
  1883. <w:t>"" + f('xMinutes').get() + ""</w:t>
  1884. </w:r>
  1885. <w:r>
  1886. <w:rPr>
  1887. <w:lang w:val=""pl-PL"" />
  1888. </w:rPr>
  1889. <w:t xml:space=""preserve""> minutes. </w:t>
  1890. </w:r>
  1891. <w:r>
  1892. <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>
  1893. </w:r>
  1894. </w:p>");
  1895. int l1 = p3.Text.Length; //960
  1896. p3.RemoveText(318, 99);
  1897. int l2 = p3.Text.Length; //should be 861
  1898. Assert.AreEqual(l1 - 99, l2);
  1899. }
  1900. }
  1901. [Test]
  1902. public void Test_Table_RemoveParagraphs()
  1903. {
  1904. var memoryStream = new MemoryStream();
  1905. var document = DocX.Create(memoryStream);
  1906. // Add a Table into the document.
  1907. Table table = document.AddTable(1, 4); // 1 row, 4 cells
  1908. table.Design = TableDesign.TableGrid;
  1909. table.Alignment = Alignment.center;
  1910. // Edit row
  1911. var row = table.Rows[0];
  1912. // Fill 1st paragraph
  1913. row.Cells[0].Paragraphs.ElementAt(0).Append("Paragraph 1");
  1914. // Fill 2nd paragraph
  1915. var secondParagraph = row.Cells[0].InsertParagraph("Paragraph 2");
  1916. // Check number of paragraphs
  1917. Assert.AreEqual(2, row.Cells[0].Paragraphs.Count());
  1918. // Remove 1st paragraph
  1919. var deleted = row.Cells[0].RemoveParagraphAt(0);
  1920. Assert.IsTrue(deleted);
  1921. // Check number of paragraphs
  1922. Assert.AreEqual(1, row.Cells[0].Paragraphs.Count());
  1923. // Remove 3rd (nonexisting) paragraph
  1924. deleted = row.Cells[0].RemoveParagraphAt(3);
  1925. Assert.IsFalse(deleted);
  1926. //check number of paragraphs
  1927. Assert.AreEqual(1, row.Cells[0].Paragraphs.Count());
  1928. // Remove secondParagraph (this time the only one) paragraph
  1929. deleted = row.Cells[0].RemoveParagraph(secondParagraph);
  1930. Assert.IsTrue(deleted);
  1931. Assert.AreEqual(0, row.Cells[0].Paragraphs.Count());
  1932. // Remove last paragraph once again - this time this paragraph does not exists
  1933. deleted = row.Cells[0].RemoveParagraph(secondParagraph);
  1934. Assert.IsFalse(deleted);
  1935. Assert.AreEqual(0, row.Cells[0].Paragraphs.Count());
  1936. }
  1937. [Test]
  1938. public void GenerateHeadingTestDocument()
  1939. {
  1940. using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, @"Document Header Test.docx")))
  1941. {
  1942. foreach (HeadingType heading in (HeadingType[])Enum.GetValues(typeof(HeadingType)))
  1943. {
  1944. string text = string.Format("{0} - The quick brown fox jumps over the lazy dog", heading.EnumDescription());
  1945. Paragraph p = document.InsertParagraph();
  1946. p.AppendLine(text).Heading(heading);
  1947. }
  1948. document.Save();
  1949. }
  1950. }
  1951. [Test]
  1952. public void CreateTableOfContents_WithTitleAndSwitches_SetsExpectedXml()
  1953. {
  1954. using (var document = DocX.Create("TableOfContents Test.docx"))
  1955. {
  1956. const string title = "TestTitle";
  1957. const int rightPos = 9350;
  1958. const TableOfContentsSwitches switches =
  1959. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  1960. TableOfContentsSwitches.U;
  1961. var toc = TableOfContents.CreateTableOfContents(document, title, switches);
  1962. const string switchString = @"TOC \h \o '1-3' \u \z";
  1963. var expectedString = string.Format(XmlTemplateBases.TocXmlBase, "TOCHeading", title, rightPos, switchString);
  1964. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  1965. var expected = XElement.Load(expectedReader);
  1966. Assert.IsTrue(XNode.DeepEquals(expected, toc.Xml));
  1967. }
  1968. }
  1969. [Test]
  1970. public void CreateTableOfContents_WithTitleSwitchesHeaderStyleLastIncludeLevelRightTabPos_SetsExpectedXml()
  1971. {
  1972. using (var document = DocX.Create("TableOfContents Test.docx"))
  1973. {
  1974. const string title = "TestTitle";
  1975. const int rightPos = 1337;
  1976. const string style = "TestStyle";
  1977. const TableOfContentsSwitches switches =
  1978. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  1979. TableOfContentsSwitches.U;
  1980. var toc = TableOfContents.CreateTableOfContents(document, title, switches, style, 6, rightPos);
  1981. const string switchString = @"TOC \h \o '1-6' \u \z";
  1982. var expectedString = string.Format(XmlTemplateBases.TocXmlBase, style, title, rightPos, switchString);
  1983. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  1984. var expected = XElement.Load(expectedReader);
  1985. Assert.IsTrue(XNode.DeepEquals(expected, toc.Xml));
  1986. }
  1987. }
  1988. [Test]
  1989. public void CreateTableOfContents_WhenCalled_AddsUpdateFieldsWithValueTrueToSettings()
  1990. {
  1991. using (var document = DocX.Create("TableOfContents Test.docx"))
  1992. {
  1993. const string title = "TestTitle";
  1994. const TableOfContentsSwitches switches =
  1995. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  1996. TableOfContentsSwitches.U;
  1997. TableOfContents.CreateTableOfContents(document, title, switches);
  1998. var updateField = document.settings.Descendants().FirstOrDefault(x => x.Name == DocX.w + "updateFields");
  1999. Assert.IsNotNull(updateField);
  2000. Assert.AreEqual("true", updateField.Attribute(DocX.w + "val").Value);
  2001. }
  2002. }
  2003. [Test]
  2004. public void CreateTableOfContents_WhenCalledSettingsAlreadyHasUpdateFields_DoesNotAddUpdateFields()
  2005. {
  2006. using (var document = DocX.Create("TableOfContents Test.docx"))
  2007. {
  2008. var element = new XElement(XName.Get("updateFields", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", true));
  2009. document.settings.Root.Add(element);
  2010. const string title = "TestTitle";
  2011. const TableOfContentsSwitches switches =
  2012. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2013. TableOfContentsSwitches.U;
  2014. TableOfContents.CreateTableOfContents(document, title, switches);
  2015. var updateFields = document.settings.Descendants().Single(x => x.Name == DocX.w + "updateFields");
  2016. Assert.AreSame(element, updateFields);
  2017. }
  2018. }
  2019. [Test]
  2020. public void CreteTableOfContents_TocHeadingStyleIsNotPresent_AddsTocHeaderStyle()
  2021. {
  2022. using (var document = DocX.Create("TableOfContents Test.docx"))
  2023. {
  2024. const string title = "TestTitle";
  2025. const string headerStyle = "TestStyle";
  2026. const TableOfContentsSwitches switches =
  2027. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2028. TableOfContentsSwitches.U;
  2029. TableOfContents.CreateTableOfContents(document, title, switches, headerStyle);
  2030. var expectedString = string.Format(XmlTemplateBases.TocHeadingStyleBase, headerStyle);
  2031. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  2032. var expected = XElement.Load(expectedReader);
  2033. var actual = document.styles.Root.Descendants().FirstOrDefault(x =>
  2034. x.Name.Equals(DocX.w + "style") &&
  2035. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2036. x.Attribute(DocX.w + "styleId").Value.Equals(headerStyle));
  2037. Assert.IsTrue(XNode.DeepEquals(expected, actual));
  2038. }
  2039. }
  2040. [Test]
  2041. public void CreteTableOfContents_Toc1StyleIsNotPresent_AddsToc1Style()
  2042. {
  2043. using (var document = DocX.Create("TableOfContents Test.docx"))
  2044. {
  2045. const string title = "TestTitle";
  2046. const TableOfContentsSwitches switches =
  2047. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2048. TableOfContentsSwitches.U;
  2049. TableOfContents.CreateTableOfContents(document, title, switches);
  2050. var expectedString = string.Format(XmlTemplateBases.TocElementStyleBase, "TOC1", "toc 1");
  2051. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  2052. var expected = XElement.Load(expectedReader);
  2053. var actual = document.styles.Root.Descendants().FirstOrDefault(x =>
  2054. x.Name.Equals(DocX.w + "style") &&
  2055. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2056. x.Attribute(DocX.w + "styleId").Value.Equals("TOC1"));
  2057. Assert.IsTrue(XNode.DeepEquals(expected, actual));
  2058. }
  2059. }
  2060. [Test]
  2061. public void CreteTableOfContents_Toc2StyleIsNotPresent_AddsToc2Style()
  2062. {
  2063. using (var document = DocX.Create("TableOfContents Test.docx"))
  2064. {
  2065. const string title = "TestTitle";
  2066. const TableOfContentsSwitches switches =
  2067. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2068. TableOfContentsSwitches.U;
  2069. TableOfContents.CreateTableOfContents(document, title, switches);
  2070. var expectedString = string.Format(XmlTemplateBases.TocElementStyleBase, "TOC2", "toc 2");
  2071. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  2072. var expected = XElement.Load(expectedReader);
  2073. var actual = document.styles.Root.Descendants().FirstOrDefault(x =>
  2074. x.Name.Equals(DocX.w + "style") &&
  2075. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2076. x.Attribute(DocX.w + "styleId").Value.Equals("TOC2"));
  2077. Assert.IsTrue(XNode.DeepEquals(expected, actual));
  2078. }
  2079. }
  2080. [Test]
  2081. public void CreteTableOfContents_Toc3StyleIsNotPresent_AddsToc3tyle()
  2082. {
  2083. using (var document = DocX.Create("TableOfContents Test.docx"))
  2084. {
  2085. const string title = "TestTitle";
  2086. const TableOfContentsSwitches switches =
  2087. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2088. TableOfContentsSwitches.U;
  2089. TableOfContents.CreateTableOfContents(document, title, switches);
  2090. var expectedString = string.Format(XmlTemplateBases.TocElementStyleBase, "TOC3", "toc 3");
  2091. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  2092. var expected = XElement.Load(expectedReader);
  2093. var actual = document.styles.Root.Descendants().FirstOrDefault(x =>
  2094. x.Name.Equals(DocX.w + "style") &&
  2095. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2096. x.Attribute(DocX.w + "styleId").Value.Equals("TOC3"));
  2097. Assert.IsTrue(XNode.DeepEquals(expected, actual));
  2098. }
  2099. }
  2100. [Test]
  2101. public void CreteTableOfContents_Toc4StyleIsNotPresent_AddsToc4Style()
  2102. {
  2103. using (var document = DocX.Create("TableOfContents Test.docx"))
  2104. {
  2105. const string title = "TestTitle";
  2106. const TableOfContentsSwitches switches =
  2107. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2108. TableOfContentsSwitches.U;
  2109. TableOfContents.CreateTableOfContents(document, title, switches);
  2110. var expectedString = string.Format(XmlTemplateBases.TocElementStyleBase, "TOC4", "toc 4");
  2111. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  2112. var expected = XElement.Load(expectedReader);
  2113. var actual = document.styles.Root.Descendants().FirstOrDefault(x =>
  2114. x.Name.Equals(DocX.w + "style") &&
  2115. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2116. x.Attribute(DocX.w + "styleId").Value.Equals("TOC4"));
  2117. Assert.IsTrue(XNode.DeepEquals(expected, actual));
  2118. }
  2119. }
  2120. [Test]
  2121. public void CreteTableOfContents_HyperlinkStyleIsNotPresent_AddsHyperlinkStyle()
  2122. {
  2123. using (var document = DocX.Create("TableOfContents Test.docx"))
  2124. {
  2125. const string title = "TestTitle";
  2126. const TableOfContentsSwitches switches =
  2127. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2128. TableOfContentsSwitches.U;
  2129. TableOfContents.CreateTableOfContents(document, title, switches);
  2130. var expectedString = XmlTemplateBases.TocHyperLinkStyleBase;
  2131. var expectedReader = XmlReader.Create(new StringReader(expectedString));
  2132. var expected = XElement.Load(expectedReader);
  2133. var actual = document.styles.Root.Descendants().FirstOrDefault(x =>
  2134. x.Name.Equals(DocX.w + "style") &&
  2135. x.Attribute(DocX.w + "type").Value.Equals("character") &&
  2136. x.Attribute(DocX.w + "styleId").Value.Equals("Hyperlink"));
  2137. Assert.IsTrue(XNode.DeepEquals(expected, actual));
  2138. }
  2139. }
  2140. [Test]
  2141. public void CreteTableOfContents_TocHeadingStyleIsPresent_DoesNotAddTocHeaderStyle()
  2142. {
  2143. using (var document = DocX.Create("TableOfContents Test.docx"))
  2144. {
  2145. const string title = "TestTitle";
  2146. const string headerStyle = "TestStyle";
  2147. const TableOfContentsSwitches switches =
  2148. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2149. TableOfContentsSwitches.U;
  2150. var xElement = XElement.Load(XmlReader.Create(new StringReader(string.Format(XmlTemplateBases.TocHeadingStyleBase, headerStyle))));
  2151. document.styles.Root.Add(xElement);
  2152. TableOfContents.CreateTableOfContents(document, title, switches, headerStyle);
  2153. var actual = document.styles.Root.Descendants().Single(x =>
  2154. x.Name.Equals(DocX.w + "style") &&
  2155. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2156. x.Attribute(DocX.w + "styleId").Value.Equals(headerStyle));
  2157. Assert.AreSame(xElement, actual);
  2158. }
  2159. }
  2160. [Test]
  2161. public void CreteTableOfContents_Toc1StyleIsPresent_DoesNotAddToc1Style()
  2162. {
  2163. using (var document = DocX.Create("TableOfContents Test.docx"))
  2164. {
  2165. const string title = "TestTitle";
  2166. const string headerStyle = "TestStyle";
  2167. const TableOfContentsSwitches switches =
  2168. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2169. TableOfContentsSwitches.U;
  2170. var xElement = XElement.Load(XmlReader.Create(new StringReader(string.Format(XmlTemplateBases.TocElementStyleBase, "TOC1", "toc 1"))));
  2171. document.styles.Root.Add(xElement);
  2172. TableOfContents.CreateTableOfContents(document, title, switches, headerStyle);
  2173. var actual = document.styles.Root.Descendants().Single(x =>
  2174. x.Name.Equals(DocX.w + "style") &&
  2175. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2176. x.Attribute(DocX.w + "styleId").Value.Equals("TOC1"));
  2177. Assert.AreSame(xElement, actual);
  2178. }
  2179. }
  2180. [Test]
  2181. public void CreteTableOfContents_Toc2StyleIsPresent_DoesNotAddToc2Style()
  2182. {
  2183. using (var document = DocX.Create("TableOfContents Test.docx"))
  2184. {
  2185. const string title = "TestTitle";
  2186. const string headerStyle = "TestStyle";
  2187. const TableOfContentsSwitches switches =
  2188. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2189. TableOfContentsSwitches.U;
  2190. var xElement = XElement.Load(XmlReader.Create(new StringReader(string.Format(XmlTemplateBases.TocElementStyleBase, "TOC2", "toc 2"))));
  2191. document.styles.Root.Add(xElement);
  2192. TableOfContents.CreateTableOfContents(document, title, switches, headerStyle);
  2193. var actual = document.styles.Root.Descendants().Single(x =>
  2194. x.Name.Equals(DocX.w + "style") &&
  2195. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2196. x.Attribute(DocX.w + "styleId").Value.Equals("TOC2"));
  2197. Assert.AreSame(xElement, actual);
  2198. }
  2199. }
  2200. [Test]
  2201. public void CreteTableOfContents_Toc3StyleIsPresent_DoesNotAddToc3Style()
  2202. {
  2203. using (var document = DocX.Create("TableOfContents Test.docx"))
  2204. {
  2205. const string title = "TestTitle";
  2206. const string headerStyle = "TestStyle";
  2207. const TableOfContentsSwitches switches =
  2208. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2209. TableOfContentsSwitches.U;
  2210. var xElement = XElement.Load(XmlReader.Create(new StringReader(string.Format(XmlTemplateBases.TocElementStyleBase, "TOC3", "toc 3"))));
  2211. document.styles.Root.Add(xElement);
  2212. TableOfContents.CreateTableOfContents(document, title, switches, headerStyle);
  2213. var actual = document.styles.Root.Descendants().Single(x =>
  2214. x.Name.Equals(DocX.w + "style") &&
  2215. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2216. x.Attribute(DocX.w + "styleId").Value.Equals("TOC3"));
  2217. Assert.AreSame(xElement, actual);
  2218. }
  2219. }
  2220. [Test]
  2221. public void CreteTableOfContents_Toc4StyleIsPresent_DoesNotAddToc4Style()
  2222. {
  2223. using (var document = DocX.Create("TableOfContents Test.docx"))
  2224. {
  2225. const string title = "TestTitle";
  2226. const string headerStyle = "TestStyle";
  2227. const TableOfContentsSwitches switches =
  2228. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2229. TableOfContentsSwitches.U;
  2230. var xElement = XElement.Load(XmlReader.Create(new StringReader(string.Format(XmlTemplateBases.TocElementStyleBase, "TOC4", "toc 4"))));
  2231. document.styles.Root.Add(xElement);
  2232. TableOfContents.CreateTableOfContents(document, title, switches, headerStyle);
  2233. var actual = document.styles.Root.Descendants().Single(x =>
  2234. x.Name.Equals(DocX.w + "style") &&
  2235. x.Attribute(DocX.w + "type").Value.Equals("paragraph") &&
  2236. x.Attribute(DocX.w + "styleId").Value.Equals("TOC4"));
  2237. Assert.AreSame(xElement, actual);
  2238. }
  2239. }
  2240. [Test]
  2241. public void CreteTableOfContents_HyperlinkStyleIsPresent_DoesNotAddHyperlinkStyle()
  2242. {
  2243. using (var document = DocX.Create("TableOfContents Test.docx"))
  2244. {
  2245. const string title = "TestTitle";
  2246. const TableOfContentsSwitches switches =
  2247. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2248. TableOfContentsSwitches.U;
  2249. var xElement = XElement.Load(XmlReader.Create(new StringReader(XmlTemplateBases.TocHyperLinkStyleBase)));
  2250. document.styles.Root.Add(xElement);
  2251. TableOfContents.CreateTableOfContents(document, title, switches);
  2252. var actual = document.styles.Root.Descendants().Single(x =>
  2253. x.Name.Equals(DocX.w + "style") &&
  2254. x.Attribute(DocX.w + "type").Value.Equals("character") &&
  2255. x.Attribute(DocX.w + "styleId").Value.Equals("Hyperlink"));
  2256. Assert.AreSame(xElement, actual);
  2257. }
  2258. }
  2259. [Test]
  2260. public void InsertDefaultTableOfContents_WhenCalled_AddsTocToDocument()
  2261. {
  2262. using (var document = DocX.Create("TableOfContents Test.docx"))
  2263. {
  2264. document.InsertDefaultTableOfContents();
  2265. var toc = TableOfContents.CreateTableOfContents(document, "Table of contents",
  2266. TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z |
  2267. TableOfContentsSwitches.U);
  2268. Assert.IsTrue(document.Xml.Descendants().FirstOrDefault(x => XNode.DeepEquals(toc.Xml, x)) != null);
  2269. }
  2270. }
  2271. [Test]
  2272. public void InsertTableOfContents_WhenCalledWithTitleSwitchesHeaderStyleMaxIncludeLevelAndRightTabPos_AddsTocToDocument()
  2273. {
  2274. using (var document = DocX.Create("TableOfContents Test.docx"))
  2275. {
  2276. const string tableOfContentsTitle = "Table of contents";
  2277. const TableOfContentsSwitches tableOfContentsSwitches = TableOfContentsSwitches.O | TableOfContentsSwitches.A;
  2278. const string headerStyle = "HeaderStyle";
  2279. const int lastIncludeLevel = 4;
  2280. const int rightTabPos = 1337;
  2281. document.InsertTableOfContents(tableOfContentsTitle, tableOfContentsSwitches, headerStyle, lastIncludeLevel, rightTabPos);
  2282. var toc = TableOfContents.CreateTableOfContents(document, tableOfContentsTitle, tableOfContentsSwitches, headerStyle, lastIncludeLevel, rightTabPos);
  2283. Assert.IsTrue(document.Xml.Descendants().FirstOrDefault(x => XNode.DeepEquals(toc.Xml, x)) != null);
  2284. }
  2285. }
  2286. [Test]
  2287. public void InsertTableOfContents_WhenCalledWithReferenceTitleSwitchesHeaderStyleMaxIncludeLevelAndRightTabPos_AddsTocToDocumentAtExpectedLocation()
  2288. {
  2289. using (var document = DocX.Create("TableOfContents Test.docx"))
  2290. {
  2291. const string tableOfContentsTitle = "Table of contents";
  2292. const TableOfContentsSwitches tableOfContentsSwitches = TableOfContentsSwitches.O | TableOfContentsSwitches.A;
  2293. const string headerStyle = "HeaderStyle";
  2294. const int lastIncludeLevel = 4;
  2295. const int rightTabPos = 1337;
  2296. document.InsertParagraph("Paragraph1");
  2297. var p2 = document.InsertParagraph("Paragraph2");
  2298. var p3 = document.InsertParagraph("Paragraph3");
  2299. document.InsertTableOfContents(p3, tableOfContentsTitle, tableOfContentsSwitches, headerStyle, lastIncludeLevel, rightTabPos);
  2300. var toc = TableOfContents.CreateTableOfContents(document, tableOfContentsTitle, tableOfContentsSwitches, headerStyle, lastIncludeLevel, rightTabPos);
  2301. var tocElement = document.Xml.Descendants().FirstOrDefault(x => XNode.DeepEquals(toc.Xml, x));
  2302. Assert.IsTrue(p2.Xml.IsBefore(tocElement));
  2303. Assert.IsTrue(tocElement.IsAfter(p2.Xml));
  2304. Assert.IsTrue(tocElement.IsBefore(p3.Xml));
  2305. Assert.IsTrue(p3.Xml.IsAfter(tocElement));
  2306. }
  2307. }
  2308. [Test]
  2309. public void ValidateBookmark_WhenCalledWithNameOfNonMatchingBookmark_ReturnsFalse()
  2310. {
  2311. using (var document = DocX.Create("Bookmark validate.docx"))
  2312. {
  2313. var p = document.InsertParagraph("No bookmark here");
  2314. Assert.IsFalse(p.ValidateBookmark("Team Rubberduck"));
  2315. }
  2316. }
  2317. [Test]
  2318. public void ValidateBookmark_WhenCalledWithNameOfMatchingBookmark_ReturnsTrue()
  2319. {
  2320. using (var document = DocX.Create("Bookmark validate.docx"))
  2321. {
  2322. var p = document.InsertParagraph("Here's a bookmark!");
  2323. const string bookmarkName = "Team Rubberduck";
  2324. p.AppendBookmark(bookmarkName);
  2325. Assert.IsTrue(p.ValidateBookmark("Team Rubberduck"));
  2326. }
  2327. }
  2328. [Test]
  2329. public void ValidateBookmarks_WhenCalledWithMatchingBookmarkNameInHeader_ReturnsEmpty()
  2330. {
  2331. using (var document = DocX.Create("Bookmark validate.docx"))
  2332. {
  2333. document.AddHeaders();
  2334. var p = document.Headers.first.InsertParagraph("Here's a bookmark!");
  2335. const string bookmarkName = "Team Rubberduck";
  2336. p.AppendBookmark(bookmarkName);
  2337. Assert.IsTrue(document.ValidateBookmarks("Team Rubberduck").Length == 0);
  2338. }
  2339. }
  2340. [Test]
  2341. public void ValidateBookmarks_WhenCalledWithMatchingBookmarkNameInMainDocument_ReturnsEmpty()
  2342. {
  2343. using (var document = DocX.Create("Bookmark validate.docx"))
  2344. {
  2345. var p = document.InsertParagraph("Here's a bookmark!");
  2346. const string bookmarkName = "Team Rubberduck";
  2347. p.AppendBookmark(bookmarkName);
  2348. Assert.IsTrue(document.ValidateBookmarks("Team Rubberduck").Length == 0);
  2349. }
  2350. }
  2351. [Test]
  2352. public void ValidateBookmarks_WhenCalledWithMatchingBookmarkNameInFooters_ReturnsEmpty()
  2353. {
  2354. using (var document = DocX.Create("Bookmark validate.docx"))
  2355. {
  2356. document.AddFooters();
  2357. var p = document.Footers.first.InsertParagraph("Here's a bookmark!");
  2358. const string bookmarkName = "Team Rubberduck";
  2359. p.AppendBookmark(bookmarkName);
  2360. Assert.IsTrue(document.ValidateBookmarks("Team Rubberduck").Length == 0);
  2361. }
  2362. }
  2363. [Test]
  2364. public void ValidateBookmarks_WhenCalledWithNoMatchingBookmarkNames_ReturnsExpected()
  2365. {
  2366. using (var document = DocX.Create("Bookmark validate.docx"))
  2367. {
  2368. document.AddHeaders();
  2369. var p = document.Headers.first.InsertParagraph("Here's a bookmark!");
  2370. p.AppendBookmark("Not in search");
  2371. var bookmarkNames = new[] { "Team Rubberduck", "is", "the most", "awesome people" };
  2372. var result = document.ValidateBookmarks(bookmarkNames);
  2373. for (var i = 0; i < bookmarkNames.Length; i++)
  2374. {
  2375. Assert.AreEqual(bookmarkNames[i], result[i]);
  2376. }
  2377. }
  2378. }
  2379. [Test]
  2380. public void CreateTable_WhenCalledSetColumnWidth_ReturnsExpected()
  2381. {
  2382. using (var document = DocX.Create("Set column width.docx"))
  2383. {
  2384. var table = document.InsertTable(1, 2);
  2385. table.SetColumnWidth(0, 1000);
  2386. table.SetColumnWidth(1, 2000);
  2387. Assert.AreEqual(1000, table.GetColumnWidth(0));
  2388. Assert.AreEqual(2000, table.GetColumnWidth(1));
  2389. }
  2390. }
  2391. [Test]
  2392. public void UpdateParagraphFontSize_WhenSetFontSize_ReturnsExpected()
  2393. {
  2394. using (var document = DocX.Create("Update paragraph font size.docx"))
  2395. {
  2396. var paragraph = document.InsertParagraph().FontSize(9);
  2397. paragraph.FontSize(11);
  2398. string szValue = paragraph.Xml.Descendants(XName.Get("sz", DocX.w.NamespaceName))
  2399. .Attributes(XName.Get("val", DocX.w.NamespaceName)).First().Value;
  2400. string szCsValue = paragraph.Xml.Descendants(XName.Get("szCs", DocX.w.NamespaceName))
  2401. .Attributes(XName.Get("val", DocX.w.NamespaceName)).First().Value;
  2402. // the expected value is multiplied by 2
  2403. // and the last font size is 11*2 = 22
  2404. Assert.AreEqual("22", szValue);
  2405. Assert.AreEqual("22", szCsValue);
  2406. }
  2407. }
  2408. [Test]
  2409. public void SetTableCellMargin_WhenSetTwoCellMargins_ContainsTwoCellMargins()
  2410. {
  2411. using (var document = DocX.Create("Set table cell margins.docx"))
  2412. {
  2413. Table table = document.InsertTable(1, 1);
  2414. table.SetTableCellMargin(TableCellMarginType.left, 20);
  2415. table.SetTableCellMargin(TableCellMarginType.right, 40);
  2416. var elements = table.Xml.Descendants(XName.Get("tblCellMar", DocX.w.NamespaceName)).ToList();
  2417. // should contain only one element named tblCellMar
  2418. Assert.AreEqual(1, elements.Count);
  2419. var element = elements.First();
  2420. // should contain two elements defining the margins
  2421. Assert.AreEqual(2, element.Elements().Count());
  2422. var left = element.Element(XName.Get("left", DocX.w.NamespaceName));
  2423. var right = element.Element(XName.Get("right", DocX.w.NamespaceName));
  2424. Assert.IsNotNull(left);
  2425. Assert.IsNotNull(right);
  2426. Assert.AreEqual("20", left.Attribute(XName.Get("w", DocX.w.NamespaceName)).Value);
  2427. Assert.AreEqual("dxa", left.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value);
  2428. Assert.AreEqual("40", right.Attribute(XName.Get("w", DocX.w.NamespaceName)).Value);
  2429. Assert.AreEqual("dxa", right.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value);
  2430. }
  2431. }
  2432. [Test]
  2433. public void SetTableCellMargin_WhenReSetCellMargin_ContainsOneCellMargin()
  2434. {
  2435. using (var document = DocX.Create("Set table cell margin.docx"))
  2436. {
  2437. Table table = document.InsertTable(1, 1);
  2438. table.SetTableCellMargin(TableCellMarginType.left, 20);
  2439. // change the table cell margin
  2440. table.SetTableCellMargin(TableCellMarginType.left, 40);
  2441. var elements = table.Xml.Descendants(XName.Get("tblCellMar", DocX.w.NamespaceName)).ToList();
  2442. // should contain only one element named tblCellMar
  2443. Assert.AreEqual(1, elements.Count);
  2444. var element = elements.First();
  2445. // should contain two elements defining the margins
  2446. Assert.AreEqual(1, element.Elements().Count());
  2447. var left = element.Element(XName.Get("left", DocX.w.NamespaceName));
  2448. Assert.IsNotNull(left);
  2449. // check that the last value is taken
  2450. Assert.AreEqual("40", left.Attribute(XName.Get("w", DocX.w.NamespaceName)).Value);
  2451. Assert.AreEqual("dxa", left.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value);
  2452. }
  2453. }
  2454. }
  2455. }