Skip to content

Commit ffaf64c

Browse files
committed
Always draw single line text in single line text form fields
DEVSIX-3012
1 parent 596a2c8 commit ffaf64c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ public class PdfFormAnnotation extends AbstractPdfFormField {
116116
*/
117117
static final float X_OFFSET = 2;
118118

119-
private static final Logger LOGGER = LoggerFactory.getLogger(PdfFormAnnotation.class);
120-
121119
protected float borderWidth = 1;
122120
protected Color backgroundColor;
123121
protected Color borderColor;
124122

123+
private static final Logger LOGGER = LoggerFactory.getLogger(PdfFormAnnotation.class);
124+
125+
private static final String LINE_ENDINGS_REGEXP = "\\r\\n|\\r|\\n";
126+
125127
private Button formFieldElement;
126128

127129
/**
@@ -910,15 +912,17 @@ protected void drawTextFormFieldAndSaveAppearance() {
910912
}
911913

912914
IFormField textFormField;
915+
String value = parent.getDisplayValue();
913916
if (parent.isMultiline()) {
914917
textFormField = new TextArea("");
915918
textFormField.setProperty(Property.FONT_SIZE, UnitValue.createPointValue(getFontSize()));
916919
} else {
917920
textFormField = new InputField("");
918921
textFormField.setProperty(Property.FONT_SIZE,
919922
UnitValue.createPointValue(getFontSize(new PdfArray(rectangle), parent.getValueAsString())));
923+
value = value.replaceAll(LINE_ENDINGS_REGEXP, " ");
920924
}
921-
textFormField.setProperty(FormProperty.FORM_FIELD_VALUE, parent.getDisplayValue());
925+
textFormField.setProperty(FormProperty.FORM_FIELD_VALUE, value);
922926
textFormField.setProperty(Property.FONT, getFont());
923927
textFormField.setProperty(Property.TEXT_ALIGNMENT, parent.getJustification());
924928
textFormField.setProperty(FormProperty.FORM_FIELD_PASSWORD_FLAG, getParentField().isPassword());

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTextTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.forms;
2424

2525
import com.itextpdf.forms.fields.PdfFormField;
26+
import com.itextpdf.forms.fields.PdfTextFormField;
27+
import com.itextpdf.forms.fields.TextFormFieldBuilder;
2628
import com.itextpdf.io.font.PdfEncodings;
2729
import com.itextpdf.kernel.font.PdfFont;
2830
import com.itextpdf.kernel.font.PdfFontFactory;
31+
import com.itextpdf.kernel.geom.Rectangle;
2932
import com.itextpdf.kernel.pdf.PdfDictionary;
3033
import com.itextpdf.kernel.pdf.PdfDocument;
3134
import com.itextpdf.kernel.pdf.PdfName;
@@ -162,4 +165,26 @@ public void fontsResourcesHelvCourierNotoFontTest() throws IOException {
162165

163166
ExtendedITextTest.printOutputPdfNameAndDir(destinationFolder + filename);
164167
}
168+
169+
@Test
170+
public void lineEndingsTest() throws IOException, InterruptedException {
171+
String destFilename = destinationFolder + "lineEndingsTest.pdf";
172+
String cmpFilename = sourceFolder + "cmp_lineEndingsTest.pdf";
173+
174+
try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destFilename))) {
175+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
176+
177+
PdfTextFormField field = new TextFormFieldBuilder(pdfDoc, "single")
178+
.setWidgetRectangle(new Rectangle(50, 700, 500, 120)).createText();
179+
field.setValue("Line 1\nLine 2\rLine 3\r\nLine 4");
180+
form.addField(field);
181+
182+
PdfTextFormField field2 = new TextFormFieldBuilder(pdfDoc, "multi")
183+
.setWidgetRectangle(new Rectangle(50, 500, 500, 120)).createMultilineText();
184+
field2.setValue("Line 1\nLine 2\rLine 3\r\nLine 4");
185+
form.addField(field2);
186+
}
187+
188+
Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFilename, destinationFolder, "diff_"));
189+
}
165190
}

0 commit comments

Comments
 (0)