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
Does anyone know the answer please?
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.
Hi Caroline,
Regards,
Irfana J.
Hi Caroline,
Regards,
Irfana J.
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.
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
Really appreciate this Irfana, many thanks for your help in resolving this.