iText - add content to existing PDF file

前端 未结 4 860
温柔的废话
温柔的废话 2020-11-27 13:09

I want to do the following with iText:

(1) parse an existing PDF file

(2) add some data to it, on the existing single page of the document (such as a timesta

4条回答
  •  执念已碎
    2020-11-27 13:42

    Gutch's code is close, but it'll only work right if:

    • There are no annotations (links, fields, etc), no Document Structure/Marked Content, no bookmarks, no document-level script, etc, etc, etc...
    • The page size happens to be A.4 (decent odds, but it won't work on any ol' PDF you happen to come across)
    • You don't mind losing all the original document metadata (producer, creation date, possibly author/title/keywords), and maybe the document ID. You can't copy the creation date and doc ID unless you do some pretty deep hackery on iText itself).

    The Approved Method is to do it the other way around. Open the existing document with a PdfStamper, and use the returned PdfContentByte from getOverContent() to write text (and whatever else you might need) directly to the page. No second document needed.

    And you can use a ColumnText to handle layout and such for you... no need to get down and dirty with beginText(),setFontAndSize(),drawText(),drawText()...,endText().

提交回复
热议问题