-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
insertText/insertTextbox create flipped text #425
Comments
Re: 1 This is not a bug either!Most probably the geometry of your page has been changed. See the documentation https://pymupdf.readthedocs.io/en/latest/faq/#misplaced-item-insertions-on-pdf-pages for how to best detect and handle this. Re 2 How to center textFirst, you need to define a rectangle within which the page number should occur. For example, define the rectangle height as 30 and let Re 3 Inserting text with mixtures of ASCII and non-ASCII charactersThat is not so easy. Best would be a font which supports both. See this example, which fills a text box with such a mixed text: import fitz
doc = fitz.open() # new PDF
ffile = "C:/windows/fonts/msyhl.ttc" # chinese font, light version
fsize = 10 # fontsize
lheight = fsize * 1.2 # line height
text = u"你好!hello, Hallo! 我很喜欢德国!德国是个好地方!" # text from Pengfei
text += "More text is following in English and justified ... let's see what happens."
page = doc.newPage()
page.insertFont(fontname="f0", fontfile=ffile)
rect = fitz.Rect(50, 100, 170, 250)
pivot = rect.bl
mat = fitz.Matrix(-30)
morph = (pivot, mat)
rc = page.insertTextbox(
rect, text, fontname="f0", rotate=90, align=fitz.TEXT_ALIGN_JUSTIFY, morph=morph
)
print("space surplus(+) or deficit(-): %g" % rc)
page.drawRect(rect, width=0.3, morph=morph)
doc.save(__file__ + ".pdf", garbage=3, deflate=True) |
thanks for the case of mixtures of ASCII and non-ASCII characters. Maybe there is no easy way, on the contrast, in Microsoft Word, we can select the text, assign Yes, commonly Chinese font supports Chinese, English letters, numbers and more. But in my case or to follow the rule, |
I understand. Then there is no "easy" way I am afraid and you probably have to do this (per page):
Good luck! |
For the first issue, as the approach I resloved, the page should be wrapped first with page.wrapContents(), if you are trying to edit an existing PDF document. Hope this information will help others, after I struggled for this a whole day. page = doc[page_no] |
1. I met one or two bug
I use PyMuPDF-1.16.10-cp36-none-win_amd64.whl, which is built on Dec 22, 2019, with Python3.6.
I try to add page number on pdf page. The prefer text postion is the center of the bottom. The initial code seems very clean
For some of my PDF files, the code works. But for some, no. For example
blank.pdf
, which is exported from a blank file from Typora 0.9.81(beta) on Windows 7 64 bits, and is available on my github.bug 1
The text, which should be "page 1", is flipped no matter I use
.
insertText
orinsertTextbox
as you can see onbug 2
And what is more, the text is not located around the center.
So how to fix it/them?
2. This is not a bug
how to make the center of the text located exactly the center of the bottom? In my case, some unicode characters are in text
3. This is not a bug
'new times roman' will be used for English letters and numbers, a unicode font( for example, "C:\Windows\Fonts\simsun.ttc") will be used for unicode characters
what is the easy way to assign font for the text?
thanks
The text was updated successfully, but these errors were encountered: