This is a migrated thread and some comments may be shown as answers.

How to set the cursor position in docx using RadFlowDocumentEditor

4 Answers 611 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Todd
Top achievements
Rank 1
Todd asked on 14 Oct 2016, 01:52 AM

I am starting with an existing document, which is attached, and trying to figure out how to correctly set the position so I can start adding text at a certain point in the document.  The key is how do you find that point so the RadFlowDocumentEditor can begin adding and creating tables in the document.  I am not trying to do a mail merge.  All I want to do is get a certain position within the document to begin adding text.  Is there something special you need to do inside of the word document itself?  Here is the code I used to try and hopefully set the position, but it adds text to the top of the document and wipes out the existing text.  If you could provide me a code snippet or some guidance, I would appreciate it.

 

DocxFormatProvider provider = new DocxFormatProvider();
using (Stream input = File.OpenRead("Input.docx"))
{
RadFlowDocument document = provider.Import(input);
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
Run firstRun = document.EnumerateChildrenOfType<Run>().First();
editor.MoveToInlineEnd(firstRun);
editor.InsertText("Hello word!");

using (Stream output = File.OpenWrite("Output.docx"))
{
           provider.Export(document, output);
}
}

4 Answers, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 14 Oct 2016, 10:32 AM
Hello Todd,

You are using the correct approach. I have slightly modified your code and the text is inserted at the end of the first paragraph.
Here is the code that I am using to insert a text after the first .
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
 
// determining the inline we would like to navigate to.
var run = document.EnumerateChildrenOfType<Run>().First();
 
// navigating to the end of the obtained inline
editor.MoveToInlineEnd(run);
 
// creating a Run with text and foreground color.
Run myRun = new Run(document);
myRun.Text = "Hello world";
myRun.ForegroundColor = new ThemableColor(System.Windows.Media.Colors.Red);
 
// inserting the new Run.
editor.InsertInline(myRun);
Attached you can see a screenshot from the result.

As a condition of determining the  you would like to navigate to, you could use the text of a run or a bookmark name. Of course, this should be done according to your scenario.

I hope this information answers your question.

Regards,
Mihail
Telerik by Progress

0
Todd
Top achievements
Rank 1
answered on 14 Oct 2016, 01:08 PM
I want to import the existing document because it contains existing styles that I want to use.  Thus, I have to do an import. I want to find the end of where the red text stops and continue the document at that point as I want to keep that header that is inside of a textbox. So if the goal is to use a bookmark, how do you locate the bookmark through code?  Say the name of the book mark is StartHere. The image that you posted was not able to be displayed.
0
Todd
Top achievements
Rank 1
answered on 14 Oct 2016, 02:36 PM

I tried the code you provided and it does not work at all.  I end up with an empty document.  All of the verbiage that was in the document originally is gone plus "Hello World" is not in the document either.  Is the export not working correctly?  Here is my code:

 

static void Main(string[] args)
{
DocxFormatProvider provider = new DocxFormatProvider();
using (Stream input = File.OpenRead("Original.docx"))
{
RadFlowDocument document = provider.Import(input);
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
// determining the inline we would like to navigate to.
var run = document.EnumerateChildrenOfType<Run>().First();
// navigating to the end of the obtained inline
editor.MoveToInlineEnd(run);
// creating a Run with text and foreground color.
Run myRun = new Run(document);
myRun.Text = "Hello world";
myRun.ForegroundColor = new ThemableColor(System.Windows.Media.Colors.Red);
// inserting the new Run.
editor.InsertInline(myRun);

using (Stream output = File.OpenWrite("OutPut.docx"))
{
provider.Export(document, output);
}

}

0
Accepted
Mihail
Telerik team
answered on 17 Oct 2016, 03:42 PM
Hello Todd,

If you would like to get a bookmark with a specific name you could iterate over all bookmark range starts and compare the bookmark name to the required one. Here is a sample code:
BookmarkRangeStart bookmarkRangeStart = editor.Document.EnumerateChildrenOfType<BookmarkRangeStart>().Where(rangeStart => rangeStart.Bookmark.Name == "StartHere").FirstOrDefault();

Regarding the problem you have encountered with the missing content, was not expected. I have tested the code samples with the latest version and there was no problem. Which version of the assemblies do you use?

The problem with the attached image in my previous post seems to be fixed.

Regards,
Mihail
Telerik by Progress

Tags
WordsProcessing
Asked by
Todd
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Todd
Top achievements
Rank 1
Share this question
or