Move Cursor to any Character Position inside a Paragraph of Word Document & Insert new Formatted Content using C#

Hi, Support,

Is there any method to insert text into the head, end , middle of a line and paragraph, and then format the style of the inserted text such as fontsize,fontcolor?

Thanks for your help.
Ducaisoft

@ducaisoft,

There are different ways to insert content/nodes in Word document. You can either use DOM (Document Object Model) classes or use DocumentBuilder class. Please check the following articles:

Hope, this helps in achieving what you are looking for.

Thanks for your response to my question.

I know how to use DocumentBuilder to write text into Document, but I fail to move and locate the cursor at any given position in a paragraph, would you please provide me a demo to show how it work like this?

---------Original Content in a doc like this ------
The paragraph one : this is a pending question.

The paragraph two: this is the paragraph into which a newtext will be inserted.

---------End Text ------------------

---------New Content in the doc after handled like this ------
The paragraph one : this is a pending question.

The paragraph two: this is the paragraph (hereisthenewtext) into which a newtext will be inserted.

---------End Text ------------------
Please refer to the demo Files.

You must change the file extension ‘zip’ as ‘doc’.

InputFile.zip (25.5 KB)
OutputFile.zip (25.5 KB)

Thanks!

@ducaisoft,

We have logged your requirement in our issue tracking system. Your ticket number is:

WORDSNET-20059: Provide API to move cursor to any position (character etc) inside a Paragraph

We will further look into the details of this requirement and will keep you updated on the status of the linked issue. Sorry for the inconvenience.

As a workaround, you may use the following code;

int paragraphIndex = 4;
int characterIndex = 49;
string text = "demo";

Document doc = new Document(@"E:\\Temp\\InputFile\\InputFile.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

Paragraph targetPara = (Paragraph)builder.CurrentStory.GetChildNodes(NodeType.Paragraph, true)[paragraphIndex];
Node[] runs = targetPara.GetChildNodes(NodeType.Run, true).ToArray();

for (int i = 0; i < runs.Length; i++)
{
    Run run = (Run)runs[i];
    int length = run.Text.Length;

    Run currentNode = run;
    for (int x = 1; x < length; x++)
    {
        currentNode = SplitRun(currentNode, 1);
    }
}

if (characterIndex > 0 && characterIndex < targetPara.Runs.Count)
{
    builder.MoveTo(targetPara.Runs[characterIndex]);
    builder.Font.Name = "Calibri";
    builder.Font.Size = 6;
    builder.Font.Color = Color.Red;
    builder.Write(text);
}
else
{
    Console.WriteLine("Incorrect character index specified");
}

doc.Save(@"E:\\Temp\\InputFile\\20.2.doc");

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), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

Hope, this helps.

Thank you very much!
It works.

@ducaisoft,

Regarding WORDSNET-20059, please upgrade to the latest (21.3) version of Aspose.Words for .NET and try running the following code:

Document doc = new Document(dir + "InputFile.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToParagraph(4, 48);
builder.Write(" ");
builder.Font.Size = 6;
builder.Font.Color = Color.Red;
builder.Write("demo");

doc.Save(dir + "Out.doc");

The DocumentBuilder.MoveToParagraph method now works as expected.

Ok!
Great!
I’ll try it later.
And another ticket: how about the function concerning to deleting rotated text in a pdf?

@ducaisoft,

I can see that we had logged PDFNET-48580 to address this problem. Please follow your other thread for further proceedings.

The issues you have found earlier (filed as WORDSNET-20059) have been fixed in this Aspose.Words for .NET 21.8 update and this Aspose.Words for Java 21.8 update.