Hi,
Thanks for your reply.
After initial investigation, I found the difference between the file “19432result.docx” and the file “expectation.docx” was caused by the different paragraph spacing and different font size.
Since there are default paragraph styles in your template Word file, but there are no paragraph style settings in your Html string, when you insert the paragraph into the template file, the paragraph style in your template file will be added into the Html string automatically.
You could set the paragraph spacing and the font size as you want when insert the paragraphs into the bookmark. Below is the code for you.
- Code: Select all
string HTML = System.IO.File.ReadAllText(@"..\19423html.txt");
//Load the file
Document document = new Document();
document.LoadFromFile(@"..\filename.doc");
//Add a section and name it section0
Section section0 = document.AddSection();
//Add a paragraph for section0
Paragraph paragraph = section0.AddParagraph();
//Append your html string
paragraph.AppendHTML(HTML);
BookmarksNavigator bn = new BookmarksNavigator(document);
bn.MoveToBookmark("Strategy_Commentary");
//set the font size and paragraph spacing of your insert paragraph
ParagraphStyle style = new ParagraphStyle(document);
style.Name = "test";
style.CharacterFormat.FontSize = 10.5f;
style.ParagraphFormat.LineSpacing = 8f;
style.ParagraphFormat.BeforeSpacing = 0f;
style.ParagraphFormat.AfterSpacing = 7.5f;
document.Styles.Add(style);
//traverse through all body child objects of the section0
for (int i = section0.Body.ChildObjects.Count - 1; i >= 0; i--)
{
DocumentObject obj = section0.Body.ChildObjects[i];
//if it is a paragraph
if (obj is Paragraph)
{
Paragraph para = obj as Paragraph;
para.ApplyStyle(style.Name);
//insert the paragraph into bookmark
bn.InsertParagraph(para);
}
}
//Remove the first empty line
bn.CurrentBookmark.BookmarkStart.Owner.Owner.ChildObjects.RemoveAt(0);
//remove the section0
document.Sections.Remove(section0);
document.SaveToFile("result.docx", FileFormat.Docx);
If this doesn't solve your issue, please provide your input Word file. In the attachment in the last post, there is no Word template "Bookmark.doc".
Best wishes,
Amber
E-iceblue support team