Skip to content

Commit 239e843

Browse files
committed
Get rid of BORDER property in favour of BORDER_BOTTOM, BORDER_RIGHT, etc.
DEVSIX-8560
1 parent 7cafdd0 commit 239e843

39 files changed

+298
-272
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,13 @@ public float getBorderWidth() {
422422
* @return The edited {@link PdfFormAnnotation}.
423423
*/
424424
public PdfFormAnnotation setBorderWidth(float borderWidth) {
425-
// Acrobat doesn't support float border width therefore we round it.
426-
int roundedBorderWidth = (int) Math.round(borderWidth);
427425
PdfDictionary bs = getWidget().getBorderStyle();
428426
if (bs == null) {
429427
bs = new PdfDictionary();
430428
put(PdfName.BS, bs);
431429
}
432-
bs.put(PdfName.W, new PdfNumber(roundedBorderWidth));
433-
this.borderWidth = roundedBorderWidth;
430+
bs.put(PdfName.W, new PdfNumber(borderWidth));
431+
this.borderWidth = borderWidth;
434432

435433
regenerateField();
436434
return this;
@@ -446,7 +444,7 @@ public Border getBorder() {
446444
Border border = FormBorderFactory.getBorder(
447445
this.getWidget().getBorderStyle(), borderWidth, borderColor, backgroundColor);
448446
if (border == null && borderWidth > 0 && borderColor != null) {
449-
border = new SolidBorder(borderColor, Math.max(1, borderWidth));
447+
border = new SolidBorder(borderColor, borderWidth);
450448
}
451449
return border;
452450
}
@@ -514,9 +512,9 @@ public PdfFormAnnotation setPage(int pageNum) {
514512
* <p>
515513
* Also note that the model element won't be used for annotations for choice form field.
516514
*
517-
* @param element model element to set.
515+
* @param element model element to set
518516
*
519-
* @return this {@link PdfFormAnnotation}.
517+
* @return this {@link PdfFormAnnotation}
520518
*/
521519
public PdfFormAnnotation setFormFieldElement(IFormField element) {
522520
this.formFieldElement = element;
@@ -1235,7 +1233,10 @@ private void setModelElementProperties(Rectangle rectangle) {
12351233
if (backgroundColor != null) {
12361234
formFieldElement.setProperty(Property.BACKGROUND, new Background(backgroundColor));
12371235
}
1238-
formFieldElement.setProperty(Property.BORDER, getBorder());
1236+
formFieldElement.setProperty(Property.BORDER_TOP, getBorder());
1237+
formFieldElement.setProperty(Property.BORDER_RIGHT, getBorder());
1238+
formFieldElement.setProperty(Property.BORDER_BOTTOM, getBorder());
1239+
formFieldElement.setProperty(Property.BORDER_LEFT, getBorder());
12391240
// Set fixed size
12401241
BoxSizingPropertyValue boxSizing = formFieldElement.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING);
12411242
// Borders and paddings are already taken into account for rectangle area, but shouldn't be included into width

forms/src/main/java/com/itextpdf/forms/form/element/IFormField.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,44 @@ public interface IFormField extends IBlockElement {
3232
/**
3333
* Sets the {@link FormProperty#FORM_FIELD_VALUE} property.
3434
*
35-
* @param value string value of the property to be set.
35+
* @param value string value of the property to be set
3636
*
37-
* @return this same {@link IFormField} instance.
37+
* @return this same {@link IFormField} instance
3838
*/
3939
IFormField setValue(String value);
4040

4141
/**
4242
* Set the form field to be interactive and added into Acroform instead of drawing it on a page.
4343
*
4444
* @param interactive {@code true} if the form field element shall be added into Acroform, {@code false} otherwise.
45-
* By default, the form field element is not interactive and drawn on a page.
45+
* By default, the form field element is not interactive and drawn on a page
4646
*
47-
* @return this same {@link IFormField} instance.
47+
* @return this same {@link IFormField} instance
4848
*/
4949
IFormField setInteractive(boolean interactive);
5050

5151
/**
5252
* Gets the id.
5353
*
54-
* @return the id.
54+
* @return the id
5555
*/
5656
String getId();
5757

5858
/**
5959
* Set the form field's width.
6060
*
61-
* @param width form field's width.
61+
* @param width form field's width
6262
*
63-
* @return this {@link FormField} element.
63+
* @return this {@link FormField} element
6464
*/
6565
IFormField setWidth(float width);
6666

6767
/**
6868
* Set the form field's height.
6969
*
70-
* @param height form field's height.
70+
* @param height form field's height
7171
*
72-
* @return this {@link FormField} element.
72+
* @return this {@link FormField} element
7373
*/
7474
IFormField setHeight(float height);
7575
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ public IRenderer createFlatRenderer() {
226226

227227
paragraph.setProperty(Property.BOX_SIZING, this.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING));
228228
modelElement.setProperty(Property.RENDERING_MODE, this.<RenderingMode>getProperty(Property.RENDERING_MODE));
229-
paragraph.setBorder(this.<Border>getProperty(Property.BORDER));
229+
paragraph.setBorderTop(this.<Border>getProperty(Property.BORDER_TOP));
230+
paragraph.setBorderRight(this.<Border>getProperty(Property.BORDER_RIGHT));
231+
paragraph.setBorderBottom(this.<Border>getProperty(Property.BORDER_BOTTOM));
232+
paragraph.setBorderLeft(this.<Border>getProperty(Property.BORDER_LEFT));
230233
paragraph.setProperty(Property.BACKGROUND, this.<Background>getProperty(Property.BACKGROUND));
231234

232235
//In html 2 pdf rendering the boxes height width ratio is always 1:1

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ protected IRenderer createFlatRenderer() {
142142
.setVerticalAlignment(DEFAULT_VERTICAL_ALIGNMENT)
143143
.setMargin(0);
144144
paragraph.setProperty(Property.BOX_SIZING, this.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING));
145-
paragraph.setBorder(this.<Border>getProperty(Property.BORDER));
145+
paragraph.setBorderTop(this.<Border>getProperty(Property.BORDER_TOP));
146+
paragraph.setBorderRight(this.<Border>getProperty(Property.BORDER_RIGHT));
147+
paragraph.setBorderBottom(this.<Border>getProperty(Property.BORDER_BOTTOM));
148+
paragraph.setBorderLeft(this.<Border>getProperty(Property.BORDER_LEFT));
146149
paragraph.setProperty(Property.BACKGROUND, this.<Background>getProperty(Property.BACKGROUND));
147150
paragraph.setBorderRadius(new BorderRadius(UnitValue.createPercentValue(50)));
148151

@@ -248,11 +251,14 @@ public void drawChildren(DrawContext drawContext) {
248251
canvas.openTag(tp.getTagReference());
249252
}
250253
Rectangle rectangle = getOccupiedArea().getBBox().clone();
251-
Border border = this.<Border>getProperty(Property.BORDER);
252-
if (border != null) {
253-
rectangle.applyMargins(border.getWidth(), border.getWidth(), border.getWidth(), border.getWidth(),
254-
false);
255-
}
254+
Border borderTop = this.<Border>getProperty(Property.BORDER_TOP);
255+
Border borderRight = this.<Border>getProperty(Property.BORDER_RIGHT);
256+
Border borderBottom = this.<Border>getProperty(Property.BORDER_BOTTOM);
257+
Border borderLeft = this.<Border>getProperty(Property.BORDER_LEFT);
258+
rectangle.applyMargins(borderTop == null ? 0 : borderTop.getWidth(),
259+
borderRight == null ? 0 : borderRight.getWidth(),
260+
borderBottom == null ? 0 : borderBottom.getWidth(),
261+
borderLeft == null ? 0 : borderLeft.getWidth(), false);
256262
final float radius = Math.min(rectangle.getWidth(), rectangle.getHeight()) / 2;
257263
canvas.saveState();
258264
canvas.setFillColor(DEFAULT_CHECKED_COLOR);

forms/src/main/java/com/itextpdf/forms/form/renderer/checkboximpl/PdfCheckBoxRenderingStrategy.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,57 @@ public void drawCheckBoxContent(DrawContext drawContext, CheckBoxRenderer checkB
7171
if (!checkBoxRenderer.isBoxChecked()) {
7272
return;
7373
}
74-
float borderWidth = CheckBoxRenderer.DEFAULT_BORDER_WIDTH;
75-
final Border border = checkBoxRenderer.<Border>getProperty(Property.BORDER);
76-
if (border != null) {
77-
borderWidth = border.getWidth();
78-
rectangle.applyMargins(borderWidth, borderWidth, borderWidth, borderWidth, true);
79-
}
74+
Border borderTop = checkBoxRenderer.<Border>getProperty(Property.BORDER_TOP);
75+
Border borderRight = checkBoxRenderer.<Border>getProperty(Property.BORDER_RIGHT);
76+
Border borderBottom = checkBoxRenderer.<Border>getProperty(Property.BORDER_BOTTOM);
77+
Border borderLeft = checkBoxRenderer.<Border>getProperty(Property.BORDER_LEFT);
78+
rectangle.applyMargins(borderTop == null ? 0 : borderTop.getWidth(),
79+
borderRight == null ? 0 : borderRight.getWidth(),
80+
borderBottom == null ? 0 : borderBottom.getWidth(),
81+
borderLeft == null ? 0 : borderLeft.getWidth(), true);
8082
final PdfCanvas canvas = drawContext.getCanvas();
8183
canvas.saveState();
8284
canvas.setFillColor(ColorConstants.BLACK);
8385
// matrix transformation to draw the checkbox in the right place
8486
// because we come here with relative and not absolute coordinates
8587
canvas.concatMatrix(1, 0, 0, 1, rectangle.getLeft(), rectangle.getBottom());
86-
8788
final CheckBoxType checkBoxType = checkBoxRenderer.getCheckBoxType();
8889
if (checkBoxType == CheckBoxType.CROSS || checkBoxType == null) {
89-
final float customBorderWidth = border == null ? 1 : borderWidth;
90+
final float customBorderWidth = retrieveBorderWidth(1, borderTop, borderRight, borderBottom, borderLeft);
9091
DrawingUtil.drawCross(canvas, rectangle.getWidth(), rectangle.getHeight(), customBorderWidth);
9192
} else {
9293
final String text = ZAPFDINGBATS_CHECKBOX_MAPPING.getByKey(checkBoxType);
9394
final PdfFont fontContainingSymbols = loadFontContainingSymbols();
94-
float fontSize = calculateFontSize(checkBoxRenderer, fontContainingSymbols, text, rectangle, borderWidth);
95+
float fontSize = calculateFontSize(checkBoxRenderer, fontContainingSymbols, text, rectangle,
96+
retrieveBorderWidth(CheckBoxRenderer.DEFAULT_BORDER_WIDTH,
97+
borderTop, borderRight, borderBottom, borderLeft));
9598
drawZapfdingbatsIcon(fontContainingSymbols, text, fontSize, rectangle, canvas);
9699
}
97100

98101
canvas.restoreState();
99102
}
100103

104+
private static float retrieveBorderWidth(float defaultWidth, Border borderTop, Border borderRight, Border borderBottom,
105+
Border borderLeft) {
106+
if (borderTop == null && borderRight == null && borderBottom == null && borderLeft == null) {
107+
return defaultWidth;
108+
}
109+
float borderWidth = 0;
110+
if (borderTop != null) {
111+
borderWidth = Math.max(borderWidth, borderTop.getWidth());
112+
}
113+
if (borderRight != null) {
114+
borderWidth = Math.max(borderWidth, borderRight.getWidth());
115+
}
116+
if (borderBottom != null) {
117+
borderWidth = Math.max(borderWidth, borderBottom.getWidth());
118+
}
119+
if (borderLeft != null) {
120+
borderWidth = Math.max(borderWidth, borderLeft.getWidth());
121+
}
122+
return borderWidth;
123+
}
124+
101125
private PdfFont loadFontContainingSymbols() {
102126
try {
103127
return PdfFontFactory.createFont(StandardFonts.ZAPFDINGBATS);

forms/src/main/java/com/itextpdf/forms/util/BorderStyleUtil.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ public final class BorderStyleUtil {
4343
* @param annotation the annotation to set border characteristics to.
4444
*/
4545
public static void applyBorderProperty(IPropertyContainer container, PdfFormAnnotation annotation) {
46-
Border border = container.<Border>getProperty(Property.BORDER);
47-
if (border == null) {
48-
// For now, we set left border to an annotation, but appropriate borders for an element will be drawn.
49-
border = container.<Border>getProperty(Property.BORDER_LEFT);
50-
}
46+
// For now, we set left border to an annotation, but appropriate borders for an element will be drawn.
47+
Border border = container.<Border>getProperty(Property.BORDER_LEFT);
5148
if (border != null) {
5249
annotation.setBorderStyle(transformBorderTypeToBorderStyleDictionary(border.getType()));
5350
annotation.setBorderColor(border.getColor());

forms/src/test/java/com/itextpdf/forms/form/element/CheckBoxTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,39 @@ public void basicCheckBoxTagged() throws IOException, InterruptedException {
582582
Assertions.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER));
583583
}
584584

585+
@Test
586+
public void setBordersTest() throws IOException, InterruptedException {
587+
String outPdf = DESTINATION_FOLDER + "checkBoxSetBorders.pdf";
588+
String cmpPdf = SOURCE_FOLDER + "cmp_checkBoxSetBorders.pdf";
589+
try (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) {
590+
final Float[] i = {.1f, .2f, .3f, .4f};
591+
document.add(new Paragraph("Test different borders"));
592+
for (int j = 0; j < 30; j++) {
593+
i[0] += .03f;
594+
i[1] += .05f;
595+
i[2] += .07f;
596+
i[3] += .09f;
597+
CheckBox checkBox = new CheckBox("test" + j);
598+
checkBox.setChecked(true);
599+
checkBox.setSize(20);
600+
if (j % 2 == 0) {
601+
checkBox.setBorderRight(new SolidBorder(ColorConstants.GREEN, (float) i[0]));
602+
}
603+
if (j % 3 == 0) {
604+
checkBox.setBorderLeft(new SolidBorder(ColorConstants.PINK, (float) i[1]));
605+
}
606+
if (j % 5 == 0 || j == 1 || j == 13 || j == 19 || j == 29) {
607+
checkBox.setBorderTop(new SolidBorder(ColorConstants.CYAN, (float) i[2]));
608+
}
609+
if (j % 7 == 0 || j == 11 || j == 17 || j == 23) {
610+
checkBox.setBorderBottom(new SolidBorder(ColorConstants.YELLOW, (float) i[3]));
611+
}
612+
document.add(checkBox);
613+
}
614+
}
615+
Assertions.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER));
616+
}
617+
585618
private List<CheckBox> generateCheckBoxes(Document document, Consumer<CheckBox> alterFunction) {
586619
List<CheckBox> checkBoxList = new ArrayList<>();
587620

forms/src/test/java/com/itextpdf/forms/form/element/InputFieldTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void noValueInputFieldTest() throws IOException, InterruptedException {
9393
InputField flattenInputField = new InputField("no value input field");
9494
flattenInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, null);
9595
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, null);
96-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
96+
flattenInputField.setBorder(new SolidBorder(2f));
9797
document.add(flattenInputField);
9898
}
9999

@@ -115,7 +115,7 @@ public void inputFieldDoesNotFitTest() throws IOException, InterruptedException
115115
InputField flattenInputField = new InputField("input field does not fit");
116116
flattenInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, Boolean.TRUE);
117117
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "input field does not fit");
118-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
118+
flattenInputField.setBorder(new SolidBorder(2f));
119119
document.add(flattenInputField);
120120
}
121121

@@ -134,7 +134,7 @@ public void inputFieldWithLangTest() throws IOException, InterruptedException {
134134
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "input field with lang");
135135

136136
flattenInputField.getAccessibilityProperties().setLanguage("random_lang");
137-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
137+
flattenInputField.setBorder(new SolidBorder(2f));
138138
document.add(flattenInputField);
139139
}
140140

@@ -153,7 +153,7 @@ public void inputFieldWithNullLangTest() throws IOException, InterruptedExceptio
153153
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "input field with null lang");
154154

155155
flattenInputField.getAccessibilityProperties().setLanguage(null);
156-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
156+
flattenInputField.setBorder(new SolidBorder(2f));
157157
document.add(flattenInputField);
158158
}
159159

@@ -169,14 +169,14 @@ public void inputFieldWithPasswordTest() throws IOException, InterruptedExceptio
169169
InputField formInputField = new InputField("form input field with password");
170170
formInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, Boolean.FALSE);
171171
formInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "form input field with password");
172-
formInputField.setProperty(Property.BORDER, new SolidBorder(2f));
172+
formInputField.setBorder(new SolidBorder(2f));
173173
formInputField.setProperty(FormProperty.FORM_FIELD_PASSWORD_FLAG, Boolean.TRUE);
174174
document.add(formInputField);
175175

176176
InputField flattenInputField = new InputField("flatten input field with password");
177177
flattenInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, Boolean.TRUE);
178178
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "flatten input field with password");
179-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
179+
flattenInputField.setBorder(new SolidBorder(2f));
180180
flattenInputField.setProperty(FormProperty.FORM_FIELD_PASSWORD_FLAG, Boolean.TRUE);
181181
document.add(flattenInputField);
182182
}
@@ -194,7 +194,7 @@ public void heightInputFieldTest() throws IOException, InterruptedException {
194194
flattenInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, Boolean.TRUE);
195195
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "flatten input field with height");
196196
flattenInputField.setProperty(Property.HEIGHT, new UnitValue(UnitValue.POINT, 100));
197-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
197+
flattenInputField.setBorder(new SolidBorder(2f));
198198
document.add(flattenInputField);
199199
}
200200

@@ -211,7 +211,7 @@ public void minHeightInputFieldTest() throws IOException, InterruptedException {
211211
flattenInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, Boolean.TRUE);
212212
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "flatten input field with height");
213213
flattenInputField.setProperty(Property.MIN_HEIGHT, new UnitValue(UnitValue.POINT, 100));
214-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
214+
flattenInputField.setBorder(new SolidBorder(2f));
215215
document.add(flattenInputField);
216216
}
217217

@@ -228,7 +228,7 @@ public void maxHeightInputFieldTest() throws IOException, InterruptedException {
228228
flattenInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, Boolean.TRUE);
229229
flattenInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "flatten input field with height");
230230
flattenInputField.setProperty(Property.MAX_HEIGHT, new UnitValue(UnitValue.POINT, 10));
231-
flattenInputField.setProperty(Property.BORDER, new SolidBorder(2f));
231+
flattenInputField.setBorder(new SolidBorder(2f));
232232
document.add(flattenInputField);
233233
}
234234

0 commit comments

Comments
 (0)