Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Jan 10, 2020 2:04 am

Hi,

I tried your method and I did not face any spacing issue.

However, what I am trying to do is insert paragraph text(HTML Text) to the template(word document) using bookmark. And when I use bookmark, an empty line is added before the actual text is inserted.

I have attached necessary documents required.

Happy to provide more details if my questions above is not clear enough.

Thanks

alvindutt
 
Posts: 11
Joined: Thu Aug 22, 2019 12:17 am

Fri Jan 10, 2020 11:18 am

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
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Jan 14, 2020 4:48 am

Hi ,

Thanks for the help.

Your code has helped fix the spacing issue on the first line. However, I need spacing to be removed from the last line as well.

Also the spacing that is added between text does not change even when I changed the spacing value to 0.
style.ParagraphFormat.LineSpacing = 0f;
style.ParagraphFormat.BeforeSpacing = 0f;
style.ParagraphFormat.AfterSpacing = 0f;

I have attached a copy of the generated document together with the template.

Thanks a lot for your assistance.

alvindutt
 
Posts: 11
Joined: Thu Aug 22, 2019 12:17 am

Tue Jan 14, 2020 10:03 am

Hi,

Thanks for your inquiry.
Please set the "AfterAutoSpacing" and "BeforeAutoSpacing" properties as false when you set the paragraph spacing. Below is the complete code for you.
Code: Select all
            string HTML = System.IO.File.ReadAllText(@"..\19423html.txt");
            Document document = new Document();
            document.LoadFromFile(@"..\Bookmark.doc");

            //set the font size of your insert paragraph as you want
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "test";
            style.CharacterFormat.FontSize = 10.5f;
            style.ParagraphFormat.LineSpacing = 10f;
            document.Styles.Add(style);

            //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");

            //traverse through all body child objects of the section0
            for (int i = section0.Body.ChildObjects.Count - 1; i >= 0; i--)
            {
                //if it is a paragraph
                DocumentObject obj = section0.Body.ChildObjects[i];
                if (obj is Paragraph)
                {
                    Paragraph para = obj as Paragraph;
                    //set the spacing of the insert paragraphs
                    para.Format.BeforeSpacing = 0;
                    para.Format.AfterSpacing = 0;
                    para.Format.AfterAutoSpacing = false;
                    para.Format.BeforeAutoSpacing = false;
                    para.ApplyStyle(style.Name);

                    //insert the paragraph into bookmark           
                    bn.InsertParagraph(para);
                }
            }

            //remove the section0
            document.Sections.Remove(section0);

            //remove the first empty line in the bookmark
            bn.CurrentBookmark.BookmarkStart.Owner.Owner.ChildObjects.RemoveAt(0);

            //remove the last empty line in the bookmark
            int lastIndex = bn.CurrentBookmark.BookmarkEnd.Owner.Owner.ChildObjects.Count - 1;
            bn.CurrentBookmark.BookmarkEnd.Owner.Owner.ChildObjects.RemoveAt(lastIndex);

            //save to file
            document.SaveToFile("result.docx", FileFormat.Docx);

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Feb 25, 2020 4:47 am

Hi,

Thanks for your assistance. I needed further help in regards to setting table borders.

Currently, when table is inserted via bookmark, there are no borders set to the table which makes the pdf form looks untidy. How do I set up all border or outside border to the table when it is inserted via bookmark.

Below is my code.
public bool ConvertHtmlString(BookmarksNavigator bookmarkNavigator, Document document, string htmlstring, string BookmarkName)
{
//return true;
bool blnConverted = false;
bookmarkNavigator.MoveToBookmark(BookmarkName);
//Add a section and name it section0
Section section0 = document.AddSection();
//Add a paragraph for section0
Paragraph paragraph = section0.AddParagraph();
//Append HTML
paragraph.AppendHTML(htmlstring);

//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)
{

//insert the paragraph into bookmark
Paragraph para = obj as Paragraph;
bookmarkNavigator.InsertParagraph(para);
}
//if it is a table
if (obj is Table)
{
//insert the table into the bookmark
Table t = obj as Table;
bookmarkNavigator.InsertTable(t);
}
}

//Remove Section0
document.Sections.Remove(section0);
blnConverted = true;
}

alvindutt
 
Posts: 11
Joined: Thu Aug 22, 2019 12:17 am

Tue Feb 25, 2020 6:59 am

Hi,

Thanks for your post.
Please refer to the following modified code to set the table border.
Code: Select all
        public bool ConvertHtmlString(BookmarksNavigator bookmarkNavigator, Document document, string htmlstring, string BookmarkName)
        {
            //return true;
            bool blnConverted = false;
            bookmarkNavigator.MoveToBookmark(BookmarkName);
            //Add a section and name it section0
            Section section0 = document.AddSection();
            //Add a paragraph for section0
            Paragraph paragraph = section0.AddParagraph();
            //Append HTML
            paragraph.AppendHTML(htmlstring);

            //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)
                {
                    //Insert the paragraph into bookmark
                    Paragraph para = obj as Paragraph;
                    bookmarkNavigator.InsertParagraph(para);
                }
                //If it is a table
                if (obj is Table)
                {
                    //Insert the table into the bookmark
                    Table t = obj as Table;
                    //***************Set border*****************
                    //Set right border of table
                    t.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                    //Set top border of table
                    t.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                    //Set left border of table
                    t.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                    //Set bottom border
                    t.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                    //Set vertical and horizontal border
                    t.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                    t.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                    //***************Set border*****************

                    bookmarkNavigator.InsertTable(t);
                }
            }

            //Remove Section0
            document.Sections.Remove(section0);
            blnConverted = true;
            return blnConverted;
        }

If there is any question, please provide your input file, your html string as well as your desired output for our reference. So that we could provide you with a corresponding solution.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Feb 28, 2020 9:56 am

Hello,

Greetings from E-iceblue.
Have you tested my code? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Mar 16, 2020 5:24 am

hi,

yes, it did work.

Thank you for your prompt assistance always. :)

alvindutt
 
Posts: 11
Joined: Thu Aug 22, 2019 12:17 am

Mon Mar 16, 2020 5:36 am

Hello,

Thanks for your feedback.
If you need any help in the future, just feel free to contact us.
Wish you all the best!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc

cron