Getting Exact Text Height

gs.GetFontSize() gives me the font size. How can i get exactly font height of Ascent.

font.getAscent() gives height in glph coordinate . How to convert that to world co-ordinate (or) font-size.

Bounding Box of font also gives size in glyph coordinates.

Bounding Box of text element cannot be used for height because the box is not optimal one.

It sounds like you would like to use TextExtractor methods to get bbox for line/word/character:

Alternatively if you are working with ElementReader you could use element.GetBBox()
… or for more low lever control see

FYI, section 9.4.4. Text Space Details ( ) provides info on how to map all gstate and font parameters to a bbox in user coordinate system.

Thanks. for you reply. element.getBBox is not accurate. Anyways i dont need entire height, i need the split of ascent and descent.

Below is the code that i used from 9.4.4 pdf spec. My Idea is to just derive a perfect Font Ascent value.

1.) pdftron::Common::Matrix2D Trm;
double Tfs = gs.GetFontSize();
double Th = gs.GetHorizontalScale() / 100;
double Trise = gs.GetTextRise();
Trm.m_a = Tfs * Th;
Trm.m_b = 0;
Trm.m_c = 0;
Trm.m_d = Tfs;
Trm.m_h = 0;
Trm.m_v = Trise;

pdftron::Common::Matrix2D Trm_mtx = element.GetCTM() * element.GetTextMatrix() * Trm;

pdftron::PDF::Rect outBBox outBBoxText = font.GetBBox();
tX1 = outBBoxText.GetX1() / 1000;
tY1 = outBBoxText.GetY1() / 1000;
tX2 = outBBoxText.GetX2() / 1000;
tY2 = outBBoxText.GetY2() / 1000;


tY2-tY1 should be the height of the ascent. But this value is inaccurate and is quite high.

2.) Other method that i used to derive Ascent is

double PdfRenderer::getFontHeight(double fontsize, const pdftron::Common::Matrix2D& mtx) {
double x = mtx.m_c * fontsize;
double y = mtx.m_d * fontsize;
return sqrt(xx + yy);

fontSize = getFontHeight(gs.GetFontSize() , element.GetCTM() * element.GetTextMatrix());

outBBoxText = font.GetBBox();
double bBoxAscent = (outBBoxText.y2 /(outBBoxText.y2 - outBBoxText.y1)) * fontSize;
double bBoxDescent = (abs(outBBoxText.y1) /(outBBoxText.y2 + abs(outBBoxText.y1))) * fontSize;

In the above logic, i multiply fontsize with a factor derived from Ascent:Descent ratio. Even this ascent value is slightly inaccurate.

I saw your post regarding taking each character Bounding Box. we render e_text_begin to e_text_end as one single text flow, so even when we take character by character ascent, we would still endup taking the tallest BBox height .

Kindly share if there is any other way to get optimal value of Ascent of Text.