using System; using System.Collections.Generic; namespace Kreta.Job.Tasks.Helpers.Iktatas { public class WordIktatoszamVonalkodTextReplacer : Aspose.Words.Replacing.IReplacingCallback { public List nodes = new List(); Aspose.Words.Replacing.ReplaceAction Aspose.Words.Replacing.IReplacingCallback.Replacing(Aspose.Words.Replacing.ReplacingArgs e) { // This is a Run node that contains either the beginning or the complete match. Aspose.Words.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((Aspose.Words.Run)currentNode, e.MatchOffset); System.Collections.ArrayList runs = new System.Collections.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 -= 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 != Aspose.Words.NodeType.Run)); } // Split the last run that contains the match if there is any text left. if ((currentNode != null) && (remainingLength > 0)) { SplitRun((Aspose.Words.Run)currentNode, remainingLength); runs.Add(currentNode); } String runText = ""; foreach (Aspose.Words.Run run in runs) runText += run.Text; ((Aspose.Words.Run)runs[0]).Text = runText; for (int i = 1; i < runs.Count; i++) { ((Aspose.Words.Run)runs[i]).Remove(); } ((Aspose.Words.Run)runs[0]).Font.Color = System.Drawing.Color.Black; if (e.Match.Value == "{vonalkod}") { ((Aspose.Words.Run)runs[0]).Font.Name = "Code 128"; } nodes.Add((Aspose.Words.Run)runs[0]); return Aspose.Words.Replacing.ReplaceAction.Replace; } private static Aspose.Words.Run SplitRun(Aspose.Words.Run run, int position) { Aspose.Words.Run afterRun = (Aspose.Words.Run)run.Clone(true); afterRun.Text = run.Text.Substring(position); run.Text = run.Text.Substring(0, position); run.ParentNode.InsertAfter(afterRun, run); return afterRun; } } }