Example that explains how to rename a field. Files: When renaming a field, you need to respect the existing hierarchy. */ package. iText PDF, learn how to make a fillable PDF, create a form, fill out and flatten a form. They are usually added on top of the existing content. . box or list field), buttons (push buttons, check boxes and radio buttons), and signature fields. Gets a template layer to create a signature appearance. Gets a new signature field name that doesn’t clash with any existing name.
|Published (Last):||15 June 2010|
|PDF File Size:||3.75 Mb|
|ePub File Size:||15.63 Mb|
|Price:||Free* [*Free Regsitration Required]|
In the previous chapters, we’ve created PDF documents by adding content to a page. It didn’t matter if we were adding high-level objects e. In this chapter, we’ll add content of a different nature.
We’ll add interactive features, known as annotations. Annotations aren’t part of the content stream. They are usually added on top of the existing content. There are many different types of annotations, many of which allow user interaction.
We’ll start with a series of simple examples.
On top of the text, we’ve added a green text annotation. Most of the code of the TextAnnotation example is identical to the Hello World example. The only difference is that we create and add an annotation:.
We define the location of the text annotation using a Rectangle. We set the color, title a PdfStringcontents a Stringand the open status of the annotation. We ask the PdfDocument for its first page and add the annotation. You can open that URL by clicking the annotation. This is a link annotation. As the annotation is part of a sentence, it wouldn’t be convenient if we had to calculate the position of the word “here”.
Fortunately, we can wrap the link annotation in a Link object and iText will calculate the Rectangle automatically. The LinkAnnotation example shows how it’s done. In line 2, we create a URI action that opens the iText web site. We use this action for the link annotation. We then create a Link object. This is a basic building block that accepts a link annotation as parameter.
This link annotation won’t be added to the content stream —because annotations aren’t part of the content stream.
Instead it will be added to the corresponding page at the corresponding coordinates. Making text clickable doesn’t change the appearance of that text in the content stream.
In our example, we underlined the word “here” so that we know where to click.
Every type of annotation requires its own type of parameters. The LineAnnotation shows what is needed to create this appearance. In this example, we add the annotation to a newly created page. There’s no Document instance involved in this example. With iText, you can add all of these annotation types to a PDF document, but in the context of this tutorial, we’ll only look at one more example before we move on to interactive forms.
Looking at the TextMarkupAnnotation example, we see that we really need a separate tutorial to understand what all the nuts and bolts used in this code snippet are about. In the next section, we’ll create an interactive form consisting of different form fields.
Each form field in that form will correspond with a widget annotationbut those annotations will be created implicitly. In the next example, we’re going to create an interactive form based on AcroForm technology. This technology was introduced in PDF 1. When text doesn’t fit into the available text area of an HTML form, that field can be resized.
The content of a list field can be updated on the fly based on a query to the server. In short, an HTML form can be very dynamic. That isn’t true for interactive forms based on AcroForm technology. Such a form can best be compared with a paper form where every field has its fixed place and its fixed size.
The idea of using PDF forms for collecting user data in a web browser has been abandoned over the years. HTML forms are much more user friendly for online data collection.
That doesn’t mean that AcroForm technology has become useless. Interactive PDF forms are very common in two specific use cases:. When the form is the equivalent of digital paper. In some cases, there are strict formal requirements with respect to a form. It is important that the digital document is an exact replica of the corresponding form. Every form that is filled out needs to comply to the exact same formal requirements. When the form isn’t used for data collection, but as a template.
On this form, you have different fields for the name of the person who bought the ticket, the date and the time of the event, the row and the seat number, and so on.
When people buy a ticket, you don’t need to regenerate the complete voucher, you can take the form and simply fill it out with the appropriate data. In both use cases, the form will be created manually, for instance using Adobe software, LibreOffice, or any other tool with a graphical user interface. You could also create such a form programmatically, but there are very few use cases that would justify using a software library to create a form or a template, instead of using a tool with a GUI.
Nevertheless, we’re going to give it a try.
Add Digital Signature Fields in existing pdf
We see these fields because they are represented by a widget annotation. This widget annotation is created implicitly when we create a field.
The second parameter is a Boolean indicating if a new form needs to be created if there is no existing form.
As we’ve just created the Documentthere is no form present yet, so that parameter should be true:. Now we can start adding fields. We’ll use a Rectangle to define the dimension of each widget annotation and its position on the page. The createText method needs a PdfDocument instance, a Rectanglethe name of the field, and a default value in this case, the default value is an empty String. Note that the label of the field and the widget annotation are two different things.
We’ve added “Full name: That Paragraph is part of the content stream. The field itself doesn’t belong in the content stream. It’s represented using a widget annotation. We create a radio field for choosing a language. Note that there is one radio group named language with five unnamed button fields, one for each language that can be chosen:.
Only one language can be selected at a time. If multiple options could apply, we should have used check boxes. In the next snippet, we’ll introduce three check boxes, named experience0experience1experience As you can see, we use the createCheckBox method with the following parameters: A check box has two possible values: It’s also possible to have people select one or more option from a list or a combo box.
In PDF terminology, we call this a choice field. Choice fields can be configured in a way that people can select only one of the options, or several options. In our example, we create a combo box. Our choice field is named “shift” and it offers existng options of which “Any” is selected by default. We also see a multi-line field in the form. As opposed to the regular text field, where you can only add text in a single line, text in this field will be wrapped if it doesn’t fit on asd single line.
In a real-world example we’d use a submit button that allows people to submit signaturf data they’ve entered in the form to a server.
iText – Add Digital Signature Fields in existing pdf
We conclude the example by adding a reset button that will reset a selection of fields to their initial value when the button is clicked. If signatur want to create a PDF form using iText, you now have a fair idea of how it’s done. In many cases, it’s a much better idea to create a form manually, using a tool with a graphical user interface. You are then going to use iText to fill out this form automatically, for instance using data from a database.
When we created our form, we could have defined default values, so that the form was filled out as shown in Figure 4. We can still add these values after we’ve created the form. The CreateAndFill example shows us how.
We asked the PdfAcroForm to which we’ve added all the exidting field for its fields, and we get a Map consisting of key-value pairs with the names and PdfFormField objects of each field.
We can get the PdfFormField instances one by one, and set their value. Granted, this doesn’t make much sense. It would probably have been smarter to set the correct value right away the moment you create each field.
A more common use case is to pre-fill an existing form. See the FillForm example. We introduce a new object in line 2. In this case, src holds the path to an existing form. In line 2, we create a PdfWriter that will write a new version of the source file.