소스 검색

Fixed a bug with ReplaceText.

Fixed a bug with DocX ignoring the CAPS Element.
master
coffeycathal_cp 15 년 전
부모
커밋
fae1cfe176
4개의 변경된 파일65개의 추가작업 그리고 13개의 파일을 삭제
  1. 25
    5
      ConsoleApplication3/Program.cs
  2. 16
    1
      DocX/HelperFunctions.cs
  3. 4
    3
      DocX/Paragraph.cs
  4. 20
    4
      UnitTests/UnitTest1.cs

+ 25
- 5
ConsoleApplication3/Program.cs 파일 보기

@@ -11,12 +11,32 @@ namespace ConsoleApplication3
{
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();
}
}
}
}

+ 16
- 1
DocX/HelperFunctions.cs 파일 보기

@@ -76,7 +76,22 @@ namespace Novacode
case "t":
goto 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":
goto case "br";
case "tc":

+ 4
- 3
DocX/Paragraph.cs 파일 보기

@@ -1719,7 +1719,7 @@ namespace Novacode
/// <param name="trackChanges">Flag this insert as a change.</param>
/// <param name="formatting">The text formatting.</param>
public void InsertText(int index, string value, bool trackChanges=false, Formatting formatting = null)
{
{
// Timestamp to mark the start of insert
DateTime now = DateTime.Now;
DateTime insert_datetime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc);
@@ -1804,8 +1804,8 @@ namespace Novacode
insert = CreateEdit(EditType.ins, insert_datetime, newRuns);
// 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
{
@@ -3075,6 +3075,7 @@ namespace Novacode
// If the formatting matches, do the replace.
if(formattingMatch)
{
var temp = Xml;
InsertText(m.Index + oldValue.Length, newValue, trackChanges, newFormatting);
RemoveText(m.Index, m.Length, trackChanges);
}

+ 20
- 4
UnitTests/UnitTest1.cs 파일 보기

@@ -38,7 +38,7 @@ namespace UnitTests
}
[TestMethod]
public void PatternReplacement()
public void Test_Pattern_Replacement()
{
Dictionary<string, string> testPatterns = new Dictionary<string, string>()
{
@@ -48,12 +48,28 @@ namespace UnitTests
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)
{
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\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\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");
}
}

Loading…
취소
저장