When using insertTextbox() method, you have to be sure that the rect you create can contain the text, because if not, you will end up with no text displayed.
One way to do this, is checking the text's lenght for your implementation, like this:
text = "absolutely not"
text_lenght = fitz.getTextlength(text, fontname="Times-Roman", fontsize=48))
text_lenght will be 270.67.
But the rect's width you have is just 150 (200-50). Also, even changing the rect's width won't work, because it's still too short, so you need also to increase it's height. Another thing you can do is just change the fontsize you're using.
These two alternatives will look like this:
- Alternative 1, bigger rect:
fontsize_to_use = 48
text = "absolutely not"
fontname_to_use = "Times-Roman"
text_lenght = fitz.getTextlength(text,
fontname=fontname_to_use,
fontsize=fontsize_to_use)
rect_x1 = 50
rect_y1 = 100
rect_x2 = rect_x1 + text_lenght + 2 # needs margin
rect_y2 = rect_y1 + fontsize_to_use + 2 # needs margin
rect = (rect_x1, rect_y1, rect_x2, rect_y2)
## Uncomment if you wish to display rect
# page.drawRect(rect,color=(.25,1,0.25))
rc = page.insertTextbox(rect, text,
fontsize=fontsize_to_use,
fontname=fontname_to_use,
align=1)
Alternative 2, smaller fontsize:
text = "absolutely not"
fontname_to_use = "Times-Roman"
rect_x1 = 50
rect_y1 = 100
rect_x2 = 200
rect_y2 = 200
rect_width = rect_x2 - rect_x1
rect_height = rect_y2 - rect_y1
rect = (rect_x1, rect_y1, rect_x2, rect_y2)
fontsize_to_use = rect_width/len(text)*2 # *2 just because 1pt is too small for a char. It mantains a good ratio for rect's width with larger text, but behaviour is not assured.
## Uncomment if you wish to display rect
# page.drawRect(rect,color=(.25,1,0.25))
rc = page.insertTextbox(rect, text,
fontsize=fontsize_to_use,
fontname=fontname_to_use,
align=1)
Note: rc is the unused rectangle height, it can also be negative, in your case it was -5.59, which means text was exceeding rect's height.