-
Notifications
You must be signed in to change notification settings - Fork 662
-
Hi @JorjMcKie
I have 2 things to ask
-
I am using
insert_textboxto insert text within providedrect. I can easily horizontal align usingalign, but there is no direct way to vertically align.
When text is insert in a rect, it is insert on top left. I would want it to vertically align.
How can I vertically align a text in Rect.
This discussion didn't really helped me out cause it uses textwriter in text Link-Discussion-1662 -
I am also rotating the text within text box. I am using
morph=(point,matrix). Origin of rotation is top left as we have point system with us. I can calculate width/2 and height/2 of Rect and rotate accordingly but text rotates based on center of Rect and NOT OF TEXT actually.
Would really appreciate your answer. Thanks
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 4 comments 6 replies
-
I am trying to re render attached PDF. Attached screenshot is where I am having issues
Beta Was this translation helpful? Give feedback.
All reactions
-
First Point
There indeed is no direct support of centering (aligning???) vertically. But with a small effort you can achieve the effect.
Just execute on a temporary page and check the return code (rc): this is the height of the unused sub-rectangle at the bottom.
Use the half of this value to write to a accordingly defined other rectangle page.insert_textbox(rect + (0, rc/2, 0, rc/2), ...).
This modified rect is shifted downwards.
To compute rc:
temp = pymupdf.open() tpage=temp.new_page(width=rect.width, height=rect.height) rc = tpage.insert_textbox(rect, ...) temp.close()
Beta Was this translation helpful? Give feedback.
All reactions
-
This itself adjust the rect but not the text inside it. My goal is to vertically center text in a Rect
Beta Was this translation helpful? Give feedback.
All reactions
-
But the effect looks like vertically centered text in the original rect:
import pymupdf text = "show vertically centered in the original rectangle" rect = pymupdf.Rect(100, 100, 200, 200) doc = pymupdf.open() page = doc.new_page() temp = pymupdf.open() tpage = temp.new_page(width=rect.width, height=rect.height) rc = tpage.insert_textbox(rect, text) page.insert_textbox(rect + (0, rc / 2, 0, rc / 2), text) page.draw_rect(rect, color=(1, 0, 0)) doc.ez_save(__file__.replace(".py", ".pdf"))
image
What are your objections?
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes this helps.
My goal is to vertically align the text and from the center of the text itself do a rotation.
Currently if I apply morph it rotates from top left of the text. I want rotation from center of text such that it rotates from center origin
Beta Was this translation helpful? Give feedback.
All reactions
-
Increase the font size and the text will be pushed towards bottom
Beta Was this translation helpful? Give feedback.
All reactions
-
As per your other point:
- We cannot write text along some curve - only straight lines. This will not be supported ever presumably.
- As far as I can see, the only way to approximately achieve what you want is
- make an upright version,
- then make an image of the result
- insert this image as a temporary PDF page via
page.show_pdf_page(... ,rotate=theta)with a desired angle.
Beta Was this translation helpful? Give feedback.
All reactions
-
@JorjMcKie I have observed that insert_textbox comes with padding. Please correct me if I am wrong
Beta Was this translation helpful? Give feedback.
All reactions
-
What does padding mean?
Beta Was this translation helpful? Give feedback.
All reactions
-
When text is inserted into textbox it keeps some space inside the borders of a rect. Even though there is visibly some space left to fill entire textbox, it just doesn't render it at all.
I extract a PDF using get_text block methods and for each text element its rect. When i redraw using same dimensions and same font family,size it doesn't render the text in textbox. Text appear when either I reduce the font size a bit (e.g) *0.9 or increase rect dimesnions
Beta Was this translation helpful? Give feedback.