Skip to content

Commit 6e8b824

Browse files
Avoid failing with exception in case form field /DA key is malformed
Also avoid using DR font name if custom font was explicitly set; if text field is created without specifying font and font-size, rely on inherited DA first. ITXT-CR-669
1 parent 6a3d902 commit 6e8b824

File tree

6 files changed

+91
-55
lines changed

6 files changed

+91
-55
lines changed

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

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,7 @@ public static PdfTextFormField createText(PdfDocument doc, Rectangle rect, Strin
411411
* @return a new {@link PdfTextFormField}
412412
*/
413413
public static PdfTextFormField createText(PdfDocument doc, Rectangle rect, String name, String value) {
414-
try {
415-
return createText(doc, rect, name, value, PdfFontFactory.createFont(), (float) DEFAULT_FONT_SIZE);
416-
} catch (IOException e) {
417-
throw new PdfException(e);
418-
}
414+
return createText(doc, rect, name, value, null, -1);
419415
}
420416

421417
/**
@@ -511,11 +507,7 @@ public static PdfTextFormField createMultilineText(PdfDocument doc, Rectangle re
511507
* @return a new {@link PdfTextFormField}
512508
*/
513509
public static PdfTextFormField createMultilineText(PdfDocument doc, Rectangle rect, String name, String value) {
514-
try {
515-
return createText(doc, rect, name, value, PdfFontFactory.createFont(), (float) DEFAULT_FONT_SIZE, true);
516-
} catch (IOException e) {
517-
throw new PdfException(e);
518-
}
510+
return createText(doc, rect, name, value, null, -1, true);
519511
}
520512

521513
/**
@@ -2657,58 +2649,50 @@ protected Object[] getFontAndSize(PdfDictionary asNormal) throws IOException {
26572649
if (asNormal != null) {
26582650
normalResources = asNormal.getAsDictionary(PdfName.Resources);
26592651
}
2652+
2653+
PdfDictionary daFontDict = null;
2654+
PdfName daFontName = null;
2655+
Object[] dab = new Object[3];
26602656
if (defaultResources != null || normalResources != null) {
26612657
PdfDictionary normalFontDic = normalResources != null ? normalResources.getAsDictionary(PdfName.Font) : null;
26622658
PdfDictionary defaultFontDic = defaultResources != null ? defaultResources.getAsDictionary(PdfName.Font) : null;
26632659
PdfString defaultAppearance = getDefaultAppearance();
2664-
26652660
if ((normalFontDic != null || defaultFontDic != null) && defaultAppearance != null) {
2666-
Object[] dab = splitDAelements(defaultAppearance.toUnicodeString());
2667-
PdfName fontName = new PdfName(dab[DA_FONT].toString());
2668-
fontAndSize[2] = fontName;
2669-
PdfDictionary requiredFontDictionary = null;
2670-
if (normalFontDic != null && null != normalFontDic.getAsDictionary(fontName)) {
2671-
requiredFontDictionary = normalFontDic.getAsDictionary(fontName);
2672-
} else if (defaultFontDic != null) {
2673-
requiredFontDictionary = defaultFontDic.getAsDictionary(fontName);
2674-
}
2675-
if (font != null) {
2676-
fontAndSize[0] = font;
2677-
} else {
2678-
PdfFont dicFont = document != null ? document.getFont(requiredFontDictionary) : PdfFontFactory.createFont(requiredFontDictionary);
2679-
fontAndSize[0] = dicFont;
2680-
}
2681-
if (fontSize >= 0) {
2682-
fontAndSize[1] = fontSize;
2683-
} else {
2684-
fontAndSize[1] = dab[DA_SIZE];
2685-
}
2686-
if (color == null) {
2687-
color = (Color) dab[DA_COLOR];
2688-
}
2689-
} else {
2690-
if (font != null) {
2691-
fontAndSize[0] = font;
2692-
} else {
2693-
fontAndSize[0] = PdfFontFactory.createFont();
2694-
}
2695-
if (fontSize >= 0) {
2696-
fontAndSize[1] = fontSize;
2697-
} else {
2698-
fontAndSize[1] = (float) DEFAULT_FONT_SIZE;
2661+
dab = splitDAelements(defaultAppearance.toUnicodeString());
2662+
Object fontNameObj = dab[DA_FONT];
2663+
if (fontNameObj != null) {
2664+
daFontName = new PdfName(fontNameObj.toString());
2665+
// according to spec, DA font shall be taken from the DR
2666+
if (defaultFontDic != null && null != defaultFontDic.getAsDictionary(daFontName)) {
2667+
daFontDict = defaultFontDic.getAsDictionary(daFontName);
2668+
} else if (normalFontDic != null) {
2669+
// search normal appearance as a fall back in case it was not found in DR
2670+
daFontDict = normalFontDic.getAsDictionary(daFontName);
2671+
}
26992672
}
27002673
}
2674+
}
2675+
2676+
if (font != null) {
2677+
fontAndSize[0] = font;
2678+
} else if (daFontDict != null) {
2679+
PdfFont daFont = document != null ? document.getFont(daFontDict) : PdfFontFactory.createFont(daFontDict);
2680+
fontAndSize[0] = daFont;
2681+
fontAndSize[2] = daFontName;
27012682
} else {
2702-
if (font != null) {
2703-
fontAndSize[0] = font;
2704-
} else {
2705-
fontAndSize[0] = PdfFontFactory.createFont();
2706-
}
2707-
if (fontSize >= 0) {
2708-
fontAndSize[1] = fontSize;
2709-
} else {
2710-
fontAndSize[1] = (float) DEFAULT_FONT_SIZE;
2711-
}
2683+
fontAndSize[0] = PdfFontFactory.createFont();
2684+
}
2685+
2686+
if (fontSize >= 0) {
2687+
fontAndSize[1] = fontSize;
2688+
} else if (dab[DA_SIZE] != null) {
2689+
fontAndSize[1] = dab[DA_SIZE];
2690+
} else {
2691+
fontAndSize[1] = (float) DEFAULT_FONT_SIZE;
2692+
}
2693+
2694+
if (color == null) {
2695+
color = (Color) dab[DA_COLOR];
27122696
}
27132697

27142698
return fontAndSize;
@@ -2757,7 +2741,7 @@ protected static Object[] splitDAelements(String da) {
27572741
stack.add(tk.getStringValue());
27582742
}
27592743
}
2760-
} catch (IOException e) {
2744+
} catch (Exception ignored) {
27612745

27622746
}
27632747
return ret;

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.kernel.font.PdfFontFactory;
5353
import com.itextpdf.kernel.geom.Rectangle;
5454
import com.itextpdf.kernel.pdf.PdfDocument;
55+
import com.itextpdf.kernel.pdf.PdfName;
5556
import com.itextpdf.kernel.pdf.PdfPage;
5657
import com.itextpdf.kernel.pdf.PdfReader;
58+
import com.itextpdf.kernel.pdf.PdfString;
5759
import com.itextpdf.kernel.pdf.PdfWriter;
5860
import com.itextpdf.kernel.pdf.StampingProperties;
5961
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
@@ -139,6 +141,29 @@ public void formFieldTest03() throws IOException, InterruptedException {
139141
}
140142
}
141143

144+
@Test
145+
public void formFieldTest04() throws IOException, InterruptedException {
146+
String filename = destinationFolder + "formFieldTest04.pdf";
147+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "formFieldFile.pdf"), new PdfWriter(filename));
148+
149+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
150+
151+
PdfPage page = pdfDoc.getFirstPage();
152+
Rectangle rect = new Rectangle(210, 490, 150, 22);
153+
154+
PdfTextFormField field = PdfFormField.createText(pdfDoc, rect, "TestField", "some value in courier font", PdfFontFactory.createFont(StandardFonts.COURIER), 10);
155+
156+
form.addField(field, page);
157+
158+
pdfDoc.close();
159+
160+
CompareTool compareTool = new CompareTool();
161+
String errorMessage = compareTool.compareByContent(filename, sourceFolder + "cmp_formFieldTest04.pdf", destinationFolder, "diff_");
162+
if (errorMessage != null) {
163+
Assert.fail(errorMessage);
164+
}
165+
}
166+
142167
@Test
143168
public void unicodeFormFieldTest() throws IOException {
144169
String filename = sourceFolder + "unicodeFormFieldFile.pdf";
@@ -481,4 +506,31 @@ public void fillFormWithDefaultResourcesUpdateFont() throws IOException, Interru
481506
Assert.fail(errorMessage);
482507
}
483508
}
509+
510+
@Test
511+
public void formRegenerateWithInvalidDefaultAppearance01() throws IOException, InterruptedException {
512+
String testName = "formRegenerateWithInvalidDefaultAppearance01";
513+
String outPdf = destinationFolder + testName + ".pdf";
514+
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
515+
String srcPdf = sourceFolder + "invalidDA.pdf";
516+
517+
PdfWriter writer = new PdfWriter(outPdf);
518+
PdfReader reader = new PdfReader(srcPdf);
519+
PdfDocument pdfDoc = new PdfDocument(reader, writer);
520+
521+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
522+
523+
Map<String, PdfFormField> fields = form.getFormFields();
524+
fields.get("Text1").setValue("New field value");
525+
fields.get("Text2").setValue("New field value");
526+
fields.get("Text3").setValue("New field value");
527+
528+
pdfDoc.close();
529+
530+
CompareTool compareTool = new CompareTool();
531+
String errorMessage = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
532+
if (errorMessage != null) {
533+
Assert.fail(errorMessage);
534+
}
535+
}
484536
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)