XTextFormatter revisited: XTextFormatterEx2 for PDFsharp 1.50 beta 2

In an earlier post I described how to improve the XTextFormatter class for PDFsharp 1.32. The problem with that old version: the modified version of XTextFormatter has to be in an assembly with access to the internals of PDFsharp, so you have to compile PDFsharp in order to have an enhanced formatter.

With PDFsharp 1.50 beta things are simpler: all the XTextFormatter class needs is public now and you can use an enhanced version of XTextFormatter even if you are working with the NuGet package of PDFsharp.

My XTextFormatterEx2 has the measuring functionality from XTextFormatterEx. Additionally it allows to control the line spacing.

To measure the height of a text block, you can still call tf.PrepareDrawString() as shown in my older post.
My post about XTextFormatterEx describing the measuring features.

One way of setting the line spacing: Absolute. You can set the line spacing to e.g. 10 points and the line spacing will be 10 points, even if the font size is 24 or 72. So set the line spacing with care or there will be some overlapping.

XTextFormatterEx2 tf = new XTextFormatterEx2(gfx, 
    new XTextFormatterEx2.LayoutOptions
    {
        Spacing = 10, 
        SpacingMode =
          XTextFormatterEx2.SpacingMode.Absolute
    });

The other line spacing mode is Relative. Set a spacing of 0 and you get the behaviour of XTextFormatter and XTextFormatterEx. Set positive values to increase the line spacing, set negative values to reduce the line spacing.

tf = new XTextFormatterEx2(gfx,
    new XTextFormatterEx2.LayoutOptions
    {
        Spacing = 5,
        SpacingMode =
          XTextFormatterEx2.SpacingMode.Relative
    });

And finally the last mode I implemented so far: Percentage. Set 100 for the default behaviour, set 200 for double line spacing, 150 for 1.5 line spacing. Or set 90 for only 90% of the normal line spacing.

tf = new XTextFormatterEx2(gfx,
    new XTextFormatterEx2.LayoutOptions
    {
        Spacing = 150,
        SpacingMode =
          XTextFormatterEx2.SpacingMode.Percentage
    });

You can download the complete XTextFormatterEx2 class as a ZIP file (about 15 kiB in size).
Download XTextFormatterEx2.zip.

The ZIP file contains a solution with a sample program and requires the PDFsharp package from NuGet. To use XTextFormatterEx2 with your project, just copy the file XTextFormatterEx2.cs into your folder and add it to your project.

5 thoughts on “XTextFormatter revisited: XTextFormatterEx2 for PDFsharp 1.50 beta 2”

    1. This is a demo, therefore it is not available as a NuGet package. Nice if it works for you, but in many cases applications will need more and you will have to enhance the code.

  1. I used it and works great. Thank you so much.
    PDF sharp last updated year is 2015. Do you stop improve anymore? Maybe you can upload github for public.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.