| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118 |
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Novacode;
- using System.Reflection;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Xml.Linq;
- using System.IO.Packaging;
- using System.Text.RegularExpressions;
-
- namespace UnitTests
- {
- /// <summary>
- /// Summary description for UnitTest1
- /// </summary>
- [TestClass]
- public class UnitTest1
- {
- // Get the fullpath to the executing assembly.
- string directory_executing_assembly;
- string directory_documents;
- string file_temp = "temp.docx";
-
- const string package_part_document = "/word/document.xml";
-
- public UnitTest1()
- {
- directory_executing_assembly = Assembly.GetExecutingAssembly().Location;
-
- // The documents directory
- List<string> steps = directory_executing_assembly.Split('\\').ToList();
- steps.RemoveRange(steps.Count() - 3, 3);
- directory_documents = String.Join("\\", steps) + "\\documents\\";
- }
-
- [TestMethod]
- public void Test_Pattern_Replacement()
- {
- Dictionary<string, string> testPatterns = new Dictionary<string, string>()
- {
- {"COURT NAME","Fred Frump"},
- {"Case Number","cr-md-2011-1234567"}
- };
-
- using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
- {
- foreach (var t in replaceDoc.Tables)
- { // each table has 1 row and 3 columns
- Assert.IsTrue(t.Rows[0].Cells.Count == 3);
- Assert.IsTrue(t.ColumnCount == 3);
- Assert.IsTrue(t.Rows.Count == 1);
- Assert.IsTrue(t.RowCount == 1);
- }
-
- // Make sure the origional strings are in the document.
- Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
- Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
-
- // There are only two patterns, even though each pattern is used more than once
- Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
-
- // Make sure the new strings are not in the document.
- Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
- Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
-
- // Do the replacing
- foreach (var p in testPatterns)
- replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
-
- // Make sure the origional string are no longer in the document.
- Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
- Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
-
- // Make sure the new strings are now in the document.
- Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
- Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
-
- // Make sure the replacement worked.
- 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");
- }
-
- }
-
- [TestMethod]
- public void Test_EverybodyHasAHome_Loaded()
- {
- // Load a document.
- using (DocX document = DocX.Load(directory_documents + "EverybodyHasAHome.docx"))
- {
- // Main document tests.
- string document_xml_file = document.mainPart.Uri.OriginalString;
- Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
-
- // header first
- Header header_first = document.Headers.first;
- string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
-
- // header odd
- Header header_odd = document.Headers.odd;
- string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
-
- // header even
- Header header_even = document.Headers.even;
- string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
-
- // footer first
- Footer footer_first = document.Footers.first;
- string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
-
- // footer odd
- Footer footer_odd = document.Footers.odd;
- string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
-
- // footer even
- Footer footer_even = document.Footers.even;
- string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- }
- }
-
- [TestMethod]
- public void Test_Insert_Picture_ParagraphBeforeSelf()
- {
- // Create test document.
- using (DocX document = DocX.Create(directory_documents + "Test.docx"))
- {
- // Add an Image to this document.
- Novacode.Image img = document.AddImage(directory_documents + "purple.png");
-
- // Create a Picture from this Image.
- Picture pic = img.CreatePicture();
- Assert.IsNotNull(pic);
-
- // Main document.
- Paragraph p0 = document.InsertParagraph("Hello");
- Paragraph p1 = p0.InsertParagraphBeforeSelf("again");
- p1.InsertPicture(pic, 3);
-
- // Save this document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_Insert_Picture_ParagraphAfterSelf()
- {
- // Create test document.
- using (DocX document = DocX.Create(directory_documents + "Test.docx"))
- {
- // Add an Image to this document.
- Novacode.Image img = document.AddImage(directory_documents + "purple.png");
-
- // Create a Picture from this Image.
- Picture pic = img.CreatePicture();
- Assert.IsNotNull(pic);
-
- // Main document.
- Paragraph p0 = document.InsertParagraph("Hello");
- Paragraph p1 = p0.InsertParagraphAfterSelf("again");
- p1.InsertPicture(pic, 3);
-
- // Save this document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_EverybodyHasAHome_Created()
- {
- // Create a new document.
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Create a Table.
- Table t = document.AddTable(3, 3);
- t.Design = TableDesign.TableGrid;
-
- // Insert a Paragraph and a Table into the main document.
- document.InsertParagraph();
- document.InsertTable(t);
-
- // Insert a Paragraph and a Table into every Header.
- document.AddHeaders();
- document.Headers.odd.InsertParagraph();
- document.Headers.odd.InsertTable(t);
- document.Headers.even.InsertParagraph();
- document.Headers.even.InsertTable(t);
- document.Headers.first.InsertParagraph();
- document.Headers.first.InsertTable(t);
-
- // Insert a Paragraph and a Table into every Footer.
- document.AddFooters();
- document.Footers.odd.InsertParagraph();
- document.Footers.odd.InsertTable(t);
- document.Footers.even.InsertParagraph();
- document.Footers.even.InsertTable(t);
- document.Footers.first.InsertParagraph();
- document.Footers.first.InsertTable(t);
-
- // Main document tests.
- string document_xml_file = document.mainPart.Uri.OriginalString;
- Assert.IsTrue(document.Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
- Assert.IsTrue(document.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(document_xml_file));
-
- // header first
- Header header_first = document.Headers.first;
- string header_first_xml_file = header_first.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(header_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
- Assert.IsTrue(header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_first_xml_file));
-
- // header odd
- Header header_odd = document.Headers.odd;
- string header_odd_xml_file = header_odd.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(header_odd.mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
- Assert.IsTrue(header_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_odd_xml_file));
-
- // header even
- Header header_even = document.Headers.even;
- string header_even_xml_file = header_even.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(header_even.mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
- Assert.IsTrue(header_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(header_even_xml_file));
-
- // footer first
- Footer footer_first = document.Footers.first;
- string footer_first_xml_file = footer_first.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(footer_first.mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
- Assert.IsTrue(footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_first_xml_file));
-
- // footer odd
- Footer footer_odd = document.Footers.odd;
- string footer_odd_xml_file = footer_odd.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(footer_odd.mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
- Assert.IsTrue(footer_odd.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_odd_xml_file));
-
- // footer even
- Footer footer_even = document.Footers.even;
- string footer_even_xml_file = footer_even.mainPart.Uri.OriginalString;
-
- Assert.IsTrue(footer_even.mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].Rows[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- Assert.IsTrue(footer_even.Tables[0].Rows[0].Cells[0].Paragraphs[0].mainPart.Uri.OriginalString.Equals(footer_even_xml_file));
- }
- }
-
- [TestMethod]
- public void Test_Document_AddImage_FromDisk()
- {
- using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
- {
- // Add a png to into this document
- Novacode.Image png = document.AddImage(directory_documents + "purple.png");
- Assert.IsTrue(document.Images.Count == 1);
- Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
-
- // Add a tiff into to this document
- Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
- Assert.IsTrue(document.Images.Count == 2);
- Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
-
- // Add a gif into to this document
- Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
- Assert.IsTrue(document.Images.Count == 3);
- Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
-
- // Add a jpg into to this document
- Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
- Assert.IsTrue(document.Images.Count == 4);
- Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
-
- // Add a bitmap to this document
- Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
- Assert.IsTrue(document.Images.Count == 5);
- // Word does not allow bmp make sure it was inserted as a png.
- Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
- }
- }
-
- [TestMethod]
- public void Test_Document_AddImage_FromStream()
- {
- using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
- {
- // DocX will always insert Images that come from Streams as jpeg.
-
- // Add a png to into this document
- Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
- Assert.IsTrue(document.Images.Count == 1);
- Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
-
- // Add a tiff into to this document
- Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
- Assert.IsTrue(document.Images.Count == 2);
- Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
-
- // Add a gif into to this document
- Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
- Assert.IsTrue(document.Images.Count == 3);
- Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
-
- // Add a jpg into to this document
- Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
- Assert.IsTrue(document.Images.Count == 4);
- Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
-
- // Add a bitmap to this document
- Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
- Assert.IsTrue(document.Images.Count == 5);
- // Word does not allow bmp make sure it was inserted as a png.
- Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
- }
- }
-
- [TestMethod]
- public void Test_Tables()
- {
- using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
- {
- // There is only one Paragraph at the document level.
- Assert.IsTrue(document.Paragraphs.Count() == 13);
-
- // There is only one Table in this document.
- Assert.IsTrue(document.Tables.Count() == 1);
-
- // Extract the only Table.
- Table t0 = document.Tables[0];
-
- // This table has 12 Paragraphs.
- Assert.IsTrue(t0.Paragraphs.Count() == 12);
- }
- }
-
- [TestMethod]
- public void Test_Images()
- {
- using (DocX document = DocX.Load(directory_documents + "Images.docx"))
- {
- // Extract Images from Document.
- List<Novacode.Image> document_images = document.Images;
-
- // Make sure there are 3 Images in this document.
- Assert.IsTrue(document_images.Count() == 3);
-
- // Extract the headers from this document.
- Headers headers = document.Headers;
- Header header_first = headers.first;
- Header header_odd = headers.odd;
- Header header_even = headers.even;
-
- #region Header_First
- // Extract Images from the first Header.
- List<Novacode.Image> header_first_images = header_first.Images;
-
- // Make sure there is 1 Image in the first header.
- Assert.IsTrue(header_first_images.Count() == 1);
- #endregion
-
- #region Header_Odd
- // Extract Images from the odd Header.
- List<Novacode.Image> header_odd_images = header_odd.Images;
-
- // Make sure there is 1 Image in the first header.
- Assert.IsTrue(header_odd_images.Count() == 1);
- #endregion
-
- #region Header_Even
- // Extract Images from the odd Header.
- List<Novacode.Image> header_even_images = header_even.Images;
-
- // Make sure there is 1 Image in the first header.
- Assert.IsTrue(header_even_images.Count() == 1);
- #endregion
- }
- }
-
- [TestMethod]
- public void Test_Insert_Picture()
- {
- // Load test document.
- using (DocX document = DocX.Create(directory_documents + "Test.docx"))
- {
- // Add Headers and Footers into this document.
- document.AddHeaders();
- document.AddFooters();
- document.DifferentFirstPage = true;
- document.DifferentOddAndEvenPages = true;
-
- // Add an Image to this document.
- Novacode.Image img = document.AddImage(directory_documents + "purple.png");
-
- // Create a Picture from this Image.
- Picture pic = img.CreatePicture();
-
- // Main document.
- Paragraph p0 = document.InsertParagraph("Hello");
- p0.InsertPicture(pic, 3);
-
- // Header first.
- Paragraph p1 = document.Headers.first.InsertParagraph("----");
- p1.InsertPicture(pic, 2);
-
- // Header odd.
- Paragraph p2 = document.Headers.odd.InsertParagraph("----");
- p2.InsertPicture(pic, 2);
-
- // Header even.
- Paragraph p3 = document.Headers.even.InsertParagraph("----");
- p3.InsertPicture(pic, 2);
-
- // Footer first.
- Paragraph p4 = document.Footers.first.InsertParagraph("----");
- p4.InsertPicture(pic, 2);
-
- // Footer odd.
- Paragraph p5 = document.Footers.odd.InsertParagraph("----");
- p5.InsertPicture(pic, 2);
-
- // Footer even.
- Paragraph p6 = document.Footers.even.InsertParagraph("----");
- p6.InsertPicture(pic, 2);
-
- // Save this document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_Insert_Hyperlink()
- {
- // Load test document.
- using (DocX document = DocX.Create(directory_documents + "Test.docx"))
- {
- // Add Headers and Footers into this document.
- document.AddHeaders();
- document.AddFooters();
- document.DifferentFirstPage = true;
- document.DifferentOddAndEvenPages = true;
-
- // Add a Hyperlink into this document.
- Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
-
- // Main document.
- Paragraph p0 = document.InsertParagraph("Hello");
- p0.InsertHyperlink(h, 3);
-
- // Header first.
- Paragraph p1 = document.Headers.first.InsertParagraph("----");
- p1.InsertHyperlink(h, 3);
-
- // Header odd.
- Paragraph p2 = document.Headers.odd.InsertParagraph("----");
- p2.InsertHyperlink(h, 3);
-
- // Header even.
- Paragraph p3 = document.Headers.even.InsertParagraph("----");
- p3.InsertHyperlink(h, 3);
-
- // Footer first.
- Paragraph p4 = document.Footers.first.InsertParagraph("----");
- p4.InsertHyperlink(h, 3);
-
- // Footer odd.
- Paragraph p5 = document.Footers.odd.InsertParagraph("----");
- p5.InsertHyperlink(h, 3);
-
- // Footer even.
- Paragraph p6 = document.Footers.even.InsertParagraph("----");
- p6.InsertHyperlink(h, 3);
-
- // Save this document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_Append_Hyperlink()
- {
- // Load test document.
- using (DocX document = DocX.Create(directory_documents + "Test.docx"))
- {
- // Add Headers and Footers into this document.
- document.AddHeaders();
- document.AddFooters();
- document.DifferentFirstPage = true;
- document.DifferentOddAndEvenPages = true;
-
- // Add a Hyperlink to this document.
- Hyperlink h = document.AddHyperlink("google", new Uri("http://www.google.com"));
-
- // Main document.
- Paragraph p0 = document.InsertParagraph("----");
- p0.AppendHyperlink(h);
- Assert.IsTrue(p0.Text == "----google");
-
- // Header first.
- Paragraph p1 = document.Headers.first.InsertParagraph("----");
- p1.AppendHyperlink(h);
- Assert.IsTrue(p1.Text == "----google");
-
- // Header odd.
- Paragraph p2 = document.Headers.odd.InsertParagraph("----");
- p2.AppendHyperlink(h);
- Assert.IsTrue(p2.Text == "----google");
-
- // Header even.
- Paragraph p3 = document.Headers.even.InsertParagraph("----");
- p3.AppendHyperlink(h);
- Assert.IsTrue(p3.Text == "----google");
-
- // Footer first.
- Paragraph p4 = document.Footers.first.InsertParagraph("----");
- p4.AppendHyperlink(h);
- Assert.IsTrue(p4.Text == "----google");
-
- // Footer odd.
- Paragraph p5 = document.Footers.odd.InsertParagraph("----");
- p5.AppendHyperlink(h);
- Assert.IsTrue(p5.Text == "----google");
-
- // Footer even.
- Paragraph p6 = document.Footers.even.InsertParagraph("----");
- p6.AppendHyperlink(h);
- Assert.IsTrue(p6.Text == "----google");
-
- // Save the document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_Append_Picture()
- {
- // Create test document.
- using (DocX document = DocX.Create(directory_documents + "Test.docx"))
- {
- // Add Headers and Footers into this document.
- document.AddHeaders();
- document.AddFooters();
- document.DifferentFirstPage = true;
- document.DifferentOddAndEvenPages = true;
-
- // Add an Image to this document.
- Novacode.Image img = document.AddImage(directory_documents + "purple.png");
-
- // Create a Picture from this Image.
- Picture pic = img.CreatePicture();
-
- // Main document.
- Paragraph p0 = document.InsertParagraph();
- p0.AppendPicture(pic);
-
- // Header first.
- Paragraph p1 = document.Headers.first.InsertParagraph();
- p1.AppendPicture(pic);
-
- // Header odd.
- Paragraph p2 = document.Headers.odd.InsertParagraph();
- p2.AppendPicture(pic);
-
- // Header even.
- Paragraph p3 = document.Headers.even.InsertParagraph();
- p3.AppendPicture(pic);
-
- // Footer first.
- Paragraph p4 = document.Footers.first.InsertParagraph();
- p4.AppendPicture(pic);
-
- // Footer odd.
- Paragraph p5 = document.Footers.odd.InsertParagraph();
- p5.AppendPicture(pic);
-
- // Footer even.
- Paragraph p6 = document.Footers.even.InsertParagraph();
- p6.AppendPicture(pic);
-
- // Save the document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_Move_Picture_Load()
- {
- // Load test document.
- using (DocX document = DocX.Load(directory_documents + "MovePicture.docx"))
- {
- // Extract the first Picture from the first Paragraph.
- Picture picture = document.Paragraphs.First().Pictures.First();
-
- // Move it into the first Header.
- Header header_first = document.Headers.first;
- header_first.Paragraphs.First().AppendPicture(picture);
-
- // Move it into the even Header.
- Header header_even = document.Headers.even;
- header_even.Paragraphs.First().AppendPicture(picture);
-
- // Move it into the odd Header.
- Header header_odd = document.Headers.odd;
- header_odd.Paragraphs.First().AppendPicture(picture);
-
- // Move it into the first Footer.
- Footer footer_first = document.Footers.first;
- footer_first.Paragraphs.First().AppendPicture(picture);
-
- // Move it into the even Footer.
- Footer footer_even = document.Footers.even;
- footer_even.Paragraphs.First().AppendPicture(picture);
-
- // Move it into the odd Footer.
- Footer footer_odd = document.Footers.odd;
- footer_odd.Paragraphs.First().AppendPicture(picture);
-
- // Save this as MovedPicture.docx
- document.SaveAs(directory_documents + "MovedPicture.docx");
- }
- }
-
- [TestMethod]
- public void Test_Paragraph_InsertHyperlink()
- {
- // Create a new document
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Add a Hyperlink to this document.
- Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
-
- // Simple
- Paragraph p1 = document.InsertParagraph("AC");
- p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
- p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
- p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
-
- // Difficult
- Paragraph p2 = document.InsertParagraph("\tA\tC\t");
- p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
- p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
- p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
-
- // Contrived
- // Add a contrived Hyperlink to this document.
- Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
- Paragraph p3 = document.InsertParagraph("\tA\tC\t");
- p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
- p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
- p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
- }
- }
-
- [TestMethod]
- public void Test_Paragraph_RemoveHyperlink()
- {
- // Create a new document
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Add a Hyperlink to this document.
- Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
-
- // Simple
- Paragraph p1 = document.InsertParagraph("AC");
- p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
- p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
- p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
-
- // Try and remove a Hyperlink using a negative index.
- // This should throw an exception.
- try
- {
- p1.RemoveHyperlink(-1);
- Assert.Fail();
- }
- catch (ArgumentException) { }
- catch (Exception) { Assert.Fail(); }
-
- // Try and remove a Hyperlink at an index greater than the last.
- // This should throw an exception.
- try
- {
- p1.RemoveHyperlink(3);
- Assert.Fail();
- }
- catch (ArgumentException) {}
- catch (Exception) { Assert.Fail(); }
-
- p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
- p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
- p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
- }
- }
-
- [TestMethod]
- public void Test_Paragraph_ReplaceText()
- {
- // Create a new document
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Simple
- Paragraph p1 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
- p1.ReplaceText("Apple", "Orange"); Assert.IsTrue(p1.Text == "Orange Pear Orange Orange Pear Orange");
- p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
- p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
-
- // Try and replace text that dosen't exist in the Paragraph.
- string old = p1.Text;
- p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
-
- // Difficult
- Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
- p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
- p2.ReplaceText("\tApple\tApple", ""); Assert.IsTrue(p2.Text == "Apple\tPear\tPear\tApple");
- p2.ReplaceText("Apple\tPear\t", ""); Assert.IsTrue(p2.Text == "Pear\tApple");
- p2.ReplaceText("Pear\tApple", ""); Assert.IsTrue(p2.Text == "");
- }
- }
-
- [TestMethod]
- public void Test_Paragraph_RemoveText()
- {
- // Create a new document
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Simple
- //<p>
- // <r><t>HellWorld</t></r>
- //</p>
- Paragraph p1 = document.InsertParagraph("HelloWorld");
- p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld");
- p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
- p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
-
- // Try and remove text at an index greater than the last.
- // This should throw an exception.
- try
- {
- p1.RemoveText(p1.Text.Length, 1);
- Assert.Fail();
- }
- catch (ArgumentOutOfRangeException) { }
- catch (Exception) { Assert.Fail(); }
-
- // Try and remove text at a negative index.
- // This should throw an exception.
- try
- {
- p1.RemoveText(-1, 1);
- Assert.Fail();
- }
- catch (ArgumentOutOfRangeException) { }
- catch (Exception) { Assert.Fail(); }
-
- // Difficult
- //<p>
- // <r><t>A</t></r>
- // <r><t>B</t></r>
- // <r><t>C</t></r>
- //</p>
- Paragraph p2 = document.InsertParagraph("A\tB\tC");
- p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC");
- p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t");
- p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t");
- p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t");
- p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "");
-
- // Contrived 1
- //<p>
- // <r>
- // <t>A</t>
- // <t>B</t>
- // <t>C</t>
- // </r>
- //</p>
- Paragraph p3 = document.InsertParagraph("");
- p3.Xml = XElement.Parse
- (
- @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
- <w:pPr />
- <w:r>
- <w:rPr />
- <w:t>A</w:t>
- <w:t>B</w:t>
- <w:t>C</w:t>
- </w:r>
- </w:p>"
- );
-
- p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "BC");
- p3.RemoveText(p3.Text.Length - 1, 1); Assert.IsTrue(p3.Text == "B");
- p3.RemoveText(0, 1); Assert.IsTrue(p3.Text == "");
-
- // Contrived 2
- //<p>
- // <r>
- // <t>A</t>
- // <t>B</t>
- // <t>C</t>
- // </r>
- //</p>
- Paragraph p4 = document.InsertParagraph("");
- p4.Xml = XElement.Parse
- (
- @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
- <w:pPr />
- <w:r>
- <w:rPr />
- <tab />
- <w:t>A</w:t>
- <tab />
- </w:r>
- <w:r>
- <w:rPr />
- <tab />
- <w:t>B</w:t>
- <tab />
- </w:r>
- </w:p>"
- );
-
- p4.RemoveText(0, 1); Assert.IsTrue(p4.Text == "A\t\tB\t");
- p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "A\tB\t");
- p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A\tB");
- p4.RemoveText(1, 1); Assert.IsTrue(p4.Text == "AB");
- p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "A");
- p4.RemoveText(p4.Text.Length - 1, 1); Assert.IsTrue(p4.Text == "");
- }
- }
-
- [TestMethod]
- public void Test_Paragraph_InsertText()
- {
- // Create a new document
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Simple
- //<p>
- // <r><t>HelloWorld</t></r>
- //</p>
- Paragraph p1 = document.InsertParagraph("HelloWorld");
- p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld");
- p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
- p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
-
- // Try and insert text at an index greater than the last + 1.
- // This should throw an exception.
- try
- {
- p1.InsertText(p1.Text.Length + 1, "-");
- Assert.Fail();
- }
- catch (ArgumentOutOfRangeException) { }
- catch (Exception) { Assert.Fail(); }
-
- // Try and insert text at a negative index.
- // This should throw an exception.
- try
- {
- p1.InsertText(-1, "-");
- Assert.Fail();
- }
- catch (ArgumentOutOfRangeException) { }
- catch (Exception) { Assert.Fail(); }
-
- // Difficult
- //<p>
- // <r><t>A</t></r>
- // <r><t>B</t></r>
- // <r><t>C</t></r>
- //</p>
- Paragraph p2 = document.InsertParagraph("A\tB\tC");
- p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC");
- p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-");
- p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-");
- p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-");
-
- // Contrived 1
- //<p>
- // <r>
- // <t>A</t>
- // <t>B</t>
- // <t>C</t>
- // </r>
- //</p>
- Paragraph p3 = document.InsertParagraph("");
- p3.Xml = XElement.Parse
- (
- @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
- <w:pPr />
- <w:r>
- <w:rPr />
- <w:t>A</w:t>
- <w:t>B</w:t>
- <w:t>C</w:t>
- </w:r>
- </w:p>"
- );
-
- p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC");
- p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-");
- p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-");
- p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-");
-
- // Contrived 2
- //<p>
- // <r>
- // <t>A</t>
- // <t>B</t>
- // <t>C</t>
- // </r>
- //</p>
- Paragraph p4 = document.InsertParagraph("");
- p4.Xml = XElement.Parse
- (
- @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
- <w:pPr />
- <w:r>
- <w:rPr />
- <w:t>A</w:t>
- <w:t>B</w:t>
- <w:t>C</w:t>
- </w:r>
- </w:p>"
- );
-
- p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC");
- p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t");
- p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t");
- p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t");
- }
- }
-
- [TestMethod]
- public void Test_Document_Paragraphs()
- {
- // Load the document 'Paragraphs.docx'
- using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
- {
- // Extract the Paragraphs from this document.
- List<Paragraph> paragraphs = document.Paragraphs;
-
- // There should be 3 Paragraphs in this document.
- Assert.IsTrue(paragraphs.Count() == 3);
-
- // Extract the 3 Paragraphs.
- Paragraph p1 = paragraphs[0];
- Paragraph p2 = paragraphs[1];
- Paragraph p3 = paragraphs[2];
-
- // Extract their Text properties.
- string p1_text = p1.Text;
- string p2_text = p2.Text;
- string p3_text = p3.Text;
-
- // Test their Text properties against absolutes.
- Assert.IsTrue(p1_text == "Paragraph 1");
- Assert.IsTrue(p2_text == "Paragraph 2");
- Assert.IsTrue(p3_text == "Paragraph 3");
-
- // Its important that each Paragraph knows the PackagePart it belongs to.
- document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));
-
- // Test the saving of the document.
- document.SaveAs(file_temp);
- }
-
- // Delete the tempory file.
- File.Delete(file_temp);
- }
-
- [TestMethod]
- public void Test_Table_mainPart_bug9526()
- {
- using (DocX document = DocX.Create("test.docx"))
- {
- Hyperlink h = document.AddHyperlink("follow me", new Uri("http://www.google.com"));
- Table t = document.AddTable(2, 3);
- int cc = t.ColumnCount;
-
- Paragraph p = t.Rows[0].Cells[0].Paragraphs[0];
- p.AppendHyperlink(h);
- }
- }
-
- [TestMethod]
- public void Test_Table_InsertRow()
- {
- using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
- {
- // Add a Table to a document.
- Table t = document.AddTable(2, 2);
- t.Design = TableDesign.TableGrid;
-
- // Insert the Table into the main section of the document.
- Table t1 = document.InsertTable(t);
- t1.InsertRow();
- t1.InsertColumn();
-
- // Save the document.
- document.Save();
- }
- }
-
- [TestMethod]
- public void Test_Document_ApplyTemplate()
- {
- using (MemoryStream documentStream = new MemoryStream())
- {
- using (DocX document = DocX.Create(documentStream))
- {
- document.ApplyTemplate(directory_documents + "Template.dotx");
- document.Save();
- Header firstHeader = document.Headers.first;
- Header oddHeader = document.Headers.odd;
- Header evenHeader = document.Headers.even;
-
- Footer firstFooter = document.Footers.first;
- Footer oddFooter = document.Footers.odd;
- Footer evenFooter = document.Footers.even;
-
- Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
- Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
-
- Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
- Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
-
- Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
- Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
-
- Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
- Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
-
- Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
- Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
-
- Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
- Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
-
- Paragraph firstParagraph = document.Paragraphs[0];
- Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
- }
- }
- }
- }
- }
|