| JasperReports Ultimate Guide - Samples - Schema - Configuration - Functions - FAQ - API (Javadoc)
|
|
|
|
|
| JasperReports - PDF Forms Sample (version master-SNAPSHOT) | |
|
|
|
|
|
|
| Main Features in This Sample | |
|
| Generating Editable PDF Content |
|
|
|
||||
| top | |||||
|
|
|||||
![]() | Generating Editable PDF Content | Documented by Sanda Zaharia | |||
|
|
|||||
|
| Description / Goal |
| Shows how specific PDF export properties can be used in order to generate editable PDF content. | ||
|
| Since |
| 6.12.0 | ||
|
|
|||||
|
|
PDF Forms and Input Fields
The content of a PDF document can be edited inside a PDF form that contains various input fields such as textboxes, lists, comboboxes, checkboxes, radio groups, etc. Using the JasperReports library, one can generate these input fields by setting specific custom properties in the report. In the following sections we will see how these properties can be set and how they work in conjunction to generate editable content in PDF documents. Input Field Types In order to determine the appropriate content and layout of an input field we need to know its type first. The JasperReports library offers support for the following types:
The background color, forecolor and text formatting inside a generated input field are inherited from the JasperReports element. For the border style we need to set the following property, with values compatible with PDF border styles: net.sf.jasperreports.export.pdf.field.border.style. Possible values are:
Dashed', this style will be applied, otherwise the pen 'Solid' style will be considered by default.
The border thickness is rounded and recalculated to have one of the following values compatible with PDF: [1,2,3]. In case we need some input field content to be read-only, we can set the net.sf.jasperreports.export.pdf.field.read.only flag for that element. Example: <reportElement positionType="Float" mode="Opaque" x="0" y="0" width="25" height="25"> <property name="net.sf.jasperreports.export.pdf.field.type" value="Check"/> <property name="net.sf.jasperreports.export.pdf.field.border.style" value="Beveled"/> <property name="net.sf.jasperreports.export.pdf.field.read.only" value="true"/> </reportElement>Text Input Fields Textboxes are text input fields. They can be either single line or multi line textboxes. To specify which kind of textbox is considered, we can set the following flag property per report element: net.sf.jasperreports.export.pdf.field.text.multiline. The initial text content of the input field is provided by the net.sf.jasperreports.export.pdf.field.value custom property and in case that one is missing, then the actual text content of the JasperReports text element is used instead. Example: <reportElement positionType="Float" mode="Opaque" x="250" y="0" width="200" height="45" forecolor="#FFFF00" backcolor="#0000FF"> <property name="net.sf.jasperreports.export.pdf.field.type" value="Text"/> <property name="net.sf.jasperreports.export.pdf.field.text.multiline" value="true"/> <property name="net.sf.jasperreports.print.keep.full.text" value="true"/> <propertyExpression name="net.sf.jasperreports.export.pdf.field.value">"This\nis a multi\nline text inside a single line text field."</propertyExpression> </reportElement>List and Combobox Input Fields List and combobox input fields can be populated by setting the following property: net.sf.jasperreports.export.pdf.field.choices. Its value contains list items separated by a special character or set of characters provided by the net.sf.jasperreports.export.pdf.field.choice.separators property. If not set, the default separator is '|'. Example: <reportElement positionType="Float" mode="Opaque" x="0" y="0" width="200" height="95" forecolor="#00FFFF" backcolor="#00FF00"> <property name="net.sf.jasperreports.export.pdf.field.type" value="List"/> <property name="net.sf.jasperreports.export.pdf.field.choice.separators" value=","/> <property name="net.sf.jasperreports.export.pdf.field.choices" value="Un,Deux,Trois,Quatre,Cinq"/> </reportElement>In case we need the textbox in the combobox field to be editable, we can set the net.sf.jasperreports.export.pdf.field.combo.edit property per combobox element, while the a preselected value can be set using the net.sf.jasperreports.export.pdf.field.value property: <reportElement positionType="Float" mode="Opaque" x="250" y="0" width="200" height="25" forecolor="#0000FF" backcolor="#FFFF00"> <property name="net.sf.jasperreports.export.pdf.field.type" value="Combo"/> <property name="net.sf.jasperreports.export.pdf.field.choices" value="One|Two|Three|Four|Five"/> <property name="net.sf.jasperreports.export.pdf.field.value" value="Four"/> <property name="net.sf.jasperreports.export.pdf.field.combo.edit" value="true"/> </reportElement>Checkbox Input Field In order to properly set a checkbox we need to know how it's decorated and whether it's checked or no. We can set the checkbox appearance by using the net.sf.jasperreports.export.pdf.field.check.type property with the following possible values:
<reportElement positionType="Float" mode="Opaque" x="0" y="0" width="25" height="25"> <property name="net.sf.jasperreports.export.pdf.field.type" value="Check"/> <property name="net.sf.jasperreports.export.pdf.field.check.type" value="Square"/> <property name="net.sf.jasperreports.export.pdf.field.checked" value="true"/> </reportElement>Radio Groups Radio groups are radio option input fields grouped together so that only a single option can be selected at a time. To group together several radio options we can use the net.sf.jasperreports.export.pdf.field.name property. By assigning the same name for multiple JasperReports elements, they will be automatically grouped as radio options. To decorate these radio elements we can use the same net.sf.jasperreports.export.pdf.field.check.type property with the same possible values as for checkboxes. Also, to indicate which option is selected when opening the document, we can use the net.sf.jasperreports.export.pdf.field.checked property. Example:
<textField>
<reportElement positionType="Float" mode="Opaque" x="250" y="0" width="25" height="25">
<property name="net.sf.jasperreports.export.pdf.field.name" value="MyRadioGroup"/>
<property name="net.sf.jasperreports.export.pdf.field.type" value="Radio"/>
<property name="net.sf.jasperreports.export.pdf.field.check.type" value="Check"/>
</reportElement>
<box>
<pen lineWidth="1"/>
</box>
<textFieldExpression><![CDATA["This text does not matter in PDF export."]] ></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" mode="Opaque" x="250" y="0" width="25" height="25">
<property name="net.sf.jasperreports.export.pdf.field.name" value="MyRadioGroup"/>
<property name="net.sf.jasperreports.export.pdf.field.type" value="Radio"/>
<property name="net.sf.jasperreports.export.pdf.field.check.type" value="Circle"/>
<property name="net.sf.jasperreports.export.pdf.field.checked" value="true"/>
</reportElement>
<box>
<pen lineWidth="1"/>
</box>
<textFieldExpression><![CDATA["This text does not matter in PDF export."]] ></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" mode="Opaque" x="250" y="0" width="25" height="25">
<property name="net.sf.jasperreports.export.pdf.field.name" value="MyRadioGroup"/>
<property name="net.sf.jasperreports.export.pdf.field.type" value="Radio"/>
<property name="net.sf.jasperreports.export.pdf.field.check.type" value="Cross"/>
</reportElement>
<box>
<pen lineWidth="1"/>
</box>
<textFieldExpression><![CDATA["This text does not matter in PDF export."]] ></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" mode="Opaque" x="250" y="0" width="25" height="25">
<property name="net.sf.jasperreports.export.pdf.field.name" value="MyRadioGroup"/>
<property name="net.sf.jasperreports.export.pdf.field.type" value="Radio"/>
<property name="net.sf.jasperreports.export.pdf.field.check.type" value="Diamond"/>
</reportElement>
<box>
<pen lineWidth="1"/>
</box>
<textFieldExpression><![CDATA["This text does not matter in PDF export."]] ></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" mode="Opaque" x="250" y="0" width="25" height="25">
<property name="net.sf.jasperreports.export.pdf.field.name" value="MyRadioGroup"/>
<property name="net.sf.jasperreports.export.pdf.field.type" value="Radio"/>
<property name="net.sf.jasperreports.export.pdf.field.check.type" value="Square"/>
</reportElement>
<box>
<pen lineWidth="1"/>
</box>
<textFieldExpression><![CDATA["This text does not matter in PDF export."]] ></textFieldExpression>
</textField>
Known Limitations and Issues
Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/forms within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/forms/build/reports directory.
Then the report will open in the JasperReports internal viewer. You could open the PDF generated document and see how it differs from other generated outputs. |
||||
|
|
|||||
|
|
| © 2001- Cloud Software Group, Inc. www.jaspersoft.com |