diff --git a/src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs b/src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs index 1c8d0f4..17b9f4f 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 f5d25de..bf051b0 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;