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

Free Support Forum - aspose.com

How to set cursor?

Hi, I have in my dotx file text “[Put_content_here]”. I want to remove it and start build my document from this place. How can I do it?

Hi there,


Thanks for your inquiry. Please refer to the following articles:
Find and Replace
Move the cursor

Please use following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + “in.docx”);<o:p></o:p>

FindandInsertText findtext = new FindandInsertText();

Regex regex = new Regex("\\[Put content here\\]", RegexOptions.IgnoreCase);

FindReplaceOptions options = new FindReplaceOptions();

options.ReplacingCallback = findtext;

doc.Range.Replace(regex, "", options);

doc.Save(MyDir + "Out.docx");

class FindandInsertText : IReplacingCallback

{

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);

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);

}

DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

builder.MoveTo((Run)runs[0]);

builder.MoveToBookmark("bookmark");

builder.Write(@"Aspose.Words supports DOC, DOCX, RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats. With Aspose.Words you can generate, modify, convert, render and print documents without utilizing Microsoft Word®.");

// Now remove the matched text

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;

}

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;

}

}