We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

InsertHTML not keeping font face and size format

When using InsertHtml the headings are not created with the correct font face or size. The font colour is correct but not the face or size. I have a sample project if that will help

        var templatePath = Directory.GetCurrentDirectory().Replace(@"bin\Debug", "") + @"Templates\Template.docx";

        // Read in the account's template
        if (File.Exists(templatePath) == false)
        {
            throw new FileNotFoundException();
        }

        var doc = new Document();
        var builder = new DocumentBuilder(doc);

        // Create the table of contents
        builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
        builder.InsertBreak(BreakType.PageBreak);

        // Insert HTML paragraphs
        builder.StartBookmark("section_1");
        builder.InsertHtml("<h1>Section 1</h1><h2>Subsection 1.1</h2><p>Content for subsection 1.1.</p><h2>Subsection 1.2</h2><p>Content for subsection 1.2.</p>");
        builder.EndBookmark("section_1");

        builder.StartBookmark("section_2");
        builder.InsertHtml("<h1>Section 2</h1><h2>Subsection 2.1</h2><p>Content for subsection 2.1.</p><h2>Subsection 2.2</h2><p>Content for subsection 2.2.</p>");
        builder.EndBookmark("section_2");

        // Update the table of contents
        doc.UpdateFields();

        // Merge the document into the template... perserve style
        Document templateDocument;

        using (Stream stream = File.OpenRead(templatePath))
        {
            templateDocument = new Document(stream);
        }

        templateDocument.RemoveAllChildren();
        templateDocument.AppendDocument(doc, ImportFormatMode.UseDestinationStyles);

        var outputPath = string.Format(@"{0}Output\{1:yyyy-MM-dd-hh-mm-ss-tt}.docx", Directory.GetCurrentDirectory().Replace(@"bin\Debug", ""), DateTime.Now);
        if (File.Exists(outputPath) == true)
        {
            throw new IOException("File Exists");
        }
        templateDocument.Save(outputPath);

The example can be downloaded here:
https://1drv.ms/u/s!AkCkc0juiTKe2gFAsRguU4nJifJV

@robschenkel,

Thanks for your inquiry. Please use overload of DocumentBuilder.InsertHtml(String html, Boolean useBuilderFormatting) method to insert an HTML string into the document. This overloaded method has an argument useBuilderFormatting. When useBuilderFormatting is false, DocumentBuilder formating is ignored and formatting of inserted text is based on default HTML formatting. As a result, the text looks as it is rendered in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with Write.

Please use the value of useBuilderFormatting as true to get the desired output.

builder.InsertHtml("<h1>Section 1</h1><h2>Subsection 1.1</h2><p>Content for subsection 1.1.</p><h2>Subsection 1.2</h2><p>Content for subsection 1.2.</p>", true);

Thanks worked perfectly.