Move Cursor to Any Character Position in any Paragraph of Word Document & then insert Text Images there using VB.NET

Hi, All,

Who can tell me how to use Words.dll to move cursor to any given position in VB.net besides the particular positions such as Head/End position of the document or a paragraph?
For moving the cursor to particular position, it can be done by using builder.MoveToDocumentEnd, builder.MoveToDocumenttStart, builder.MoveToParagraph.
But I expect the cursor can be moved and placed at a given position, for example, Builder.MoveDocCursorTo(PositionIndex In WholeDocument), here Position is any number. Or Builder.MoveParagraphCursorTo(ParagraphIndex In Paragraphs, PositionIndex in Paragraph).
Could Words.dll implements this?
Thanks!

Cheng

Hi Cheng,


Thanks for your inquiry.
jixuecheng:
Builder.MoveDocCursorTo(PositionIndex In WholeDocument), here Position is any number.
Could you please share some more detail about this query? We will then provide you more information on this along with code.
jixuecheng:
Builder.MoveParagraphCursorTo(ParagraphIndex In Paragraphs, PositionIndex in Paragraph)
Perhaps, you’re looking for a way to move to a particular character position in a Paragraph. You could do it using the following method:

DocumentBuilder.MoveToParagraph(int paragraphIndex, int characterIndex);

Using the characterIndex, you can specify the index of the character inside the paragraph. Currently you can only specify 0 to move to the beginning of the paragraph or -1 to move to the end of the paragraph. Your request has been linked to the appropriate issue (WORDSNET-10148) and you will be notified as soon as moving cursor to a particular character position in a Paragraph is supported. Sorry for the inconvenience.

@jixuecheng,

Regarding WORDSNET-10148, you may use the following code as a workaround. Please see these sample input/output Word documents (Docs.zip (17.6 KB)). Hope, this helps:

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

Document doc = new Document(@"E:\\Temp\\in.docx");
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 = "Verdana";
    builder.Font.Size = 16;
    builder.Font.Color = Color.Red;
    builder.Write(text);
}
else
{
    Console.WriteLine("Incorrect character index specified");
}

doc.JoinRunsWithSameFormatting();
doc.Save(@"E:\\Temp\\20.3.docx");

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

We will also inform you via this thread as soon as WORDSNET-10148 will be resolved in future.

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