Trying to add a background colour when using PdfHTMLTextElement

Please could someone help.  I have the following code to show HTML on my pdf.  I want the text to appear on a coloured background, so I am first drawing a rectangle with the colour box i need and then the html text over the top.  However when i write out the HTML to background colour remains white. 


RectangleF layoutRectangle = new RectangleF((float)rectXStartPos, intY + (borderSize / 2), intWidth - borderSize, height - borderSize);

 PdfBrush backgroundBrush = new PdfSolidBrush(new PdfColor(255, 0, 0));

page.Graphics.DrawRectangle(backgroundBrush, layoutRectangle);

 //HTML TEXT

 string htmlText = objTableSetting.Text;

PdfFont font = CreatePdfFont(FONT_NAME, fontSize, FontStyle.Regular);

PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, CreateSolidBrush(fontColour));

richTextElement.TextAlign = TextAlign.Left;

 PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();

 format.Layout = PdfLayoutType.Paginate;

format.Break = PdfLayoutBreakType.FitPage;


richTextElement.Draw(page, layoutRectangle, format);

If i replace
richTextElement.Draw(page, layoutRectangle, format);'

with

page.Graphics.DrawString(objTableSetting.Text, CreatePdfFont(FONT_NAME, fontSize, FontStyle.Regular), CreateSolidBrush(fontColour), new RectangleF(intX + 5, intY, intWidth - 6, height), objStringFormat);' 

it works fine and i get my text on a red background, but obviously the html tages are written out.

Can someone help?  Many thanks


7 Replies

CG Caroline Gooding March 12, 2025 07:24 AM UTC

Does anyone know the answer please?  



IJ Irfana Jaffer Sadhik Syncfusion Team March 13, 2025 11:52 AM UTC

Hi Caroline,


Currently we are validating on the reported behavior with the provided details on our end and we will share the further validation on March 17th, 2025.



Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team March 18, 2025 01:41 PM UTC

Hi Caroline,


We have confirmed the issue “Transparency is not applied while drawing the richtextbox” as a defect in our product and we will include the fix on 8th April 2025 
Please use the below feedback link to track the status of the reported bug.
Note: If you require a patch for the reported issue in any of our Essential Studio Main or SP release version, then kindly let us know the version, so that we can provide a patch in that version based on our SLA policy.
Disclaimer: “Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.”


Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team April 8, 2025 09:45 AM UTC

Hi Caroline,


We were unable to include the fix for the issue "Transparency is not applied while drawing the richtextbox" in this weekly release and will include the fix in our upcoming weekly release on 15th April 2025.


Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team April 15, 2025 09:53 AM UTC

Hi Caroline,


We have included the fix for the reported issue "Transparency is not applied while drawing the richtextbox" in our weekly release (v29.1.38). Please use the below link to download our latest NuGet.

NuGet Link : https://www.nuget.org/packages/Syncfusion.Pdf.WinForms/29.1.38

Root cause:

Transparency is not applied when rendering HTML text in a PDF. We need to create a BackColor API to set the transparency if transparency is applied to the background color.


Regards,

Irfana J.




IJ Irfana Jaffer Sadhik Syncfusion Team April 15, 2025 10:26 AM UTC

Hi Caroline,


Please use the below code snippet:

PdfDocument document = new PdfDocument();

PdfPage page = document.Pages.Add();

 

float rectXStartPos = 50;

float intY = 100;

float borderSize = 5;

 

// Create the layout rectangle

RectangleF layoutRectangle = new RectangleF(rectXStartPos, intY + (borderSize / 2), 100, 100);

 

// HTML content with transparent background

string htmlText = "Hello, <b>Syncfusion PDF!</b>";

 

// Create a font

PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

 

// Create an HTML text element

PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, PdfBrushes.White);

richTextElement.TextAlign = TextAlign.Left;

richTextElement.BackColor = Color.Transparent;

 

// Layout format for pagination

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat

{

    Layout = PdfLayoutType.Paginate,

    Break = PdfLayoutBreakType.FitPage

};

// Save graphics state before drawing the rectangle

page.Graphics.Save();

 

// Draw a red background

PdfBrush backgroundBrush = new PdfSolidBrush(new PdfColor(255, 0, 0));

page.Graphics.DrawRectangle(backgroundBrush, layoutRectangle);

 

// Restore graphics state after rectangle

page.Graphics.Restore();

 

// Save graphics state before drawing HTML

page.Graphics.Save();

 

// Draw the HTML content over the colored background

richTextElement.Draw(page, layoutRectangle, format);

 

// Restore graphics state after HTML

page.Graphics.Restore();

 

// Save the document to a file

string outputFile = Path.Combine(Directory.GetCurrentDirectory(), "Output.pdf");

document.Save(outputFile);



Regards,

Irfana J



CG Caroline Gooding April 21, 2025 01:42 PM UTC

Really appreciate this Irfana, many thanks for your help in resolving this.


Loader.
Up arrow icon