Skip to main content Skip to footer

How to add a drop shadow effect to text in .NET/.NETCore using DsPDF

Issue:

The ability to show blurred drop shadows is a bitmap effect, this is why DsPDF (previously named GcPDF) does not exposes a direct method/approach for adding shadow effect to text while generating PDF.

Therefore, this article shows how to add blurred/drop shadow effects to text using DsPDF.

Resolution

To show shadow effects with text, place the text in HTML template and set text-shadow attribute. Then, this HTML text can be added to the exported PDF using the DrawHtml method of DsPDF as seen in this code snippet:

// HTML code that represents the content to render:
var html = "<!DOCTYPE html>" +
    "<html>" +
    "<head>" +
    "<style>" +
    "span.bold {" +
        "font-weight: bold;" +
    "}" +
    "p.round {" +
        "font: 24px arial, sans-serif;" +
        "color: DarkSlateBlue;" +
        "padding: 3px 5px 3px 5px;" +
        "text-shadow: 2px 1px LightSkyBlue;" +
    "}" +
    "</style>" +
    "</head>" +
    "<body>" +
    "<p class='round'>Text generated using HTML with <span class='bold'>GcHtml</span>!</p>" +
    "</body>" +
    "</html>";

// Create a new PDF document, add a page, get graphics to draw on:
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
// Render HTML text
g.DrawHtml(html, 50, 50, new HtmlToPdfFormat(false) { MaxPageWidth = 6.5f }, out SizeF size);

Here is the text showing the shadow effect has been applied:


Tags:

Ruchir Agarwal