Skip to content

Commit c0e2eff

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 Autoported commit. Original commit hash: [6e8b824838]
1 parent ade8aec commit c0e2eff

File tree

7 files changed

+83
-59
lines changed

7 files changed

+83
-59
lines changed

itext.tests/itext.forms.tests/itext/forms/PdfFormFieldTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ public virtual void FormFieldTest03() {
120120
}
121121
}
122122

123+
/// <exception cref="System.IO.IOException"/>
124+
/// <exception cref="System.Exception"/>
125+
[NUnit.Framework.Test]
126+
public virtual void FormFieldTest04() {
127+
String filename = destinationFolder + "formFieldTest04.pdf";
128+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "formFieldFile.pdf"), new PdfWriter(filename
129+
));
130+
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
131+
PdfPage page = pdfDoc.GetFirstPage();
132+
Rectangle rect = new Rectangle(210, 490, 150, 22);
133+
PdfTextFormField field = PdfFormField.CreateText(pdfDoc, rect, "TestField", "some value in courier font",
134+
PdfFontFactory.CreateFont(StandardFonts.COURIER), 10);
135+
form.AddField(field, page);
136+
pdfDoc.Close();
137+
CompareTool compareTool = new CompareTool();
138+
String errorMessage = compareTool.CompareByContent(filename, sourceFolder + "cmp_formFieldTest04.pdf", destinationFolder
139+
, "diff_");
140+
if (errorMessage != null) {
141+
NUnit.Framework.Assert.Fail(errorMessage);
142+
}
143+
}
144+
123145
/// <exception cref="System.IO.IOException"/>
124146
[NUnit.Framework.Test]
125147
public virtual void UnicodeFormFieldTest() {
@@ -432,5 +454,29 @@ public virtual void FillFormWithDefaultResourcesUpdateFont() {
432454
NUnit.Framework.Assert.Fail(errorMessage);
433455
}
434456
}
457+
458+
/// <exception cref="System.IO.IOException"/>
459+
/// <exception cref="System.Exception"/>
460+
[NUnit.Framework.Test]
461+
public virtual void FormRegenerateWithInvalidDefaultAppearance01() {
462+
String testName = "formRegenerateWithInvalidDefaultAppearance01";
463+
String outPdf = destinationFolder + testName + ".pdf";
464+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
465+
String srcPdf = sourceFolder + "invalidDA.pdf";
466+
PdfWriter writer = new PdfWriter(outPdf);
467+
PdfReader reader = new PdfReader(srcPdf);
468+
PdfDocument pdfDoc = new PdfDocument(reader, writer);
469+
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
470+
IDictionary<String, PdfFormField> fields = form.GetFormFields();
471+
fields.Get("Text1").SetValue("New field value");
472+
fields.Get("Text2").SetValue("New field value");
473+
fields.Get("Text3").SetValue("New field value");
474+
pdfDoc.Close();
475+
CompareTool compareTool = new CompareTool();
476+
String errorMessage = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
477+
if (errorMessage != null) {
478+
NUnit.Framework.Assert.Fail(errorMessage);
479+
}
480+
}
435481
}
436482
}

itext/itext.forms/itext/forms/fields/PdfFormField.cs

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,7 @@ public static PdfTextFormField CreateText(PdfDocument doc, Rectangle rect, Strin
496496
/// <see cref="PdfTextFormField"/>
497497
/// </returns>
498498
public static PdfTextFormField CreateText(PdfDocument doc, Rectangle rect, String name, String value) {
499-
try {
500-
return CreateText(doc, rect, name, value, PdfFontFactory.CreateFont(), (float)DEFAULT_FONT_SIZE);
501-
}
502-
catch (System.IO.IOException e) {
503-
throw new PdfException(e);
504-
}
499+
return CreateText(doc, rect, name, value, null, -1);
505500
}
506501

507502
/// <summary>
@@ -658,12 +653,7 @@ public static PdfTextFormField CreateMultilineText(PdfDocument doc, Rectangle re
658653
/// </returns>
659654
public static PdfTextFormField CreateMultilineText(PdfDocument doc, Rectangle rect, String name, String value
660655
) {
661-
try {
662-
return CreateText(doc, rect, name, value, PdfFontFactory.CreateFont(), (float)DEFAULT_FONT_SIZE, true);
663-
}
664-
catch (System.IO.IOException e) {
665-
throw new PdfException(e);
666-
}
656+
return CreateText(doc, rect, name, value, null, -1, true);
667657
}
668658

669659
/// <summary>
@@ -3274,71 +3264,59 @@ protected internal virtual Object[] GetFontAndSize(PdfDictionary asNormal) {
32743264
if (asNormal != null) {
32753265
normalResources = asNormal.GetAsDictionary(PdfName.Resources);
32763266
}
3267+
PdfDictionary daFontDict = null;
3268+
PdfName daFontName = null;
3269+
Object[] dab = new Object[3];
32773270
if (defaultResources != null || normalResources != null) {
32783271
PdfDictionary normalFontDic = normalResources != null ? normalResources.GetAsDictionary(PdfName.Font) : null;
32793272
PdfDictionary defaultFontDic = defaultResources != null ? defaultResources.GetAsDictionary(PdfName.Font) :
32803273
null;
32813274
PdfString defaultAppearance = GetDefaultAppearance();
32823275
if ((normalFontDic != null || defaultFontDic != null) && defaultAppearance != null) {
3283-
Object[] dab = SplitDAelements(defaultAppearance.ToUnicodeString());
3284-
PdfName fontName = new PdfName(dab[DA_FONT].ToString());
3285-
fontAndSize[2] = fontName;
3286-
PdfDictionary requiredFontDictionary = null;
3287-
if (normalFontDic != null && null != normalFontDic.GetAsDictionary(fontName)) {
3288-
requiredFontDictionary = normalFontDic.GetAsDictionary(fontName);
3289-
}
3290-
else {
3291-
if (defaultFontDic != null) {
3292-
requiredFontDictionary = defaultFontDic.GetAsDictionary(fontName);
3276+
dab = SplitDAelements(defaultAppearance.ToUnicodeString());
3277+
Object fontNameObj = dab[DA_FONT];
3278+
if (fontNameObj != null) {
3279+
daFontName = new PdfName(fontNameObj.ToString());
3280+
// according to spec, DA font shall be taken from the DR
3281+
if (defaultFontDic != null && null != defaultFontDic.GetAsDictionary(daFontName)) {
3282+
daFontDict = defaultFontDic.GetAsDictionary(daFontName);
3283+
}
3284+
else {
3285+
if (normalFontDic != null) {
3286+
// search normal appearance as a fall back in case it was not found in DR
3287+
daFontDict = normalFontDic.GetAsDictionary(daFontName);
3288+
}
32933289
}
3294-
}
3295-
if (font != null) {
3296-
fontAndSize[0] = font;
3297-
}
3298-
else {
3299-
PdfFont dicFont = document != null ? document.GetFont(requiredFontDictionary) : PdfFontFactory.CreateFont(
3300-
requiredFontDictionary);
3301-
fontAndSize[0] = dicFont;
3302-
}
3303-
if (fontSize >= 0) {
3304-
fontAndSize[1] = fontSize;
3305-
}
3306-
else {
3307-
fontAndSize[1] = dab[DA_SIZE];
3308-
}
3309-
if (color == null) {
3310-
color = (Color)dab[DA_COLOR];
3311-
}
3312-
}
3313-
else {
3314-
if (font != null) {
3315-
fontAndSize[0] = font;
3316-
}
3317-
else {
3318-
fontAndSize[0] = PdfFontFactory.CreateFont();
3319-
}
3320-
if (fontSize >= 0) {
3321-
fontAndSize[1] = fontSize;
3322-
}
3323-
else {
3324-
fontAndSize[1] = (float)DEFAULT_FONT_SIZE;
33253290
}
33263291
}
33273292
}
3293+
if (font != null) {
3294+
fontAndSize[0] = font;
3295+
}
33283296
else {
3329-
if (font != null) {
3330-
fontAndSize[0] = font;
3297+
if (daFontDict != null) {
3298+
PdfFont daFont = document != null ? document.GetFont(daFontDict) : PdfFontFactory.CreateFont(daFontDict);
3299+
fontAndSize[0] = daFont;
3300+
fontAndSize[2] = daFontName;
33313301
}
33323302
else {
33333303
fontAndSize[0] = PdfFontFactory.CreateFont();
33343304
}
3335-
if (fontSize >= 0) {
3336-
fontAndSize[1] = fontSize;
3305+
}
3306+
if (fontSize >= 0) {
3307+
fontAndSize[1] = fontSize;
3308+
}
3309+
else {
3310+
if (dab[DA_SIZE] != null) {
3311+
fontAndSize[1] = dab[DA_SIZE];
33373312
}
33383313
else {
33393314
fontAndSize[1] = (float)DEFAULT_FONT_SIZE;
33403315
}
33413316
}
3317+
if (color == null) {
3318+
color = (Color)dab[DA_COLOR];
3319+
}
33423320
return fontAndSize;
33433321
}
33443322

@@ -3407,7 +3385,7 @@ protected internal static Object[] SplitDAelements(String da) {
34073385
}
34083386
}
34093387
}
3410-
catch (System.IO.IOException) {
3388+
catch (Exception) {
34113389
}
34123390
return ret;
34133391
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6a3d9021cfabe03aaf5ee7851ce08968e95af1a4
1+
6e8b824838790fba08afbe516ae8c9b8d43120f4

0 commit comments

Comments
 (0)