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
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
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
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
thanks for your example code. It works perfectly.
Best regards
Li
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
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
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
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
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