From 4019f67de3ab192ad1bca08480e8f5ac7f252027 Mon Sep 17 00:00:00 2001 From: Mikkel Ovesen Date: Thu, 2 Jul 2026 21:52:11 +0200 Subject: [PATCH] feat(html): support background-color and bgcolor on chunks in simple parser Map the CSS background-color property to the internal bgcolor key in both FactoryProperties.InsertStyle overloads and apply it as the chunk background in CreateChunk. The legacy bgcolor tag attribute works as well, since tag attributes flow into ChainedProperties automatically. --- .../HtmlWorkerTests.cs | 32 ++++++++++++++++++- .../html/simpleparser/FactoryProperties.cs | 15 +++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs b/src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs index 1c8d0f48..17b9f4ff 100644 --- a/src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs +++ b/src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.util; using iTextSharp.LGPLv2.Core.FunctionalTests.iTextExamples; using iTextSharp.text; @@ -181,6 +182,35 @@ public void Verify_Html_To_Pdf_With_colspan_CanBeCreated() TestUtils.VerifyPdfFileIsReadable(pdfFilePath); } + [TestMethod] + public void Verify_BackgroundColor_Style_Is_Applied_To_Chunks() + { + var styles = new StyleSheet(); + + using var reader = + new StringReader( + "styled attributed"); + + var objects = HtmlWorker.ParseToList(reader, styles); + + var chunks = new List(); + + foreach (var element in objects) + { + chunks.AddRange(element.Chunks); + } + + var styledChunk = chunks.Find(c => c.Content == "styled"); + Assert.IsNotNull(styledChunk); + var styledBackground = (object[])styledChunk.Attributes[Chunk.BACKGROUND]; + Assert.AreEqual(expected: 0xFFFF00, ((BaseColor)styledBackground[0]).ToArgb() & 0xFFFFFF); + + var attributedChunk = chunks.Find(c => c.Content == "attributed"); + Assert.IsNotNull(attributedChunk); + var attributedBackground = (object[])attributedChunk.Attributes[Chunk.BACKGROUND]; + Assert.AreEqual(expected: 0xFF0000, ((BaseColor)attributedBackground[0]).ToArgb() & 0xFFFFFF); + } + private void applyRtlRunDirection(IElement htmlElement) { if (htmlElement is not PdfPTable table) diff --git a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/html/simpleparser/FactoryProperties.cs b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/html/simpleparser/FactoryProperties.cs index f5d25de6..bf051b02 100644 --- a/src/iTextSharp.LGPLv2.Core/iTextSharp/text/html/simpleparser/FactoryProperties.cs +++ b/src/iTextSharp.LGPLv2.Core/iTextSharp/text/html/simpleparser/FactoryProperties.cs @@ -273,6 +273,10 @@ public static void InsertStyle(INullValueDictionary h) h[key: "color"] = hs; } } + else if (key.Equals(Markup.CSS_KEY_BGCOLOR, StringComparison.OrdinalIgnoreCase)) + { + h[key: "bgcolor"] = prop[key]; + } else if (key.Equals(Markup.CSS_KEY_LINEHEIGHT, StringComparison.OrdinalIgnoreCase)) { var ss = prop[key].Trim(); @@ -387,6 +391,10 @@ public static void InsertStyle(INullValueDictionary h, ChainedPr h[key: "color"] = hs; } } + else if (key.Equals(Markup.CSS_KEY_BGCOLOR, StringComparison.OrdinalIgnoreCase)) + { + h[key: "bgcolor"] = prop[key]; + } else if (key.Equals(Markup.CSS_KEY_LINEHEIGHT, StringComparison.OrdinalIgnoreCase)) { var ss = prop[key].Trim(); @@ -447,6 +455,13 @@ public static Chunk CreateChunk(string text, ChainedProperties props) ck.SetTextRise(size); } + var bgColor = Markup.DecodeColor(props[key: "bgcolor"]); + + if (bgColor != null) + { + ck.SetBackground(bgColor); + } + ck.SetHyphenation(GetHyphenation(props)); return ck;