Skip to content

Commit 0c32b7a

Browse files
committed
Support running elements for formfields
DEVSIX-7760
1 parent 5956db5 commit 0c32b7a

File tree

7 files changed

+61
-27
lines changed

7 files changed

+61
-27
lines changed

forms/src/main/java/com/itextpdf/forms/fields/PdfFormAnnotation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,8 @@ protected void drawRadioButtonAndSaveAppearance(String value) {
710710
// Create it one time and re-set properties during each widget regeneration.
711711
formFieldElement = new Radio("");
712712
}
713+
final boolean wasChecked = Boolean.TRUE.equals(
714+
formFieldElement.<Boolean>getProperty(FormProperty.FORM_FIELD_CHECKED));
713715
setModelElementProperties(getRect(getPdfObject()));
714716

715717
// First draw off appearance
@@ -732,7 +734,7 @@ protected void drawRadioButtonAndSaveAppearance(String value) {
732734
canvas.add(formFieldElement);
733735
normalAppearance.put(new PdfName(value), xObject.getPdfObject());
734736
}
735-
737+
((Radio) formFieldElement).setChecked(wasChecked);
736738
getWidget().setNormalAppearance(normalAppearance);
737739
formFieldElement.setInteractive(true);
738740
}
@@ -945,6 +947,8 @@ protected void drawCheckBoxAndSaveAppearance(String onStateName) {
945947
}
946948
reconstructCheckBoxType();
947949
createCheckBox();
950+
final boolean wasChecked = Boolean.TRUE.equals(
951+
formFieldElement.<Boolean>getProperty(FormProperty.FORM_FIELD_CHECKED));
948952
final PdfDictionary normalAppearance = new PdfDictionary();
949953
((CheckBox) formFieldElement).setChecked(false);
950954
final PdfFormXObject xObjectOff = new PdfFormXObject(
@@ -981,6 +985,7 @@ protected void drawCheckBoxAndSaveAppearance(String onStateName) {
981985
parent.checkType.getValue())));
982986
getWidget().put(PdfName.MK, mk);
983987
formFieldElement.setInteractive(true);
988+
((CheckBox) formFieldElement).setChecked(wasChecked);
984989
}
985990

986991
static void setMetaInfoToCanvas(Canvas canvas) {

forms/src/main/java/com/itextpdf/forms/form/renderer/AbstractFormFieldRenderer.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ This file is part of the iText (R) project.
4343
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
4444
import com.itextpdf.layout.properties.OverflowPropertyValue;
4545
import com.itextpdf.layout.properties.Property;
46+
import com.itextpdf.layout.properties.UnitValue;
4647
import com.itextpdf.layout.renderer.BlockRenderer;
4748
import com.itextpdf.layout.renderer.DrawContext;
4849
import com.itextpdf.layout.renderer.IRenderer;
4950
import com.itextpdf.layout.tagging.IAccessibleElement;
5051

52+
import java.util.HashMap;
5153
import java.util.List;
54+
import java.util.Map;
55+
import java.util.Map.Entry;
5256
import org.slf4j.LoggerFactory;
5357

5458
/**
@@ -294,12 +298,37 @@ protected void writeAcroFormFieldLangAttribute(PdfDocument pdfDoc) {
294298
* Deletes all margin properties. Used in {@code applyAcroField} to not apply margins twice as we already use area
295299
* with margins applied (margins shouldn't be an interactive part of the field, i.e. included into its occupied
296300
* area).
301+
*
302+
* @return the map of deleted margins
297303
*/
298-
void deleteMargins() {
304+
Map<Integer, Object> deleteMargins() {
305+
306+
final Map<Integer, Object> margins = new HashMap<>();
307+
margins.put(Property.MARGIN_TOP, this.modelElement.<UnitValue>getOwnProperty(Property.MARGIN_TOP));
308+
margins.put(Property.MARGIN_BOTTOM, this.modelElement.<UnitValue>getOwnProperty(Property.MARGIN_BOTTOM));
309+
margins.put(Property.MARGIN_LEFT, this.modelElement.<UnitValue>getOwnProperty(Property.MARGIN_LEFT));
310+
margins.put(Property.MARGIN_RIGHT, this.modelElement.<UnitValue>getOwnProperty(Property.MARGIN_RIGHT));
311+
299312
modelElement.deleteOwnProperty(Property.MARGIN_RIGHT);
300313
modelElement.deleteOwnProperty(Property.MARGIN_LEFT);
301314
modelElement.deleteOwnProperty(Property.MARGIN_TOP);
302315
modelElement.deleteOwnProperty(Property.MARGIN_BOTTOM);
316+
return margins;
317+
}
318+
319+
/**
320+
* Applies the properties to the model element.
321+
*
322+
* @param properties the properties to apply
323+
*/
324+
void applyProperties(Map<Integer, Object> properties) {
325+
for (Entry<Integer, Object> integerObjectEntry : properties.entrySet()) {
326+
if (integerObjectEntry.getValue() != null) {
327+
modelElement.setProperty(integerObjectEntry.getKey(), integerObjectEntry.getValue());
328+
} else {
329+
modelElement.deleteOwnProperty(integerObjectEntry.getKey());
330+
}
331+
}
303332
}
304333

305334
/**

forms/src/main/java/com/itextpdf/forms/form/renderer/ButtonRenderer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ This file is part of the iText (R) project.
5353
import com.itextpdf.layout.renderer.LineRenderer;
5454
import com.itextpdf.layout.renderer.ParagraphRenderer;
5555

56+
import java.util.List;
57+
import java.util.Map;
5658
import org.slf4j.Logger;
5759
import org.slf4j.LoggerFactory;
5860

59-
import java.util.List;
60-
6161
/**
6262
* The {@link AbstractTextFieldRenderer} implementation for buttons.
6363
*/
@@ -251,7 +251,7 @@ protected void applyAcroField(DrawContext drawContext) {
251251
PdfDocument doc = drawContext.getDocument();
252252
Rectangle area = getOccupiedArea().getBBox().clone();
253253
applyMargins(area, false);
254-
deleteMargins();
254+
final Map<Integer, Object> margins = deleteMargins();
255255
PdfPage page = doc.getPage(occupiedArea.getPageNumber());
256256

257257
Background background = this.<Background>getProperty(Property.BACKGROUND);
@@ -282,6 +282,7 @@ protected void applyAcroField(DrawContext drawContext) {
282282
forms.addField(button, page);
283283

284284
writeAcroFormFieldLangAttribute(doc);
285+
applyProperties(margins);
285286
}
286287

287288
/**

forms/src/main/java/com/itextpdf/forms/form/renderer/CheckBoxRenderer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.forms.form.renderer;
2424

25-
import com.itextpdf.forms.PdfAcroForm;
2625
import com.itextpdf.forms.fields.CheckBoxFormFieldBuilder;
2726
import com.itextpdf.forms.fields.PdfButtonFormField;
2827
import com.itextpdf.forms.fields.PdfFormAnnotation;
@@ -53,6 +52,8 @@ This file is part of the iText (R) project.
5352
import com.itextpdf.layout.renderer.IRenderer;
5453
import com.itextpdf.layout.renderer.ParagraphRenderer;
5554

55+
import java.util.Map;
56+
5657

5758
/**
5859
* The {@link AbstractFormFieldRenderer} implementation for checkboxes.
@@ -230,7 +231,8 @@ protected void applyAcroField(DrawContext drawContext) {
230231
final String name = getModelId();
231232
final PdfDocument doc = drawContext.getDocument();
232233
final Rectangle area = flatRenderer.getOccupiedArea().getBBox().clone();
233-
deleteMargins();
234+
235+
final Map<Integer, Object> margins = deleteMargins();
234236
final PdfPage page = doc.getPage(occupiedArea.getPageNumber());
235237
final CheckBoxFormFieldBuilder builder = new CheckBoxFormFieldBuilder(doc, name).setWidgetRectangle(area)
236238
.setConformanceLevel(this.<PdfAConformanceLevel>getProperty(FormProperty.FORM_CONFORMANCE_LEVEL));
@@ -254,6 +256,7 @@ protected void applyAcroField(DrawContext drawContext) {
254256

255257
PdfFormCreator.getAcroForm(doc, true).addField(checkBox, page);
256258
writeAcroFormFieldLangAttribute(doc);
259+
applyProperties(margins);
257260
}
258261

259262
/**

forms/src/main/java/com/itextpdf/forms/form/renderer/InputFieldRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ This file is part of the iText (R) project.
3434
import com.itextpdf.kernel.pdf.PdfDocument;
3535
import com.itextpdf.kernel.pdf.PdfPage;
3636
import com.itextpdf.kernel.pdf.PdfString;
37-
import com.itextpdf.layout.element.Paragraph;
38-
import com.itextpdf.layout.element.Text;
3937
import com.itextpdf.layout.font.FontProvider;
4038
import com.itextpdf.layout.layout.LayoutContext;
4139
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
@@ -49,6 +47,7 @@ This file is part of the iText (R) project.
4947
import com.itextpdf.layout.renderer.ParagraphRenderer;
5048

5149
import java.util.List;
50+
import java.util.Map;
5251
import org.slf4j.Logger;
5352
import org.slf4j.LoggerFactory;
5453

@@ -164,7 +163,7 @@ protected void applyAcroField(DrawContext drawContext) {
164163
final PdfDocument doc = drawContext.getDocument();
165164
final Rectangle area = this.getOccupiedArea().getBBox().clone();
166165
applyMargins(area, false);
167-
deleteMargins();
166+
final Map<Integer, Object> margins = deleteMargins();
168167
final PdfPage page = doc.getPage(occupiedArea.getPageNumber());
169168
final float fontSizeValue = fontSize.getValue();
170169

@@ -196,6 +195,7 @@ protected void applyAcroField(DrawContext drawContext) {
196195
PdfFormCreator.getAcroForm(doc, true).addField(inputField, page);
197196

198197
writeAcroFormFieldLangAttribute(doc);
198+
applyProperties(margins);
199199
}
200200

201201
/**

forms/src/main/java/com/itextpdf/forms/form/renderer/RadioRenderer.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ This file is part of the iText (R) project.
2828
import com.itextpdf.forms.fields.PdfFormAnnotation;
2929
import com.itextpdf.forms.fields.PdfFormCreator;
3030
import com.itextpdf.forms.fields.RadioFormFieldBuilder;
31-
import com.itextpdf.forms.util.DrawingUtil;
3231
import com.itextpdf.forms.form.FormProperty;
3332
import com.itextpdf.forms.form.element.Radio;
33+
import com.itextpdf.forms.util.DrawingUtil;
3434
import com.itextpdf.kernel.colors.Color;
3535
import com.itextpdf.kernel.colors.ColorConstants;
3636
import com.itextpdf.kernel.exceptions.PdfException;
@@ -52,6 +52,8 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.layout.renderer.IRenderer;
5353
import com.itextpdf.layout.renderer.ParagraphRenderer;
5454

55+
import java.util.Map;
56+
5557
/**
5658
* The {@link AbstractFormFieldRenderer} implementation for radio buttons.
5759
*/
@@ -168,7 +170,7 @@ protected void applyAcroField(DrawContext drawContext) {
168170
PdfDocument doc = drawContext.getDocument();
169171
PdfAcroForm form = PdfFormCreator.getAcroForm(doc, true);
170172
Rectangle area = flatRenderer.getOccupiedArea().getBBox().clone();
171-
deleteMargins();
173+
final Map<Integer, Object> margins = deleteMargins();
172174

173175
PdfPage page = doc.getPage(occupiedArea.getPageNumber());
174176
String groupName = this.<String>getProperty(FormProperty.FORM_FIELD_RADIO_GROUP_NAME);
@@ -177,12 +179,10 @@ protected void applyAcroField(DrawContext drawContext) {
177179
}
178180

179181
PdfButtonFormField radioGroup = (PdfButtonFormField) form.getField(groupName);
180-
boolean addNew = false;
181182
if (null == radioGroup) {
182183
radioGroup = new RadioFormFieldBuilder(doc, groupName).createRadioGroup();
183184
radioGroup.disableFieldRegeneration();
184185
radioGroup.setValue(PdfFormAnnotation.OFF_STATE_VALUE);
185-
addNew = true;
186186
} else {
187187
radioGroup.disableFieldRegeneration();
188188
}
@@ -204,13 +204,10 @@ protected void applyAcroField(DrawContext drawContext) {
204204
radioGroup.addKid(radio);
205205
radioGroup.enableFieldRegeneration();
206206

207-
if (addNew) {
208-
form.addField(radioGroup, page);
209-
} else {
210-
form.replaceField(groupName, radioGroup);
211-
}
207+
form.addField(radioGroup, page);
212208

213209
writeAcroFormFieldLangAttribute(doc);
210+
applyProperties(margins);
214211
}
215212

216213
/**

forms/src/main/java/com/itextpdf/forms/form/renderer/TextAreaRenderer.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,36 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.forms.form.renderer;
2424

25+
import com.itextpdf.commons.utils.MessageFormatUtil;
2526
import com.itextpdf.forms.fields.AbstractPdfFormField;
2627
import com.itextpdf.forms.fields.PdfFormCreator;
27-
import com.itextpdf.forms.logs.FormsLogMessageConstants;
28-
import com.itextpdf.forms.PdfAcroForm;
2928
import com.itextpdf.forms.fields.PdfFormField;
3029
import com.itextpdf.forms.fields.TextFormFieldBuilder;
3130
import com.itextpdf.forms.form.FormProperty;
3231
import com.itextpdf.forms.form.element.TextArea;
33-
import com.itextpdf.commons.utils.MessageFormatUtil;
32+
import com.itextpdf.forms.logs.FormsLogMessageConstants;
3433
import com.itextpdf.io.logs.IoLogMessageConstant;
3534
import com.itextpdf.kernel.geom.Rectangle;
3635
import com.itextpdf.kernel.pdf.PdfDocument;
3736
import com.itextpdf.kernel.pdf.PdfPage;
3837
import com.itextpdf.kernel.pdf.PdfString;
39-
import com.itextpdf.layout.element.Paragraph;
40-
import com.itextpdf.layout.element.Text;
4138
import com.itextpdf.layout.layout.LayoutArea;
4239
import com.itextpdf.layout.layout.LayoutContext;
4340
import com.itextpdf.layout.layout.LayoutResult;
4441
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
45-
import com.itextpdf.layout.properties.OverflowPropertyValue;
4642
import com.itextpdf.layout.properties.BoxSizingPropertyValue;
43+
import com.itextpdf.layout.properties.OverflowPropertyValue;
4744
import com.itextpdf.layout.properties.Property;
4845
import com.itextpdf.layout.properties.UnitValue;
4946
import com.itextpdf.layout.renderer.DrawContext;
5047
import com.itextpdf.layout.renderer.IRenderer;
5148
import com.itextpdf.layout.renderer.LineRenderer;
5249
import com.itextpdf.layout.renderer.ParagraphRenderer;
5350

51+
import java.util.List;
52+
import java.util.Map;
5453
import org.slf4j.Logger;
5554
import org.slf4j.LoggerFactory;
56-
import java.util.List;
5755

5856
/**
5957
* The {@link AbstractTextFieldRenderer} implementation for text area fields.
@@ -196,7 +194,7 @@ protected void applyAcroField(DrawContext drawContext) {
196194
PdfDocument doc = drawContext.getDocument();
197195
Rectangle area = getOccupiedArea().getBBox().clone();
198196
applyMargins(area, false);
199-
deleteMargins();
197+
final Map<Integer, Object> margins = deleteMargins();
200198
PdfPage page = doc.getPage(occupiedArea.getPageNumber());
201199
final float fontSizeValue = fontSize.getValue();
202200
final PdfString defaultValue = new PdfString(getDefaultValue());
@@ -216,6 +214,7 @@ protected void applyAcroField(DrawContext drawContext) {
216214
PdfFormCreator.getAcroForm(doc, true).addField(inputField, page);
217215

218216
writeAcroFormFieldLangAttribute(doc);
217+
applyProperties(margins);
219218
}
220219

221220
/**

0 commit comments

Comments
 (0)