You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I notice that there is a origin attribute in the TextPage content, but when I change the MediaBox and CropBox of the pdf file, the origin becomes very weird.
if I want to insert a text to this file, I find it would be inserted to a weird place
This has other reasons:
Inside the page definitions (/Contents object), geometry changes happen in a "non-localized" way - meaning they are not encapsulated properly by stacking / unstacking commands "q" / "Q".
Heal this by doing page.clean_contents() or page.wrap_contents() before your inserts and all is roses 😎:
I am wondering if I need to correct the media box and crop box first to insert a text properly? Given the media box is Rect(9.0, 9.0, 621.0, 801.0) and crop box is Rect(9.0, 0.0, 621.0, 792.0), the text's bottom left is located at (100, 109) instead of (100, 100).
I am wondering if I need to correct the media box and crop box first to insert a text properly?
No, don't do that. This will break the position of other stuff on the page.
I will need to adjust PyMuPDF to accomodate this weird situation.
In the meantime subtract page.MediaBox.y0 from the desired insertion y-position as a workaround. In "normal" cases this value will be zero and do no harm.
So in your case, page.insertText((100, 100 - page.MediaBox.y0), "hello") should make it.
Activity
JorjMcKie commentedon Jan 19, 2021
Your combination of mediabox / cropbox data make no sense: cropbox must be inside mediabox, which in turn must start at (0,0).
I will add checks to prevent things you have done.
VeryLazyBoy commentedon Jan 19, 2021
Thanks for your reply, but I do encounter some pdf with abnormal mediabox and cropbox. For example, the file I attached:
123.pdf
if I want to insert a text to this file, I find it would be inserted to a weird place
JorjMcKie commentedon Jan 19, 2021
This has other reasons:
Inside the page definitions (
/Contentsobject), geometry changes happen in a "non-localized" way - meaning they are not encapsulated properly by stacking / unstacking commands "q" / "Q".Heal this by doing
page.clean_contents()orpage.wrap_contents()before your inserts and all is roses 😎:JorjMcKie commentedon Jan 19, 2021
you can check for potential issues like that by looking at
page.is_wrapped.JorjMcKie commentedon Jan 19, 2021
For more background of this read section "Misplaced Item Insertions on PDF Pages" of this documentation chapter.
VeryLazyBoy commentedon Jan 19, 2021
This pdf seems to have multiple problems. I have tried your commands:
I am wondering if I need to correct the media box and crop box first to insert a text properly? Given the media box is
Rect(9.0, 9.0, 621.0, 801.0)and crop box isRect(9.0, 0.0, 621.0, 792.0), the text's bottom left is located at (100, 109) instead of (100, 100).JorjMcKie commentedon Jan 19, 2021
No, don't do that. This will break the position of other stuff on the page.
I will need to adjust PyMuPDF to accomodate this weird situation.
In the meantime subtract page.MediaBox.y0 from the desired insertion y-position as a workaround. In "normal" cases this value will be zero and do no harm.
So in your case,
page.insertText((100, 100 - page.MediaBox.y0), "hello")should make it.VeryLazyBoy commentedon Jan 19, 2021
Thanks for your kindly help!