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.

Tue Apr 14, 2015 11:56 am

Hello,

I have a template word doc that contains key tags of the format [FIELD_0], [FIELD_1], etc...
I load this document in memory, and then replace those tags with the correct string values.
When I am done, I return it to the user.
This is working fine.

However, now I have a case where one of my string is html format (bold, italic, bullets), I am wondering how can I convert this "Hmtl string" into "word doc string" ???

Thanks.

Dryadwoods
 
Posts: 1
Joined: Tue Apr 14, 2015 11:53 am

Wed Apr 15, 2015 9:47 am

Hello,

Sorry for late replay because of time difference.
There are some codes for your reference.
Code: Select all
            Document doc = new Document("sample.docx");
            TextSelection selection = doc.FindString("[FILED_0]", true, true);
            TextRange range = selection.GetAsOneRange();
            int index = range.OwnerParagraph.ChildObjects.IndexOf(range);
            Document htmldoc = new Document();
            StringReader sr = new StringReader("<b>hello </b><i>world</i>");
            htmldoc.LoadHTML(sr, XHTMLValidationType.None);
            foreach (Section section in htmldoc.Sections)
            {
                foreach (Paragraph p in section.Paragraphs)
                {
                    foreach (DocumentObject obj in p.ChildObjects)
                    {
                        range.OwnerParagraph.ChildObjects.Insert(index++, obj.Clone());
                    }
                }
            }
            range.OwnerParagraph.ChildObjects.Remove(range);
            doc.SaveToFile("replace1.docx", FileFormat.Docx);


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Apr 17, 2015 3:33 am

Hello,

Has your issue been resolved? Could you please give us some feedback at your convenience?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Tue Aug 27, 2019 1:54 am

Hi I need help with the same where I need to enter the following "<b>hello </b><i>world</i>" on a bookmark and later convert to a word document.

During conversion the html string should render as it would on html.

e.g. "<b>hello </b><i>world</i>" => hello world

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

Tue Aug 27, 2019 5:35 am

Hi,

Thanks for your inquiry.
Please refer to following code:
Code: Select all
            Document document = new Document();
            document.LoadFromFile(FilePath + @"sample.docx");
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("bookmark1");

            //Add a section and name it section0
            Section section0 = document.AddSection();
            //Add a paragraph for section0
            Paragraph paragraph = section0.AddParagraph();
            //Append HTML
            paragraph.AppendHTML("<b>hello </b><i>world</i>");

            bn.InsertParagraph(paragraph);
            //Remove Section0
            document.Sections.Remove(section0);

            string output = "18691.docx";
            document.SaveToFile(output, FileFormat.Docx);


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Aug 27, 2019 11:30 pm

Hi Betsy,

the code works however is there a limit on the conversion?

attached is the html string that needs to be converted.

SAMPLE.zip


On my output I am getting a empty. Refer Below

OutputInPDF.PNG

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

Wed Aug 28, 2019 3:15 am

Hi,

Thanks for your information.
After checking your HTML, I found it has many elements(paragraphs and so on). You need to change your code to below code:
Code: Select all
            string HTML = System.IO.File.ReadAllText(FilePath + "18691-SAMPLE.txt");

            Document document = new Document();
            document.LoadFromFile(FilePath + @"sample.docx");
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("bookmark1");

            //Add a section and name it section0
            Section section0 = document.AddSection();
            //Add a paragraph for section0
            Paragraph paragraph = section0.AddParagraph();
            paragraph.AppendHTML(HTML);

            ParagraphBase FirstItem = section0.Paragraphs[0].Items.FirstItem as ParagraphBase;
            ParagraphBase LastItem = section0.Paragraphs[section0.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
            TextBodySelection selection = new TextBodySelection(FirstItem, LastItem);
            TextBodyPart part = new TextBodyPart(selection);
            //Change to use InsertTextBodyPart
            bn.InsertTextBodyPart(part);

            //Remove Section0
            document.Sections.Remove(section0);

            document.SaveToFile(output, FileFormat.Docx);
            document.SaveToFile("18691.pdf", FileFormat.PDF);


If you still have the issue, please provide your input Word and the code(if it is different from the code I provided) for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Aug 28, 2019 10:00 pm

Amazing

Thank you so much it rendered well.

If there is anything more i will let you know

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

Thu Aug 29, 2019 1:37 am

Hi,

Thanks for your feedback.
If there is any question, welcome to contact us. :D

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Nov 01, 2019 6:50 am

Hi Betsy

for some reason the following attached html string is converting to empty string.
htmlstring_error.zip


I have used the below code provided.

ParagraphBase FirstItem = section0.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase LastItem = section0.Paragraphs[section0.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
TextBodySelection selection = new TextBodySelection(FirstItem, LastItem);
TextBodyPart part = new TextBodyPart(selection);

//Change to use InsertTextBodyPart
bookmarkNavigator.InsertTextBodyPart(part);

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

Fri Nov 01, 2019 9:12 am

Hi,

Thanks for your inquiry. This is Amber from E-iceblue support team.
After initial investigation, I found your Html string is actually a table. Kindly note that paragraph doesn't include table, they are at the same level. Please refer to the code below to insert table into the bookmark in Word file.

Code: Select all
            string HTML = System.IO.File.ReadAllText(@"……\htmlstring_error.txt");

            Document document = new Document();
            document.LoadFromFile(@"……\sample.docx");
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("bookmark1");

            //Add a section and name it section0
            Section section0 = document.AddSection();
            //Add a paragraph for section0
            Paragraph paragraph = section0.AddParagraph();
            //Append the HTML string
            paragraph.AppendHTML(HTML);

            //Get the table in the section0
            Table t = section0.Tables[0] as Table;
            //Insert the table into the bookmark
            bn.InsertTable(t);

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

            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

Thu Dec 05, 2019 9:49 pm

Hi Support,

is there a code that will be work for both tables and also many elements of a paragraph?

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

Fri Dec 06, 2019 10:51 am

Hi,

Thanks for your reply.
Please refer to the following code to achieve your demand. If there is any question, please feel free to write back.

Code: Select all
            Document document = new Document();
            document.LoadFromFile(filepath+"filename.docx");
            //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("bookmark1");

            //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;
                    bn.InsertParagraph(para);
                }
                //if it is a table
                if (obj is Table)
                {
                    //insert the table into the bookmark
                    Table t = obj as Table;
                    bn.InsertTable(t);
                }
            }
            //remove the section0
            document.Sections.Remove(section0);
            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

Thu Jan 02, 2020 3:50 am

Hi,

I am able to convert html to paragraphs now. However, there are unnecessary spaces created when documents are saved.

Please refer attached document for detailed explanation.

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

Thu Jan 02, 2020 6:29 am

Hi,

Thanks for your inquiry.
Please note our Spire.Doc follows the the rules of MS Word. I open the HTML you mentioned in MS Word, below is the result showed in it.
openWithMSWord.png

Then with following code, I got same result Word file.
Code: Select all
            Document document = new Document();
            string HTML = File.ReadAllText(FilePath + "19423input.html");
            Section section0 = document.AddSection();
            Paragraph paragraph = section0.AddParagraph();
            //Append your html string
            paragraph.AppendHTML(HTML);
            document.SaveToFile("19432result.docx", FileFormat.Docx);

I didn't notice the issue you mentioned. Attached are my files for reference.
If I misunderstand, please share following information for further investigation:
1. The input Word file(if any)
2. The complete code which could reproduce your issue directly
3. The result Word you got
4. The expected Word file

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc