Page 1 of 1

Getting Text Size With wxPdfDocument

Posted: Wed Aug 15, 2018 8:53 pm
by Everydaydiesel
Hello, I am trying to use GetStringWidth() in wxPdfDocument and I was expecting the line drawn to be the same length as the text. Does anyone know what I am doing wrong?

Code: Select all

    string sFilePath = "/home/test/test.pdf";
    wxPdfFontManager::GetFontManager()->AddSearchPath(wxT("/usr/share/fonts"));
    wxPdfDocument pdf;
    pdf.AddPage(wxPORTRAIT, wxPAPER_A4);
    pdf.SetFont(wxT("Helvetica"), NULL, 14);
    string sToDraw = "Test String Size";
    double dWidth = pdf.GetStringWidth(sToDraw);
    pdf.Text(10, 10, sToDraw);
    pdf.Line(10, 10, dWidth, 10);
    pdf.SaveAsFile(sFilePath);
    string sOpenCommand = "xdg-open " + sFilePath;
    system (sOpenCommand.c_str());
textsample.png
textsample.png (6.32 KiB) Viewed 4027 times
Thanks in advance with any help you can give me!

Re: Getting Text Size With wxPdfDocument

Posted: Thu Aug 16, 2018 9:55 pm
by utelle
Everydaydiesel wrote:Hello, I am trying to use GetStringWidth() in wxPdfDocument and I was expecting the line drawn to be the same length as the text. Does anyone know what I am doing wrong?
Yes. You are using the Line method incorrectly. You have to specify the start point and the end point of the line. However, you used the intended length of the line as the x coordinate of the end point, but you started the line with an offset of 10 millimeter to the left border. Therefore the line is too short by 10 millimeter.

Instead of

Code: Select all

    pdf.Line(10, 10, dWidth, 10);
use

Code: Select all

    pdf.Line(10, 10, 10 + dWidth, 10);
and you will get the intended result.

Regards,

Ulrich

Re: Getting Text Size With wxPdfDocument

Posted: Fri Aug 17, 2018 3:05 am
by Everydaydiesel
Thank you kind sir! That worked!