TextFormat Padding

Posted by: brian on 27 March 2020, 1:15 am EST

  • Posted 27 March 2020, 1:15 am EST

    How can I get rid of the padding that seems be placed when writing text on an Image? I wrote some sample code below, and attached my output. You can see there there is some padding between the letters and the surrounding box. Is there a way to get rid of that?


    using (var bmp = new GcBitmap(500, 500, false, 96, 96)) {

    using (var g = bmp.CreateGraphics()) {

    var rc = new RectangleF(0, 0, bmp.Width, bmp.Height);
    g.FillRectangle(rc, ColorTranslator.FromHtml("#000"));

    var txtRec = new RectangleF(50, 50, 400, 400);

    // Create a text format for the "Hello World!" string:
    TextFormat tf = new TextFormat() {
    FontSize = 50,
    ForeColor = ColorTranslator.FromHtml("#FFF"),
    FontStyle = GrapeCity.Documents.Text.FontStyle.Regular
    };

    // Draw the string (text)
    g.DrawString("1234", tf, txtRec, TextAlignment.Leading, ParagraphAlignment.Near, true);

    // Draw Outline
    g.DrawRectangle(txtRec, new GrapeCity.Documents.Drawing.Pen(Color.Red));

    }

    bmp.SaveAsPng(@"D:\temp\Outputs\Test.png");

    }
  • Replied 27 March 2020, 4:47 am EST

    Do you mean the (small) space between the text and the red box?
  • Replied 27 March 2020, 5:09 am EST

    Thanks for the reply.

    Yes. I know it seems insignificant, but if I'm trying to grasp what I am doing wrong before I setup coordinate systems of where text should go and start laying things out.

    Not sure if I should adjust all coordinates for that small space.

    Thanks again.
  • Marked as Answer

    Replied 27 March 2020, 5:25 am EST

    Hi,

    You can set TextFormat.UseTypoMetrics to true, this will get rid of the padding - but, the '1' in your text will still be padded on the sides, this is glyph (and font) specific. Change the text to "2345" and there will be no padding at all.
    Hope this helps, let me know.

    Dmitry.
  • Replied 27 March 2020, 5:35 am EST

    Thank You! That worked perfect.

    I'm very impressed with your library so far. I'm in the process of evaluating it for a new project we are working on, but I have some licensing questions.
    I've sent an email to your sales department (us.sales@grapecity.com) regarding them.

    Can you have someone email me and explain the pricing before I start down a path we can't afford.

    Thanks again for your help and the quick response!
  • Replied 27 March 2020, 6:00 am EST

    Hi Brian,

    I forwarded your info to our sales, they should be getting in touch with you shortly.

    Cheers,
    Dmitry.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels