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

Import docx docmarks into RadRichTextBox and jump to bookmark

11 Answers 175 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Li
Top achievements
Rank 1
Li asked on 08 May 2013, 07:25 AM
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?

11 Answers, 1 is accepted

Sort by
0
Li
Top achievements
Rank 1
answered on 08 May 2013, 10:09 AM
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.
0
Accepted
Stefan
Telerik team
answered on 13 May 2013, 08:30 AM
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?
0
Li
Top achievements
Rank 1
answered on 16 May 2013, 09:51 AM
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
0
Stefan
Telerik team
answered on 20 May 2013, 12:39 PM
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?
0
Li
Top achievements
Rank 1
answered on 04 Jun 2013, 08:36 AM
Hello Stefan,

thanks for your example code. It works perfectly.

Best regards
Li
0
Li
Top achievements
Rank 1
answered on 06 Jun 2013, 11:59 AM
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
0
Stefan
Telerik team
answered on 11 Jun 2013, 10:29 AM
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?
0
Li
Top achievements
Rank 1
answered on 14 Jun 2013, 10:45 AM
I created a support ticket to attach zip file: 704533
0
Stefan
Telerik team
answered on 17 Jun 2013, 08:52 AM
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?
0
Li
Top achievements
Rank 1
answered on 18 Jun 2013, 09:56 AM
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
0
Stefan
Telerik team
answered on 18 Jun 2013, 03:34 PM
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?
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Li
Top achievements
Rank 1
Answers by
Li
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or