Import docx docmarks into RadRichTextBox and jump to bookmark

12 posts, 1 answers
  1. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 08 May 2013 Link to this post

    Is it possible to import a docx Word-document with bookmarks into the RadRichTextBox and then jump to a bookmark that has been set with Word in the docx file? We use the version 2012.2.608.40 of RadRichTextBox.

     

    I created a bookmark “Test1” in Word, saved the document as docx and imported the docx file into a RadRichTextBox. Then I called document.GetBookmarkByName() and document.GetAllBookmarks(). But the bookmark wasn’t found in both methods. See my sample code below.

     

    public void ImportDocx(string filename)

    {

         RadDocument document = null;

         IDocumentFormatProvider provider = new DocxFormatProvider();

         using (FileStream stream = new FileStream(filename, FileMode.Open))

         {

               document = provider.Import(stream);

          }

          radRichTextBox.Document = document;

          BookmarkRangeStart bookmarkRangeStart = document.GetBookmarkByName("Test1");

          document.GoToBookmark("Test1");

    }

     

    Is the import of bookmarks created with Word not supported?

  2. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 08 May 2013 Link to this post

    I have some extra information for you. After calling "radRichTextBox.ShowManageBookmarksDialog()" and adding a new bookmark "abc" the bookmark "Test1" which was created in Word is found. Only open the ManageBookmarksDialog without adding a bookmark does not work.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 13 May 2013 Link to this post

    Hello Li,

    Thank you for writing.

    I have just tested this case and it seems to be working fine on my end. Could you please make sure you are calling the ImportDocx method on Form.Load or later event, as this might be causing such behavior on your end.

    Attached you can find a sample I implemented.

    I hope this helps. Should you have any other questions, do not hesitate to contact us.
     

    All the best,
    Stefan
    the Telerik team
    RadChart for WinForms is obsolete. Now what?
  5. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 16 May 2013 Link to this post

    Hello Stefan,

    many thanks for your solution. I called the import in the constructor. After moving the code into Form.Load() jumping to the bookmark worked.

    But I have another question: It it possible to scroll the document that the bookmark starts at the top of the RadRichTextBox? Actually the last visible line of the RadRichTextBox show the beginning of the bookmark.

    Best regards
    Li
  6. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 20 May 2013 Link to this post

    Hi Li,

    Here is how you can do that:
    public void ImportDocx(string filename)
    {
        RadDocument document = null;
        IDocumentFormatProvider provider = new DocxFormatProvider();
        using (FileStream stream = new FileStream(filename, FileMode.Open))
        {
            document = provider.Import(stream);
        }
        radRichTextBox1.Document = document;
        document.GoToBookmark("test1");
     
        BookmarkRangeStart bookmark = document.GetBookmarkByName("test1");
     
        DocumentPosition documentPosition = this.radRichTextBox1.Document.CaretPosition;
        documentPosition.Reset();
        documentPosition.MoveToInline(bookmark.FirstLayoutBox as InlineLayoutBox, 0);
        double height = documentPosition.GetCurrentInlineBox().ActualHeight;
     
        int value = radRichTextBox1.DocumentView.Scroller.VScrollBar.Value + (int)radRichTextBox1.DocumentView.DesiredSize.Height - (int)height;
        radRichTextBox1.DocumentView.Scroller.VScrollBar.Value = value < radRichTextBox1.DocumentView.Scroller.VScrollBar.Maximum ? value : radRichTextBox1.DocumentView.Scroller.VScrollBar.Maximum;
    }

    I hope you find this useful.
     

    Kind regards,
    Stefan
    the Telerik team
    RadChart for WinForms is obsolete. Now what?
  7. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 04 Jun 2013 Link to this post

    Hello Stefan,

    thanks for your example code. It works perfectly.

    Best regards
    Li
  8. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 06 Jun 2013 Link to this post

    Hello Stefan,

    at first your example with scrolling seems to work for me. But unfortunately if you jump to a bookmark which is not at the end of the RadRichTextBox then the RadRichTextBox is not scrolled correctly. I suppose that your code uses the fact that the boomark is scrolled to the last visible line after jumping to it.

    What can I do to improve the scrolling mechanism so that the beginning of the bookmark is always the first visible line (or the scrollbar is scrolled to the maximum position if the bookmark is at the end of the word document)?

    Best regards
    Li
  9. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 11 Jun 2013 Link to this post

    Hello Li,

    I am not quite sure I understand what do you mean by "if you jump to a bookmark which is not at the end of the RadRichTextBox". The bookmark in the document which I have send it not at the end of the document, it is somewhere in the middle of it and the code seems to be working correctly. Would you please modify the attached project on a way to replicate the undesired behavior and get back to me with it, so I can investigate it and help you resolve it.

    Thank you in advance for your time and cooperation.

    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  10. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 14 Jun 2013 Link to this post

    I created a support ticket to attach zip file: 704533
  11. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 17 Jun 2013 Link to this post

    Hello,

    The case was that when the first bookmark is within the first page of the document (less than the size of the document), the logic did not place the first bookmark at the first line. The attached solution aims to resolve this casel. Please let me know how it works for you.

    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  12. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 18 Jun 2013 Link to this post

    Hello Stefan,

    With an addition to make it work also after jumping around in the document, the code now works as desired.

    Thank you very much for your help solving this issue!

    Kind regards,
    Li
  13. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 18 Jun 2013 Link to this post

    Hi Li,

    I am glad everything is now OK on your end. If you wish, you can post the modification you introduced here so that the community can benefit from it.

    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
Back to Top
UI for WinForms is Visual Studio 2017 Ready