Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Vertically align text in a Rect when using insert_textbox #3993

rafayaar started this conversation in General
Discussion options

Hi @JorjMcKie
I have 2 things to ask

  • I am using insert_textbox to insert text within provided rect. I can easily horizontal align using align, 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

You must be logged in to vote

Replies: 4 comments 6 replies

Comment options

image

I am trying to re render attached PDF. Attached screenshot is where I am having issues

dinnerly 2.pdf

You must be logged in to vote
0 replies
Comment options

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()
You must be logged in to vote
4 replies
Comment options

This itself adjust the rect but not the text inside it. My goal is to vertically center text in a Rect

Comment options

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?

Comment options

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

Comment options

Increase the font size and the text will be pushed towards bottom

Comment options

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.
You must be logged in to vote
0 replies
Comment options

@JorjMcKie I have observed that insert_textbox comes with padding. Please correct me if I am wrong

You must be logged in to vote
2 replies
Comment options

What does padding mean?

Comment options

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /