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 Oct 06, 2023 11:05 am

Bug when replacing HTML content in Word file and converting it to PDF. The content is correctly formatted (in our case bold text parts) when generating the Word file. When converting to PDF the formatting gets skewed and random text parts become bold.

I've created a repository to reproduce the issue here: github.com/ngRevan/SpireHtmPdflBug

Tested with Spire.Doc 11.9.19 on Windows 10.

itbu
 
Posts: 2
Joined: Mon Feb 25, 2019 11:40 am

Sat Oct 07, 2023 9:42 am

Hi,

Thank you for your inquiry.
I have tested the code and file you provided and encounter the same issue as you. However, there is another solution to achieve the effect you want, please refer to the following code and have a test:
Code: Select all
  var htmlContent = "Pommes Frites (Pommes frites (Kartoffeln, Rapsöl), HO Sonnenblumenöl, Speisesalz), Kalbfleisch (Kalbfleisch (Schweiz).), Gemüsemischung (Karotten, Blumenkohl, Bohnen grün, Zwiebeln, <strong>Butter</strong>, Petersilie glatt (mit Antioxidationsmittel: Milchsäure)), Panade-Mischung (Paniermehl (Hart<strong>weizen</strong>mehl, <strong>Weizen</strong>mehl, Hefe, Kochsalz jodiert.), <strong>Vollei </strong>flüssig, pasteurisiert, Bodenhaltung; Säuerungsmittel E330.), Mehlmischung (<strong>Weizen</strong>mehl, <strong>Hartweizendunst</strong>, <strong>Weizengluten</strong>, <strong>Gerstenmalzmehl</strong>)), Pflanzenöl (HO Sonnenblumenöl), Zitronen (Zitronen.)";

            var document = new Document("TestHtml.docx");
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
            bookmarkNavigator.MoveToBookmark("HTMLContent", false, true);

            if (bookmarkNavigator.CurrentBookmark == null)
            {
                return;
            }

            var tempSection = document.AddSection();
            tempSection.AddParagraph().AppendHTML(htmlContent);

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

            bookmarkNavigator.DeleteBookmarkContent(false);
            bookmarkNavigator.ReplaceBookmarkContent(part,false,true);

            document.Sections.Remove(tempSection);
            document.SaveToFile(@"E:\34740\TestHtml4.docx", FileFormat.Docx);
            document.SaveToFile(@"E:\34740\TestHtml4.pdf", new ToPdfParameterList { IsEmbeddedAllFonts = true });

This is my test screenshot:
屏幕截图 2023-10-07 172957.png

If you have any issue just feel free to contact us.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Wed Nov 01, 2023 2:07 am

Hello,

Did you test the code on your side? Did it solve your problem?
Please feel free to us if you have any problem.

Best Regards,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.Doc