Dear Wasim,
You're welcome.
I have talked our developer about a method like navigator.ReplaceBookmarkContent("New html string", true) , sorry we can't add such method because it's not friendly, and some customers need to adjust the generated format later. So the current way to replace bookmark content with html string has good control.
For the issue of positioning of replacement, please refer to the following code to keep the original indent of paragraph.
- Code: Select all
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile(input file);
ReplaceBookmarkContent(doc, "TestBookmark", strHTML );
doc.SaveToFile(@"C:\testbookmarkReplaced.docx", FileFormat.Docx);
}
static void ReplaceBookmarkContent(Document doc, string bookmarkName, string newHtmlString)
{
BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark(bookmarkName);
TextBodyPart content = navigator.GetBookmarkContent();
Paragraph paragraph;
float leftIndent = 0;
if (content.BodyItems.Count > 0)
{
if (content.BodyItems[0] is Paragraph)
{
paragraph = (Paragraph)content.BodyItems[0];
leftIndent = paragraph.Format.LeftIndent;
}
}
content.Clear();
Section sec = doc.AddSection();
Paragraph par = sec.AddParagraph();
par.AppendHTML(newHtmlString);
for (int i = 0; i < sec.Body.ChildObjects.Count; i++)
{
if (sec.Body.ChildObjects[i] is Paragraph)
{
paragraph = (Paragraph)sec.Body.ChildObjects[i];
if (paragraph.ListFormat.CurrentListLevel != null)
{
paragraph.Format.LeftIndent = paragraph.ListFormat.CurrentListLevel.TextPosition + leftIndent;
}
else
{
paragraph.Format.LeftIndent += leftIndent;
}
}
content.BodyItems.Add(sec.Body.ChildObjects[i].Clone());
}
navigator.ReplaceBookmarkContent(content);
doc.Sections.Remove(sec);
}
If the code can't work, please provide your source file.Thanks.
Sincerely,
Amy
E-iceblue support team