Fixed a bug with DocX ignoring the CAPS Element.master
| { | { | ||||
| static void Main(string[] args) | static void Main(string[] args) | ||||
| { | { | ||||
| // Create a document. | |||||
| using (DocX document = DocX.Load(@"Test.docx")) | |||||
| // Create a new document. | |||||
| using (DocX document = DocX.Create(@"Test.docx")) | |||||
| { | { | ||||
| document.ReplaceText("Hio", "World"); | |||||
| document.SaveAs("Test2.docx"); | |||||
| }// Release this document from memory. | |||||
| // Add Headers to the document. | |||||
| document.AddHeaders(); | |||||
| // Get the default Header. | |||||
| Header header = document.Headers.odd; | |||||
| // Insert a Paragraph into the Header. | |||||
| Paragraph p0 = header.InsertParagraph(); | |||||
| // Append place holders for PageNumber and PageCount into the Header. | |||||
| // Word will replace these with the correct value foreach Page. | |||||
| p0.Append("Page ("); | |||||
| p0.AppendPageNumber(PageNumberFormat.normal); | |||||
| p0.Append(" of "); | |||||
| p0.AppendPageCount(PageNumberFormat.normal); | |||||
| p0.Append(")"); | |||||
| p0.ReplaceText("Page (", "Monster <"); | |||||
| p0.ReplaceText(")", ">"); | |||||
| // Save the document. | |||||
| document.Save(); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| case "t": | case "t": | ||||
| goto case "delText"; | goto case "delText"; | ||||
| case "delText": | case "delText": | ||||
| return e.Value; | |||||
| { | |||||
| if (e.Parent != null && e.Parent.Name.LocalName == "r") | |||||
| { | |||||
| XElement run = e.Parent; | |||||
| var rPr = run.Elements().FirstOrDefault(a => a.Name.LocalName == "rPr"); | |||||
| if (rPr != null) | |||||
| { | |||||
| var caps = rPr.Elements().FirstOrDefault(a => a.Name.LocalName == "caps"); | |||||
| if (caps != null) | |||||
| return e.Value.ToUpper(); | |||||
| } | |||||
| } | |||||
| return e.Value; | |||||
| } | |||||
| case "tr": | case "tr": | ||||
| goto case "br"; | goto case "br"; | ||||
| case "tc": | case "tc": |
| /// <param name="trackChanges">Flag this insert as a change.</param> | /// <param name="trackChanges">Flag this insert as a change.</param> | ||||
| /// <param name="formatting">The text formatting.</param> | /// <param name="formatting">The text formatting.</param> | ||||
| public void InsertText(int index, string value, bool trackChanges=false, Formatting formatting = null) | public void InsertText(int index, string value, bool trackChanges=false, Formatting formatting = null) | ||||
| { | |||||
| { | |||||
| // Timestamp to mark the start of insert | // Timestamp to mark the start of insert | ||||
| DateTime now = DateTime.Now; | DateTime now = DateTime.Now; | ||||
| DateTime insert_datetime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc); | DateTime insert_datetime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc); | ||||
| insert = CreateEdit(EditType.ins, insert_datetime, newRuns); | insert = CreateEdit(EditType.ins, insert_datetime, newRuns); | ||||
| // Special case to deal with Page Number elements. | // Special case to deal with Page Number elements. | ||||
| if (parentElement.Name.LocalName.Equals("fldSimple")) | |||||
| parentElement.AddBeforeSelf(insert); | |||||
| //if (parentElement.Name.LocalName.Equals("fldSimple")) | |||||
| // parentElement.AddBeforeSelf(insert); | |||||
| else | else | ||||
| { | { | ||||
| // If the formatting matches, do the replace. | // If the formatting matches, do the replace. | ||||
| if(formattingMatch) | if(formattingMatch) | ||||
| { | { | ||||
| var temp = Xml; | |||||
| InsertText(m.Index + oldValue.Length, newValue, trackChanges, newFormatting); | InsertText(m.Index + oldValue.Length, newValue, trackChanges, newFormatting); | ||||
| RemoveText(m.Index, m.Length, trackChanges); | RemoveText(m.Index, m.Length, trackChanges); | ||||
| } | } |
| } | } | ||||
| [TestMethod] | [TestMethod] | ||||
| public void PatternReplacement() | |||||
| public void Test_Pattern_Replacement() | |||||
| { | { | ||||
| Dictionary<string, string> testPatterns = new Dictionary<string, string>() | Dictionary<string, string> testPatterns = new Dictionary<string, string>() | ||||
| { | { | ||||
| using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx")) | using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx")) | ||||
| { | { | ||||
| // Make sure the origional string are the document. | |||||
| Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2); | |||||
| Assert.IsTrue(replaceDoc.FindAll("<Case Number>").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) | foreach (var p in testPatterns) | ||||
| { | |||||
| replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase); | replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase); | ||||
| } | |||||
| replaceDoc.SaveAs(directory_documents + "ReplaceResults.docx"); | |||||
| // 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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