Hi there,
When importing HTML fragments, like <h1>Hello World</h1>, it seems that in addition to setting the paragraph style, GemBox explicitly sets the character format of the created run with a preset font size, bold, etc. Is it possible to have GemBox respect the default document styles when importing these elements, and not override them in the run’s character format?
Hi,
Try setting the HtmlLoadOptions.InheritCharacterFormat property to true.
Regards,
Stipo
Hi Stipo,
We are using the following options, and are still observing that headings have additional styles beyond the document defaults.
position.LoadText(html, new HtmlLoadOptions
{
HtmlType = HtmlType.HtmlInline,
InheritParagraphFormat = true,
InheritCharacterFormat = true
});
I would happy to put together a small example, if that would be beneficial.
Here’s a simple example to reproduce the issue. We configure the default font color and size for Heading 1, then insert one from HTML. While the color is as expected, the font size is not.
using System.Diagnostics;
using GemBox.Document;
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var doc = new DocumentModel();
var headingStyle = (ParagraphStyle)doc.Styles.GetOrAdd(StyleTemplateType.Heading1);
headingStyle.CharacterFormat.Size = 10;
headingStyle.CharacterFormat.FontColor = Color.Red;
var position = doc.Content.End;
var html = """
<h1>Hello world</h1>
""";
position.LoadText(html, new HtmlLoadOptions
{
HtmlType = HtmlType.HtmlInline,
InheritParagraphFormat = true,
InheritCharacterFormat = true
});
var heading = doc.GetChildElements(true, ElementType.Paragraph).Cast<Paragraph>().Single();
var headingText = (Run)heading.Inlines[0];
Debug.Assert(headingText.CharacterFormat.FontColor == Color.Red, "Color was expected to be red"); // works
Debug.Assert(headingText.CharacterFormat.Size == 10, $"Size was expected to be 10, was {headingText.CharacterFormat.Size}"); // fails, gets 24
Hi Michael,
Please try again with this NuGet package:
Install-Package GemBox.Document -Version 2025.8.113
Note that this is a hidden (unlisted) version. To install it, you’ll need to run the above command on the NuGet Package Manager Console (Tools → NuGet Package Manager → Package Manager Console).
Does this solve your issue?
Regards,
Mario
That fix resolved my issue, thanks for your assistance!