问题
Can we make placeholders for a text in a pdf and mark them with an id (similar to html tags) and just fill that placeholder with our text, of whichever length in Java, using PdfBox?
回答1:
Can we make placeholders for a text in a pdf and mark them with an id (similar to html tags) and just fill that placeholder with our text, of whichever length
No, at least not without a great deal of coding around it.
The reason is that PDF is a format for documents with a finished layout.
If you fill that placeholder with your text, of whichever length, in particular in case of filling with a long text, the contents of the document would have to be re-flowed, text following the placeholder would have to be moved down, text already at the bottom of the page body would have to be moved to the next page, etc.
As PDF documents in general don't contain information on stuff like margins, text alignments, etc., that task is severely non-trivial.
(There also are other issues, e.g. embedded font subsets without the glyphs of your replacement text or backgrounds or borders without linkage to the "backgrounded" or "bordered" text.)
I'm not aware of an automatized general purpose implementation of that task, in particular not in free PDF libraries.
来源:https://stackoverflow.com/questions/49729775/placeholders-for-a-text-in-a-pdf-java-pdfbox