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

Manually positioning text on the pdf #556

Closed
Doragon-S opened this issue Jun 2, 2021 · 8 comments · Fixed by #578
Closed

Manually positioning text on the pdf #556

Doragon-S opened this issue Jun 2, 2021 · 8 comments · Fixed by #578

Comments

@Doragon-S
Copy link

I'd like to manually control the position of text on a pdf page.

I thought it would be convenient to have a function that adds text to the pdf starting at a specified position

someone suggested making a table, setting it to a specific point, and adding the text into there. But I haven't figured out how to do that, and I found no tutorials online.

This is the way pdfBox adds text, and since I know nothing about how to build a programming library, I'm assuming that the document.add() function ends up boiling down to something similar. If so, would it be easy to set it up as a separate accessible function?

@Doragon-S
Copy link
Author

I guess a simple way to rewrite the question is this: Is there a way to access the basic actions of editing a pdf? (adding a page, adding a word to a specific place on the pdf, etc.)

@mkl-public
Copy link
Contributor

OpenPdf offers lower-level APIs, too.
In these cases you address a PdfContentByte instance (e.g. from PdfWriter.getDirectContent()/ PdfWriter.getDirectContentUnder() or from PdfStamper.getOverContent()/PdfStamper.getUnderContent()) with similar methods as you see in PDFBox.
There are some intermediary level methods, e.g. PdfContentByte.showTextAligned() and use of the ColumnText class.

@Doragon-S
Copy link
Author

Doragon-S commented Jun 6, 2021

Thank you so much!
Could I also ask you how to make a page? I keep getting the "document has no pages" error. I couldn't find anything about adding pages except pdfPage, and that one said it needed a rectangle, hashmap and a dictionary. And I could only find what a rectangle was. If you can help explain this, I'd be very appreciative.

@mkl-public
Copy link
Contributor

CAn you show your code so we can tell you what to change?

@Doragon-S
Copy link
Author

Doragon-S commented Jun 17, 2021

` val wr = PdfWriter.getInstance(document, FileOutputStream("filepath.pdf"))

val r = Rectangle(50f,50f,100f,100f)

wr.setBoxSize("LETTER", r)

wr.directContent.setFontAndSize(font.baseFont, font.size)

wr.directContent.showTextBasic(text)`

It doesn't put anything on the page. At least that I can see.

@mkl-public
Copy link
Contributor

Please be aware that you are working with a low-level API here. Thus, you have to enclose text objects with beginText() and endText() calls. Furthermore, you don't use a moveText variant, so your text might appear at a location you don't expect.

Also you do wr.setBoxSize("LETTER", r) according to the JavaDocs: Allowed names are: "crop", "trim", "art" and "bleed". Thus, your LETTER box might not do what you want.

@mluppi
Copy link
Contributor

mluppi commented Jul 17, 2021

@Doragon-S
I have answered your question on StackOverflow: https://stackoverflow.com/a/68419550/5024726

Additionally, I'm adding a new example to the existing collection in PR #578 which extends on the above mentioned answer. This should show sufficiently how to add text in boxes at absolute positions. It also shows how to deal with possible page breaks and how to add a new page.

In the most simplistic form, this is how to add a single box with some text:

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();

PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
ct.addElement(new Paragraph("Some text."));
ct.setSimpleColumn(200, 400, 500, 600);
ct.go();

document.close();

@Doragon-S
Copy link
Author

thank you

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

Successfully merging a pull request may close this issue.

3 participants