Getting Text Size With wxPdfDocument Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 111
Joined: Wed Oct 28, 2015 9:48 pm

Getting Text Size With wxPdfDocument

Post by Everydaydiesel » Wed Aug 15, 2018 8:53 pm

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 1625 times
Thanks in advance with any help you can give me!

utelle
Moderator
Moderator
Posts: 867
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: Getting Text Size With wxPdfDocument

Post by utelle » Thu Aug 16, 2018 9:55 pm

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

Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 111
Joined: Wed Oct 28, 2015 9:48 pm

Re: Getting Text Size With wxPdfDocument

Post by Everydaydiesel » Fri Aug 17, 2018 3:05 am

Thank you kind sir! That worked!

Post Reply