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 06, 2021 9:32 pm

Hello,
I want insert in my word bookmark rich text HTML. For that I use your tuto : Replace-the-Content-of-Word-Bookmark-with-HTML-Code. But if my Rich Text contain a html table it's not working : appendHTML doesn't like when there is style on tag <td> and when there is none there is a problem with TextBodySelection since the two arguments are null. So is it possible to insert in word bookmark a section and is there a solution where there is style in the tag <td> ?

Here is the HTML for my table :
Code: Select all
"<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n
<tbody>\n
<tr>\n
<td style=\"width: 30.8543%;\">un</td>\n
<td style=\"width: 30.8543%;\">joli</td>\n
<td style=\"width: 30.8543%;\">tableau</td>\n
</tr>\n
<tr>\n<td style=\"width: 30.8543%;\">pour</td>\n
<td style=\"width: 30.8543%;\">le</td>\n
<td style=\"width: 30.8543%;\">contexte</td>\n
</tr>\n
</tbody>\n
</table>"


Thanks

Julien

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Wed Apr 07, 2021 6:27 am

Hello Julien,

Thanks for your inquiry.
I did reproduce your issue and I have logged it in our bug tracking system with the ticket SPIREDOC-5785. If there is any update, we will inform you. Apologize for the inconvenience caused.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Apr 07, 2021 7:34 am

Thank you for your answer, I'm waiting for your solution ! For your information we use FreeSpire.Office we want to buy the licence.

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Mon Apr 12, 2021 3:48 am

Hello,

Hope you are doing well. This is to inform you of updates about your issue.
In Word documents, tables and paragraphs are objects of the same level, but in our tutorial, the inserted html text is treated as a sub-object of the paragraph. So if you use this code to replace the bookmark with the html-style table, the table will not be rendered correctly.
To achieve your requirements, please use the following code instead. If you have any other questions, just feel free to contact us.
Code: Select all
            Document document = new Document("BeforeReplace.doc");
            Section tempSection = document.AddSection();

            String html = "you html string";
            tempSection.AddParagraph().AppendHTML(html);
            TextBodyPart part = new TextBodyPart(document);
            part.BodyItems.Add(tempSection.Body.ChildObjects[0]);

            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);

            bookmarkNavigator.MoveToBookmark("bookmark2");
            bookmarkNavigator.ReplaceBookmarkContent(part);

            document.Sections.Remove(tempSection);
            document.SaveToFile(@"AfterReplace.doc");



Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Apr 12, 2021 10:49 am

Thank you for your answer ! I use a little trick in the case there is paragraph before or after :
Code: Select all
TextBodyPart part = new TextBodyPart(doc);
int childCount = tempSection.Body.ChildObjects.Count;
for (int i = 0; i < childCount; i++)
   part.BodyItems.Add(tempSection.Body.ChildObjects[i - i]);


But I still have the problem when there is style on td tag, I have the following error : "Input string was not in a correct format.". Do you have any solution ? It's happen with the AppendHTML function.

Julien

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Tue Apr 13, 2021 3:52 am

Hello Julien,

Thanks for your feedback.
I did an initial test with your code, but I did not encounter any issue. To help us reproduce your issue and solve it, please provide us with your input Word file, your complete code and your html string. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Apr 13, 2021 10:19 am

Hello,

Here the code to fill a word :
Code: Select all
public void FillDoc(){
   Document doc = WordUtils.LoadDocument("CDS_Contexte.docx"); //Fonction pour charger un doc
   BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
   WordUtils.FillBookmarkRT(doc, bookmarkNavigator, "CDS_Contexte_Observations", cds.ObservationContexte);
}

Code: Select all
internal static void FillBookmarkRT(Document doc, BookmarksNavigator bookmarkNavigator, string bookmarkName, string html){
   if(html != null && doc.Bookmarks.FindByName(bookmarkName) != null)
   {
      Section tempSection = doc.AddSection();
      tempSection.AddParagraph().AppendHTML(html);
      TextBodyPart part = new TextBodyPart(doc);
      int childCount = tempSection.Body.ChildObjects.Count;
      for (int i = 0; i < childCount; i++)
         part.BodyItems.Add(tempSection.Body.ChildObjects[0]);
     
      bookmarkNavigator.MoveToBookmark(bookmarkName);
      bookmarkNavigator.ReplaceBookmarkContent(part);
      doc.Sections.Remove(tempSection);
   }
}

The HTML :
Code: Select all
"<p>xdcsd</p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 30.8543%;\">un</td>\n<td style=\"width: 30.8543%;\">joli</td>\n<td style=\"width: 30.8543%;\">tableau</td>\n</tr>\n<tr>\n<td style=\"width: 30.8543%;\">pour</td>\n<td style=\"width: 30.8543%;\">le</td>\n<td style=\"width: 30.8543%;\">contexte</td>\n</tr>\n</tbody>\n</table>"

I have just buy the licence for Spire.Office and I have the version 6.4.0.

Julien

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Wed Apr 14, 2021 6:28 am

Hello Julien,

Thanks for providing more information.
I tested you case but still did not reproduce your issue. Here is my test project, please run it directly on your side and then tell us your test results.

If the issue persists, to help us investigate further, please provide us with your OS information (e.g. Windows7, 64bit) and your region setting (e.g. China, Chinese). Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Apr 14, 2021 10:25 am

Hello,

How is your issue now? Could you please give us some feedback at your convenience?

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Apr 14, 2021 1:02 pm

Hello,

thanks for your reply. Itry with your test projet but I still have the error... :cry:

The top of StackTrace that contains the error :
Code: Select all
   à System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt)
   à System.Convert.ToSingle(String value)
   à spr⥦.ᜀ(XmlAttribute A_0, String A_1, TableCell A_2, ᜃ A_3)
   à spr⥦.ᜀ(XmlNode A_0, TableCell A_1, Int32 A_2, ᜁ A_3, ᜃ A_4)
   à spr⥦.ᜈ(XmlNode A_0)
   à spr⥦.ᜦ(XmlNode A_0)
   à spr⥦.ᜀ(XmlNodeList A_0)
   à spr⥦.ᜇ(XmlNode A_0)
   à spr⥦.ᜦ(XmlNode A_0)
   à spr⥦.ᜀ(XmlNodeList A_0)
   à spr⥦.ᜋ(XmlNode A_0)
   à spr⥦.ᜦ(XmlNode A_0)
   à spr⥦.ᜀ(XmlNodeList A_0)
   à spr⥦.ᜍ(XmlNode A_0)
   à spr⥦.ᜦ(XmlNode A_0)
   à spr⥦.ᜀ(XmlNodeList A_0)
   à spr⥦.ᜀ(IBody A_0, String A_1, Int32 A_2, Int32 A_3)
   à spr⥦.ᜀ(IBody A_0, String A_1, Int32 A_2, Int32 A_3, IParagraphStyle A_4, ListStyle A_5)
   à Spire.Doc.Documents.Paragraph.AppendHTML(String html)


I work on Windows 10 64 bits, the version of my Visual Studio is 16.8.3.

Julien

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Thu Apr 15, 2021 10:17 am

Hello Julien,

Thanks for your feedback.
I checked our records and found that your country is France, so I changed my region setting to French (France) and then tested this case, and I indeed reproduced your issue. I have logged it in our bug tracking system with the ticket SPIREDOC-5844. If there is any update, we will inform you immediately.

Sincerely,
Brian
E-iceblue support
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Apr 15, 2021 10:50 am

Tanks, I'm impatiently awaiting your answer!

Julien

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Wed Apr 28, 2021 6:49 am

Hello Julien,

I just got news that our Dev team has fixed the issue of ticket SPIREDOC-5844. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Apr 28, 2021 9:26 am

Great ! Thank you !

Julien.Themis
 
Posts: 8
Joined: Wed Mar 31, 2021 12:37 pm

Fri Apr 30, 2021 10:06 am

Hello,

Greetings from E-iceblue!
Glad to inform you that we just released Spire.Office Platinum(Hotfix) Version:6.4.4 which fixes the issue SPIREDOC-5844, please download it from the following links to test on your side. Looking forward to your test result.
Website link: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget link: https://www.nuget.org/packages/Spire.Office/6.4.4

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc