Search code examples
c#.netsystem.drawingpdfsharp

.net way to manipulate underline thickness in System.Drawing via PDFsharp


I'm interested in tweaking / fine-tuning some code which is getting written into a PDF using the PDFsharp library.

The issue is: when I have text that is both bold and underlined, the underline becomes thicker as well as its respective text. I would like to be able to have the underline remain thin while the text becomes bold.

Looking at PDFsharp’s API as well as that of System.Drawing, it looks like there is no easy way of doing this.

Does anyone know otherwise?

Below is an example of current output from code consuming the PDFsharp lib, which I’m pretty sure has some wrapping calls to System.Drawing and other GDI/WPF implementations.

We're using GDI I am pretty sure. The main PDFsharp call we are making here is

public void DrawString(string s, XFont font, XBrush brush, double x, double y, XStringFormat format, double width)

enter image description here


Solution

  • PDFsharp does not use System.Drawing when generating a PDF file, so manipulating System.Drawing won't do any good in your case.

    PDFsharp uses underline position and underline thickness from the font table.

    Since PDFsharp is open source, you can modify it to use a fixed underline thickness.

    Or write a helper method that draws text without underline and later draws a line below the text. This can be done without modifying PDFsharp and you can easily upgrade to later versions.

    Update: Another option: Use MigraDoc to create the PDF files.

    MigraDoc uses PDFsharp to create the PDF file, but MigraDoc draws the underlines of text with own code and regular and bold text will have identical underlines.