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).
Related
Doing linguistics and phonetics, I often need to use certain special phonetic symbols. Although I'm using a special keyboard layout that enables me to write some of those characters by typing, they key combinations can often get both quite complex and highly repetitive, so I would like to create a litle app that would contain some buttons, perhaps, each of them capable of sending a specified (phonetic) symbol to whatever the current cursor position is, no matter what window on one's screen is in focus.
Is anything of this sort possible to do in Java?
I've seen a solution that copies the values into clipboard and then pastes them (Java paste to current cursor position), but that is not a very clean way to do it, is it? Is there a way better than just pasting the charactedr(s) via ctrl+V?
Many thanks for any help or advice in advance!
P.
You can use the AWT Robot to generate key press events. This will not provided the ability to insert arbitrary unicode characters but you can combine it with the technique you already described: transfer the unicode characters to the clipboard and generate a CTRL+V key event afterwards. You can try to save and restore the original clipboard content but this will work with types supported by Java only.
The focus problem mentioned in the comments can be solved by setting the window to not receive the focus via Window.setFocusableWindowState with an argument of false.
An alternative is to provide the unicode text via drag&drop. Most applications support dropping text in their input fields. The code for exporting the text is very similar as both, clipboard and d&d use the same interfaces in Java.
Can i create an expandable list in pdf files. Expandable list will be of the form :
+Item1
+Item2
-Item3
-Subitem3.1
-Subitem3.2
+Item4
-Item5
-Subitem5.1
-Subitem5.2
-Subitem5.3
Also I need to create the pdf file from Java(I was thinking of using iText, is another library better/easier?). Is this possible. Or is a report in some other standard format(not pdf or html) an easier way out.
First this: I'm the creator of iText, so forgive me for not pointing you to other solutions ;-)
Now for your question: you're asking for dynamic functionality (a tree structure that opens/closes upon user interaction) inside a PDF document.
The most obvious answer is: this isn't possible. When creating PDF, think of paper. Can you print a tree structure on paper that opens/closes when the end user touches the paper? No, you can't, therefore you're asking something that isn't possible in PDF.
The less obvious answer is: it depends. What type of PDF are we talking about?
If you're talking about an interactive XFA form, then you may be able to achieve what you want. The XML Forms Architecture (XFA) is an XML specification that can be used to define interactive forms. When you use XFA, the PDF is nothing more than a container for XML. This XML is rendered dynamically inside Adobe Reader. How to create an XFA form? I only know about two products: Adobe LiveCycle Designer and Avoka Smart Forms Designer.
If you're talking about 'regular PDF', then one option is to embed a swf file. In this case, the tree structure will be rendered by Flash player (which could be a disadvantage, because this might not work with all PDF viewers). Another disadvantage: the tree structure will be confined to a fixed rectangle on a fixed page.
Finally: you can have create such a structure in the bookmarks panel. In PDF terminology, those bookmarks are called Outlines. Obviously, the tree structure won't be a part of the printable content. It will be visible in a separate panel in your PDF viewer.
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)
Actually, still now I have not got the exact way to add different types of annotation/lines/boxes/highlights using the mouse directly on PDF.
I have tried different API (pdfbox, icepdf, iText, aspose) and successfully able to add text, text annotation, box annotation, images, but for each one i have some issue like:
when i add object, it add in a new page
when i add object it remove the old data
i want use the existing ICEPDF viewer to add text, but it does not allow it. and if i try to mix other API with ICEPDF, then I am not able to get the mouse click position where need to add.
There is only one API (ICE PDF) which allow to add box annotation direct on GUI by mouse but it does allow other things.
If not possible in pdf viewer then is there any class which will allow to do this?
I'm also open for any commercial library/class with a one time fee to do that.
I think BFO have added quite a few annotation tools. Have you looked at their tool?
I'm trying and failing to understand how to use Java's text editor components to colorize text as you insert it. I don't want or need a fully featured syntax highlighting library.
Basically, I have a JTextField (or some other JText... component), and a list of words. I want any words in the field that appear in the list to be red, and the rest of the words be green. So for example, if "fire" is in the list, "fir" would appear green and "fire" would appear red.
I've tried using a JTextPane and a DefaultStyledDocument, using a KeyListener to go over the text in the document and using AbstractStyledDocument.replace to replace the existing words with versions that have the correct attributes. This didn't do anything. What am I doing wrong?
Neither JTextPane nor JTextField isn't able to present formatted text, i.e text having more than one format. For text-editor-like capabilities like you'd find in WordPad or HTML, the component to use is the JEditorPane or its descendant, JTextPane.
The simplest thing you can do is set the ContentType of the JEditorPane to "text/html" and simply set its text to a string containing HTML. The Java structured text components are surprisingly competent with HTML; you can display tables and/or DIVs, and there is support for much of CSS2. Simplest to do your styles inline, but you can even do external style hrefs.
If you want to get fancy programmatically, you can access the DocumentModel and create text from spans of text each having their own formatting. The DocumentModel works essentially like a programmable text editor.
EDIT: Re-reading your question, I see my answer doesn't quite address it. Since you want multi-colored text JEditorPane is your only option; but rather than just piping in pre-colored text via HTML or such, you'll have to put a listener on your document model to catch changes introduced when you type; and after every document change you'll want to examine the text (again from the Document model) for text that should or should not be highlighted, and you'll want to apply formatting to certain runs of text.
There are devils in the details, but this should get you started.