public ReplaceAction Replacing(ReplacingArgs e) { //获取当前节点 var node = e.MatchNode; //获取当前文档 Document doc = node.Document as Document; DocumentBuilder builder = new DocumentBuilder(doc); //将光标移动到指定节点 builder.MoveTo(node); //插入图片 builder.InsertHtml(html); return ReplaceAction.Replace; }
/// <summary> /// This method is called by the Aspose.Words find and replace engine for each match. /// This method replaces the match string, even if it spans multiple runs. /// </summary> ReplaceAction IReplacingCallback.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 removing. 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); } // Create Document Buidler and insert text. DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document); builder.MoveTo((Run)runs[runs.Count - 1]); builder.Write(mText); // Now remove all runs in the sequence. foreach (Run run in runs) run.Remove(); // Signal to the replace engine to do nothing because we have already done all what we wanted. return ReplaceAction.Skip; }
/// <summary> /// Finds and splits the match runs and returns them in an ArrayList. /// </summary> public ArrayList FindAndSplitMatchRuns(ReplacingArgs args) { // This is a Run node that contains either the beginning or the complete match. Node currentNode = args.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 (args.MatchOffset > 0) currentNode = SplitRun((Run)currentNode, args.MatchOffset); // This array is used to store all nodes of the match for further removing. ArrayList runs = new ArrayList(); // Find all runs that contain parts of the match string. int remainingLength = args.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); } return runs; }
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args) { // Create a builder to insert the field. DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document); // Move to the first node of the match. builder.MoveTo(args.MatchNode); // If the user specified text to be used in the field as display text then use that, otherwise use the // Match string as the display text. string insertText; if (!string.IsNullOrEmpty(mFieldText)) insertText = mFieldText; else insertText = args.Match.Value; // Insert the TC field before this node using the specified string as the display text and user defined switches. builder.InsertField(string.Format("TC \"{0}\" {1}", insertText, mFieldSwitches)); // We have done what we want so skip replacement. return ReplaceAction.Skip; }
public ReplaceAction Replacing(ReplacingArgs args) { ArrayList runs = FindAndSplitMatchRuns(args); // Create DocumentBuilder which is used to insert the field. DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document); builder.MoveTo((Run)runs[runs.Count - 1]); // Calculate the name of the field from the FieldType enumeration by removing the first instance of "Field" from the text. // This works for almost all of the field types. string fieldName = mFieldType.ToString().ToUpper().Substring(5); // Insert the field into the document using the specified field type and the match text as the field name. // If the fields you are inserting do not require this extra parameter then it can be removed from the string below. builder.InsertField(string.Format("{0} {1}", fieldName, args.Match.Groups[0])); // Now remove all runs in the sequence. foreach (Run run in runs) run.Remove(); // Signal to the replace engine to do nothing because we have already done all what we wanted. return ReplaceAction.Skip; }
/// <summary> /// NOTE: This is a simplistic method that will only work well when the match /// starts at the beginning of a run. /// </summary> ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { DocumentBuilder builder = new DocumentBuilder((Aspose.Words.Document)e.MatchNode.Document); builder.MoveTo(e.MatchNode); // Replace '<CustomerName>' text with a red bold name. builder.InsertHtml("<b><font color='red'>James Bond</font></b>"); e.Replacement = ""; return ReplaceAction.Replace; }
/// <summary> /// This is called during a replace operation each time a match is found. /// This method appends a number to the match string and returns it as a replacement string. /// </summary> ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { e.Replacement = e.Match.ToString() + mMatchNumber.ToString(); mMatchNumber++; return ReplaceAction.Replace; }
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { // This is the run node that contains the found text. Note that the run might contain other // text apart from the URL. All the complexity below is just to handle that. I don't think there // is a simpler way at the moment. Run run = (Run)e.MatchNode; Paragraph para = run.ParentParagraph; string url = e.Match.Value; // We are using \xbf (inverted question mark) symbol for temporary purposes. // Any symbol will do that is non-special and is guaranteed not to be presented in the document. // The purpose is to split the matched run into two and insert a hyperlink field between them. para.Range.Replace(url, "\xbf", true, true); Run subRun = (Run)run.Clone(false); int pos = run.Text.IndexOf("\xbf"); subRun.Text = subRun.Text.Substring(0, pos); run.Text = run.Text.Substring(pos + 1, run.Text.Length - pos - 1); para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), subRun); mBuilder.MoveTo(run); // Specify font formatting for the hyperlink. mBuilder.Font.Color = System.Drawing.Color.Blue; mBuilder.Font.Underline = Underline.Single; // Insert the hyperlink. mBuilder.InsertHyperlink(url, url, false); // Clear hyperlink formatting. mBuilder.Font.ClearFormatting(); // Let's remove run if it is empty. if (run.Text.Equals("")) run.Remove(); // No replace action is necessary - we have already done what we intended to do. return ReplaceAction.Skip; }
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { Document subDoc = new Document(RunExamples.GetDataDir_WorkingWithDocument() + "InsertDocument2.doc"); // Insert a document after the paragraph, containing the match text. Paragraph para = (Paragraph)e.MatchNode.ParentNode; InsertDocument(para, subDoc); // Remove the paragraph with the match text. para.Remove(); return ReplaceAction.Skip; }
public ReplaceAction Replacing(ReplacingArgs e, int width, int height) { //获取当前节点 var node = e.MatchNode; //获取当前文档 Document doc = node.Document as Document; DocumentBuilder builder = new DocumentBuilder(doc); //将光标移动到指定节点 builder.MoveTo(node); //插入图片 builder.InsertImage(url, width, height); return ReplaceAction.Replace; }
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { Aspose.Words.Document subDoc = new Aspose.Words.Document(ExDir + "InsertDocument2.doc"); // Insert a document after the paragraph, containing the match text. Paragraph para = (Paragraph)e.MatchNode.ParentNode; InsertDocument(para, subDoc); // Remove the paragraph with the match text. para.Remove(); return ReplaceAction.Skip; }