Hello,
Thanks for your inquiry.
Do you want to insert the bookmark at a special location of the document, and then add content to the bookmark? If so, please refer to the following code.
- Code: Select all
Document document = new Document();
document.LoadFromFile("input.docx");
Section section1 = document.Sections[0];
Paragraph para = section1.Paragraphs[0];
//Add bookmark
para.AppendBookmarkStart("bookmark");
para.AppendText("bookmark");
para.AppendBookmarkEnd("bookmark");
BookmarksNavigator bn = new BookmarksNavigator(document);
//Find bookmark by name
bn.MoveToBookmark("bookmark", true, true);
//The content to be inserted
Stream stream = new FileStream("InsertContent.docx", FileMode.Open);
Document insertContent = new Document();
insertContent.LoadFromStream(stream, FileFormat.Auto);
//Create a TextBodyPart instance and add the content to it
TextBodyPart part = new TextBodyPart(document);
foreach (Section section2 in insertContent.Sections)
{
foreach (Paragraph paragraph in section2.Paragraphs)
{
part.BodyItems.Add(paragraph.Clone());
}
}
//Replace bookmark content
bn.ReplaceBookmarkContent(part);
document.SaveToFile("output.docx", FileFormat.Docx);
If this is not what you want, to help us better understand your requirement, please share your input file as well as your expected Word file with us. You could upload them here or send them to us(
support@e-iceblue.com) via email.
Sincerely,
Rachel
E-iceblue support team