We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose Word Find and formating logic is not working for specific documents

Existing Scenario: In one of my application feature I am accepting files as input and concatenating all files in one document , the files may contain some anchor strings like {{Signature}},{{FullName}} which i am using for placing signature controls of Docusign. Since i don’t want to display the string in final document I am using below logic to color the string with background colors .

======Implementation for IreplacingCallBack class =============
public class ReplaceEvaluatorFindAndHighlight : IReplacingCallback
{
///
/// This method is called by the Aspose.Words find and replace engine for each match.
/// This method highlights the match string, even if it spans multiple runs.
///
///
public string StrColor { get; set; }
public ReplaceEvaluatorFindAndHighlight(string strColor)
{
StrColor = strColor;

}
public ReplaceAction Replacing(ReplacingArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;

// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);

// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = remainingLength - currentNode.GetText().Length;

// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
}
while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
}

// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
SplitRun((Run)currentNode, remainingLength);
runs.Add(currentNode);
}

// Now highlight all runs in the sequence.
foreach (Run run in runs)
{
run.Font.Color = System.Drawing.ColorTranslator.FromHtml(StrColor);

}


// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}
===============Call in Document for color replacement =============
Regex regex = new Regex("{{Signature}}", RegexOptions.IgnoreCase);
objCollatedDoc.Range.Replace(regex, new ReplaceEvaluatorFindAndHighlight(strColor), false);

================================Status of above code =======================
The code is working perfectly file for maximum documents but there is specific document attached and used in same sequence for concatenation the replace logic is not working .

Even when i tried single file from the attachment or collated Language1.docx before Downloads.doc the replace logic is working fine .

Can somebody help me with the root cause why such issue i am facing and solution for same .




Hi Malik,

We are sorry for the inconvenience. We will appreciate it if you please share following resources here for testing. We will test the scenario at our end and will guide you accordingly.

  • Your input documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

Best Regards,