Search code examples
c#pdfpdfsharp

C# PDFSharp - Add black underline to separate text


I'm using PDFSharp to create a PDF in C# and I want to add a black underline between two blocks of text, like this:

This is (part) of the code I'm using to write my PDF:

        PdfDocument pdf = new PdfDocument(); 
        PdfPage page = pdf.AddPage();
        XGraphics gfx = XGraphics.FromPdfPage(page);
        XFont title = new XFont("Times New Roman", 30, XFontStyle.Bold);
        XFont formatBody = new XFont("Times New Roman", 15);

        XImage image = XImage.FromFile("Logo.png");

        gfx.DrawString("Text Title", title, XBrushes.Black,
            new XRect(0, 75, page.Width, page.Height), XStringFormats.TopCenter);
         
        //LINE SEPARATOR HERE

        gfx.DrawString("Body text", formatBody, XBrushes.Black,
            new XRect(50, 130, page.Width, page.Height), XStringFormats.TopLeft); 

Thanks in advance for the help.


Solution

  • Drawing a line is simple:

    gfx.DrawLine(XPens.DarkGreen, 0, 120, page.Width, 0);
    

    The PDFsharp samples are explained here:
    http://pdfsharp.net/wiki/Graphics-sample.ashx#Draw_simple_lines_0

    Sample source code can be found here:
    https://github.com/empira/PDFsharp-samples