0

I have a requirment to embed a image into PDF. The PDF size and image size are the same (I have everything working up to this point). The difficult part is the end user has some text that they need to enter above some areas of the image, these areas will be predefined, but the text that goes in those positions are not. I know its possible to create fillable fields using tools like iText.. I have searched for days on how to use iText to accomplish setting up fillable field and positioning text of any kind to an absolute position, but frustratingly have made zero progress. So I could really use someones expertise on this subject. Thanks

  • Can you clarify your question. Seems like you are asking something that is very simple, but you made your question so complex that you confuse me. I've read it a couple of times now, and although the title is clear, the body of your question seems to be about something different. Please clarify! – Bruno Lowagie Feb 28 '16 at 08:45
  • As @Bruno said, most likely what you want is simple to do. I do wonder, though, why you want a fillable field there. Or does someone later have to manually manipulate the text you put there? – mkl Feb 28 '16 at 08:52
  • @mkl my guess is that he wants to use iText the way Cirque du Soleil is using iText. Cirque du Soleil has really nice ticket templates that consist of an image and a number of form fields. These form fields are then filled out with the name of the person who purchased the ticket, a date, a row number, a seat number, and so on. Maybe VCDeveloper is developing a system to create such tickets. – Bruno Lowagie Feb 28 '16 at 09:14
  • @mkl As explained in my answer, maybe the question is as simple as: "which coordinates do I need to use." It's hard to tell when people don't do an effort writing a clear question. It's also very ungrateful when an answer trying to guess what was asked gets downvoted as was done here: http://stackoverflow.com/a/35671700/1622493 If the answer doesn't really answer the question, people should at least explain *why* it's a bad answer, so that the answer can be updated. – Bruno Lowagie Feb 28 '16 at 09:17
  • Strange indeed, such down votes... @Bruno – mkl Feb 28 '16 at 09:54
  • Consider I am attempting to create editable PDF of postcard where user can enter name and address. The PDF to be generated out of JAVA would only include the image of postcard template, the mailing name and address is missing. I would like the user to be able to open the PDF into adobe reader enter name, address info.. the location of these editable fields must be print in a specific coordinate above image in order for it to be mail-able.. After user edits they print the single postcard with address info they entered. My requirement is similar to this but we are not creating postcards. – VCDeveloper Feb 29 '16 at 19:42

1 Answers1

1

Your question isn't entirely clear, and the answer is different if you make different assumptions.

Assumption 1: Suppose that you have a PDF that consists of an image that fills the complete page. You now want to add text fields at positions that you know in advance.

In this case, you'd use PdfStamper and the addAnnotation() method as is done in the answer to the StackOverflow question How can I add a new AcroForm field to a PDF?

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// create a field for which you define the coordinates using a Rectangle
stamper.addAnnotation(field, 1);
stamper.close();

Here we add field to page 1 using the addAnnotation() method.

Now for the question: how to create that field object. That's easy. See for instance the ReadOnlyField example:

Rectangle rect = new Rectangle(36, 720, 144, 806);
TextField tf = new TextField(stamper.getWriter(), rect, "text");
tf.setOptions(TextField.MULTILINE);
PdfFormField field = tf.getTextField();

Note that I use the coordinate of the lower-left corner (36, 720) and the upper-right corner (144, 806) to create a Rectangle object. I create a TextField using the stamper's PdWriter instance, that rect and I give that field the name text. Assuming that you want the text that is entered to be wrapped, I made the text field a MULTILINE field. I then obtain a PdfFormFieldinstance from the TextField object.

Assumption 2: you are creating a PDF document from scratch in which you create a page to which you add an image with the same size of the page. Now you just want to add form fields to add text. There are many examples on how to define and add a text field on the official iText web site: MultiLineField, TextFields, GenericFields, CreateFormInTable, and many more.

You'll also find a good example in the question How to add a hidden text field?. The example in the question shows how to add a visible text field; the answer shows how to hide it.

In this example, x and y are the coordinates of the lower-left corner, whereas w and h are the width and the height of the field:

TextField field = new TextField(writer, new Rectangle(x, y - h, x + w, y), name);
field.BackgroundColor = new BaseColor(bgcolor[0], bgcolor[1], bgcolor[2]);
field.BorderColor = new BaseColor(
    bordercolor[0], bordercolor[1], bordercolor[2]);
field.BorderWidth = border;
field.BorderStyle = PdfBorderDictionary.STYLE_SOLID;
field.Text = text;
writer.AddAnnotation(field.GetTextField());

This is an iTextSharp example (written in C#), but it's very easy to port it to Java.

Finally: maybe you already knew all of this. Maybe you were just wondering what all these coordinates are about. The answer to this question can also be found on the official iText web site:

Almost all of the links in my answer refer to examples and answers that were written in answer to previous questions on StackOverflow. Please refrain from saying things like I have searched for days on how to use iText to accomplish setting up fillable field and positioning text of any kind to an absolute position because it is hard to believe for people who know that all the answers can be found on the official iText web site. Your boss might wonder which sites you were searching for all those days.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks, Ill give this a shot. – VCDeveloper Feb 29 '16 at 19:50
  • One more question.. Is it possible to have annotation include date stamp? The date would be also be editable, and also should be current date. – VCDeveloper Mar 02 '16 at 20:13
  • @VCDeveloper Please post another question if you want another answer. Please be more accurate. Adding a date in the text field is so trivial that it's hard to believe you need to ask the question. Maybe you're asking something that isn't trivial so you have to be more specific so that we can assess if your question is challenging or if you're just being lazy... – Bruno Lowagie Mar 02 '16 at 20:22
  • Yes, although I am fairly new to JAVA creating static date stamp at time of pdf generation is simple, thats NOT what I was asking. I dont see why the need for passing such judgment, its unwarranted. – VCDeveloper Mar 03 '16 at 17:39