Editing a PDF file with Java - java

I'm trying to edit a existing PDF file. It is a file where I need to fill in some addresses and other stuff. I want to connect an addressbook to the application so the user can select a user from the addressbook and a part get filled in automatic.
My questions are:
Is it possible to edit a existing PDF file and fill in some fields (+/- 20), because I know there is Itext (http://www.itextpdf.com) but I read that the possiblities are very small.
Or can I better convert the PDF to JPG and get it as background. And create JLabels on the places where I need to fill in the fields. And then print the whole Frame on a A4.
Or are there better posibilities?
So what I need to do, step by step:
Select one of the PDF's (they are in the program)
Fill some fields with content/addresses
Print the PDF/Form with a printer

There is a toolkit given by Adobe named as Acrobat Forms Data Format (FDF) Toolkit which gives API for different languages to fill forms.
You can get the java code at the bottom of that page or check this link

We didn't edit existing PDF's but created totally new letters/reports/doco from our java app using iReport

You could use pdf form and edit field values programmatically using IText or Apache PDFBox (download pdfbox and see SetField.java example)

Related

How to dynamically resize text fields in Adobe Acrobat using Apache PDFBox?

I'm using Apache PDFBox to populate an existing Adobe Acrobat PDF template.
In this template I have fixed sizes of text fields in which I insert data from database.
In one text field the amount of data may vary so it would be great if text field could get bigger or smaller to adjust to the amount of text inserted.
The main problem is that I have another text fields below the one I want to resize dynamically so in case of making it bigger or smaller other text fields have to move relatively
I was searching for the solution but found only offers to resize font dynamically, but it doesn't satisfy my needs. I could create one text field and insert all of my data in it, but some of those text fields must have different color of text so it's important to have different text fields to be able to specify their color in code (as far as I know).
Is that even possible and if not what can I do to achieve the result I'm looking for?
P.S. Our code is based on templates from Adobe Acrobat and Apache PDFBox, so it's more preferable to keep it that way. But if this is not possible with those two, I will be glad to hear your recommendations on what should I use (open source first).
PDF Producer: Adobe Acrobat Pro DC (32-bit) 21.7.20102,
PDF Version: 1.6 (Acrobat 7.x),
Apache PDFBox Version: 2.0.26
You can find an example of my PDF below

How to display image on textpane?

I am working on a note taking application which is supposed to include image ,audio ,video along with text
So im including a feature to add links(like in evernote )which should display an icon on clicking which that file should open
My question is how write to and read from a file so that when a link is encountered an imageicon mus be displayed in textpane similary a link must be written in the file
I am already able to implement text links i want to know how to do the same with images(image links)
You can Use JEditorPane to use the functionality of both text and Images in java . This would also enable you to add support for custom text formats to the components of your application.
For details Refer : http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
As an alternative to JEditorPane: you might want to check out Flying Saucer, which can render XML/XHTML. If you use a suitable XML model internally, you might be able to render your content directly via CSS.

Use iText to produce formatted TextFields?

Is there any way in iText to format a TextField's input? I want to have a TextField accept a phone number "(###)###-####", but I don't want the user to have to format it when they enter it. Pdf supports masks on form fields, is there any way to do this in iText?
My current solution is to create the pdf in Acrobat, then populate known fields through iText. But that isn't ideal for this deployment. Ideally I'll have iText generate the entire form.
Thanks for all assistance in advance.
You can add JavaScript to your form that changes the content of fields. See for instance the Calculator example for a fun PDF that acts as a Calculator (obviously this app only works in a PDF viewer that supports JavaScript).
When you create a text field, you need to add an additional action with the setAdditionalActions() method. You can choose between different events: K for keystroke (e.g. useful if you want to change every character to uppercase when somebody fills out a form), Bl for blurred (useful to process the content of a field as soon as the focus is lost), etc.
You can write your own document-level JavaScript to format the fields. See calculator.js for the JavaScript used in the Calculator example. Or you can use one of the many AF methods that are predefined Adobe Reader, such as AFNumber_Format (I don't find an overview of the available methods right now).

Insert text to a generated pdf document

I have a library which generates pdf document with images.
I want to be able to add text after each image. What is the syntax for that? How to insert text into pdf documents?
I have to use the library I have, not another one.
First of all, mkl is correct, have a look at the specification for all of the details. PDF is an exact language, if you make mistakes they will routinely be punished severely once you open the PDF in viewers.
Secondly, when you think about putting text on the page, don't forget that besides the text operators to draw the text on the page, you'll also have to specify the font to use to draw this text. Which will include making sure there is a font resource included in the PDF file if your library doesn't automatically handle all of that for you.
If you want to cut corners (I shiver while writing this) and perhaps don't read the specification as thoroughly, try this.
1) Create a PDF file that looks more or less like what you want.
2) Use a tool such as pdfToolbox from callas (http://www.callassoftware.com/callas/doku.php/en:download) or Browser from Enfocus (http://www.enfocus.com/en/products/browser). Both of these tools allow you to investigate the low-level structure of a PDF file, including looking at the actual page description code. This will show you how fonts are embedded (if you have to do it yourself that could be very handy) and how text is rendered on the page (and how you set the font, size, color etc... to use).

How to add a hidden image to a PDF document?

I have a program. It outputs to pdf, but that is close to impossible to read from again. So i need a additional file attached to my PDF in order to be able to make it editable in my program. Attaching a file to PDF is a good idea, but that is visible to the user, which i don't wan't it to be.
An alternative is to hide my readable file format inside an image which would be added to the PDF somewhere to the top of the first page, before everything else... Even to metadata if that's possible...
That way I can extract image from pdf using a PDF library (iText), and read from it.
My question is how to add image to PDF to be as well hidden as it could be (visually and by accesibility). And it has to be in a place which would be same for any created document (somewhere on the top, or on the very bottom of the document, or to the part of the document which isn't displayed at all... I'm really guessing here, I'm not really familliar with PDF file format)...
Any ideas?
P.S. It's not really important which image is it, I could be a e.g. completly transparent image, 1x1 pixels.
I'm not sure what you mean by Image, but you can "extend" the PDF reference.
A PDF consists of objects: PDF numbers, PDF names, PDF strings, PDF arrays, PDF dictionaries, PDF streams. What you probably want, is to add an entry to a dictionary (pick one: the root dictionary, the info dictionary, the root of the page tree,...) that isn't defined in the PDF reference, so that it isn't rendered in a PDF viewer.
The key of such an entry must be a PDF name. To avoid clashes with existing names (names that are part of a current PDF spec, or will be part of a future spec), it is advised to register a four-letter key with ISO. For instance, Adobe registered adbe, iText registered ITXT and use that name with an underscore. For instance, ITXT_OriginalData would be a good name if we needed the functionality you describe.
The value of such an entry will be a PDF stream. In iText, you need the PdfStream class for this.

Categories