How to set the cursor position in docx using RadFlowDocumentEditor

5 posts, 1 answers
  1. Todd
    Todd avatar
    5 posts
    Member since:
    Oct 2016

    Posted 13 Oct Link to this post

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

  2. Mihail
    Admin
    Mihail avatar
    192 posts

    Posted 14 Oct Link to this post

    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 inline.
    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 inline 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

  3. Todd
    Todd avatar
    5 posts
    Member since:
    Oct 2016

    Posted 14 Oct in reply to Mihail Link to this post

    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.
  4. Todd
    Todd avatar
    5 posts
    Member since:
    Oct 2016

    Posted 14 Oct Link to this post

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

    }

  5. Answer
    Mihail
    Admin
    Mihail avatar
    192 posts

    Posted 17 Oct Link to this post

    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

Back to Top