Skip to content
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

How to use PyMuPDF to generate vertical text? #653

Closed
kingqiuol opened this issue Sep 11, 2020 · 10 comments
Closed

How to use PyMuPDF to generate vertical text? #653

kingqiuol opened this issue Sep 11, 2020 · 10 comments
Assignees
Labels

Comments

@kingqiuol
Copy link

No description provided.

@JorjMcKie
Copy link
Collaborator

The simplest way: page.insertTextbox(). Specify desired rectangle and angle 0, 90, 180, 270. Text will then be written either:

  • top-left to bottom-right, or
  • top-right to bottom-left, or
  • bottom-right to top-left, or
  • bottom-left to top-right

The are also lots of other ways, including writing in arbitrary angles (other than int multiples of 90 degrees).

@JorjMcKie
Copy link
Collaborator

Any feedback on this?

@cges30901
Copy link
Contributor

I think he/she is probably referring to vertical writing in CJK languages. rotate is not useful here.

Here is my example script, modified from this example and #329

import fitz
doc = fitz.open()  # new or existing PDF
page = doc.newPage()  # new or existing page via doc[n]
r1 = fitz.Rect(100,100,200,200)
text = "你好!hello!Hallo! 我很喜欢德国!德国是个好地方"
page.insertTextbox(r1, text, fontname="china-s", rotate=270)

doc.save("text.pdf")

If rotate is used, characters are also rotated, which is not desired.

@JorjMcKie
Copy link
Collaborator

@cges30901 - thanks for your comment! You are probably very right.
But @kingqiuol did not even honor us with more than just the title - so we are left with guessing ...

@JorjMcKie
Copy link
Collaborator

So if your assumption applies, the simplest, temporary solution would be to insert linebreaks after each character, wouldn't it?

@JorjMcKie
Copy link
Collaborator

I am about to finish the next PyMuPDF version. You can do something like this with it:

import fitz

font = fitz.Font("china-s")
doc = fitz.open()
page = doc.newPage()
tw = fitz.TextWriter(page.rect)
text = "直至在專輯製作過程中方被重新發現。"
pos = fitz.Point(250, 70)
fsize = 11

tw.appendv(pos, text, font=font, fontsize=fsize)
tw.writeText(page)

doc.save("some.pdf")

Produces this on the PDF page:
grafik

@cges30901 - do you believe this is the required functionality?

@kingqiuol
Copy link
Author

kingqiuol commented Sep 14, 2020

@JorjMcKie @cges30901 Thank you for your answer. I use CJK languages and have solved this problem according to @cges30901

@kingqiuol
Copy link
Author

kingqiuol commented Sep 14, 2020

@JorjMcKie ,I used your answer, but something went wrong,expect the next PyMuPDF version.

@JorjMcKie
Copy link
Collaborator

The new version 1.17.7 is underway to be uploaded. Look at PyPI in about half an hour.

@asnahu
Copy link

asnahu commented Jan 2, 2022

I am about to finish the next PyMuPDF version. You can do something like this with it:

import fitz

font = fitz.Font("china-s")
doc = fitz.open()
page = doc.newPage()
tw = fitz.TextWriter(page.rect)
text = "直至在專輯製作過程中方被重新發現。"
pos = fitz.Point(250, 70)
fsize = 11

tw.appendv(pos, text, font=font, fontsize=fsize)
tw.writeText(page)

doc.save("some.pdf")

Produces this on the PDF page: grafik

@cges30901 - do you believe this is the required functionality?

Is this possible translate/rotate an oriented vertical text PDF to horizontal?

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

No branches or pull requests

4 participants