How to add a list to an existing named bookmark in a template

1 Answer 88 Views
WordsProcessing
Joost
Top achievements
Rank 1
Joost asked on 22 Jul 2024, 09:52 AM

Hi there,

I am trying to generate a list on the position of a named bookmark in an template.

The list is generated and looks fine, but is a positioned at the very end of the document.

The code looks very similar to my code for generating a  table on the position of a bookmark, but for a list it does not work.

Does anyone kwows what is going wrong? Is a section always generated at the very end of the document?

Thank for your help!

 

My code:

var bookmark = bookmarks.FirstOrDefault(b => b.Bookmark.Name == "PaymentMilestones");

if (bookmark is not null)
{
editor.MoveToInlineEnd(bookmark.Bookmark.BookmarkRangeStart);

var section = editor.InsertSection();

List list = document.Lists.Add(ListTemplateType.BulletDefault);

if (purchaseOrderPaymentMilestones is not null && purchaseOrderPaymentMilestones.Any())
{
foreach (var purchaseOrderPaymentMilestone in purchaseOrderPaymentMilestones)
{
var paragraph = section.Blocks.AddParagraph();
paragraph.Inlines.AddRun($"{purchaseOrderPaymentMilestone.Percentage.ToString("N0")}% {purchaseOrderPaymentMilestone.PaymentMilestone.FullDescription}");
paragraph.ListId = list.Id;
paragraph.ListLevel = 0;
}
}
}

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 22 Jul 2024, 12:42 PM

Hi Joost,

You are experiencing these results because when inserting elements with the RadFlowDocumentEditor, the editor is automatically moved to the position of the inserted element.

In this case, the editor is correctly positioned at the bookmark, but then a new Section is added and the editor is moved to its position. That said, the editor and its positioning are still ignored and not being used because the list paragraphs are being added directly to the Section:

var paragraph = section.Blocks.AddParagraph();

To utilize the position of the editor you need to use it instead to add the paragraphs:

var paragraph = editor.InsertParagraph();

Still, the editor would still be in the wrong (section) position, so you need to move it back to the bookmark.

Example:

var editor = new RadFlowDocumentEditor(document);

var bookmarks = document.EnumerateChildrenOfType<BookmarkRangeEnd>().ToList();
var bookmarkEnd = bookmarks.FirstOrDefault(b => b.Bookmark.Name == "BM1");

editor.MoveToInlineEnd(bookmarkEnd);

List list = document.Lists.Add(ListTemplateType.BulletDefault);

editor.MoveToInlineEnd(bookmarkEnd);

for (int i = 0; i < 4; i++)
{
    var paragraph = editor.InsertParagraph();
    paragraph.Inlines.AddRun($"new text{i}");

    paragraph.ListId = list.Id;
    paragraph.ListLevel = 0;
    editor.MoveToParagraphEnd(paragraph);
}

Results:


I hope this helps.

Regards,
Yoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Joost
Top achievements
Rank 1
commented on 22 Jul 2024, 01:11 PM

Thanks it works for me!
Tags
WordsProcessing
Asked by
Joost
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or