Adding text to PDF file #1833
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
First you must find an approximate minimum number of lines that will be used for a given box width. Width being 495 we have text_length = fitz.get_text_length(text,fontname="tiro")*10
math.ceil(math.ceil(text_length/width)*lineheight)
5 You can also overwrite the font lineheight and e.g. take 1.2. I called the above "approximate" because the algorithm will start a new line whenever a word will not completely fit in the current line. The quotient textlength/width ignores this and will tend to compute a value smaller than the final number of lines. |
Beta Was this translation helpful? Give feedback.
First you must find an approximate minimum number of lines that will be used for a given box width. Width being 495 we have
math.ceil(text_length / 495)
is 3.Then multiply this with the "natural" line height of your font, and again use
math.ceil
for the result. The natural line height of a font isfont.ascender - font.descender
. For Times-Roman this is ca. 1.334, so we haveYou can also overwrite the font lineheight and e.g. take 1.2.
I called the above "approximate" because the algorithm will start a new line whenever a word will not completely fit in the current line. The qu…