Skip to content

Commit 07f9561

Browse files
mrsafimichaeldemey
authored andcommitted
fixed findFieldName method not giving the result more than one time.
1 parent 64c7997 commit 07f9561

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

forms/src/main/java/com/itextpdf/forms/xfa/XfaForm.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,11 @@ This file is part of the iText (R) project.
5555
import com.itextpdf.kernel.pdf.PdfVersion;
5656
import com.itextpdf.kernel.pdf.VersionConforming;
5757
import com.itextpdf.kernel.xmp.XmlDomWriter;
58-
import org.w3c.dom.Document;
59-
import org.w3c.dom.Element;
60-
import org.w3c.dom.Node;
61-
import org.w3c.dom.NodeList;
62-
import org.xml.sax.EntityResolver;
63-
import org.xml.sax.InputSource;
64-
import org.xml.sax.SAXException;
6558

6659
import javax.xml.parsers.DocumentBuilder;
6760
import javax.xml.parsers.DocumentBuilderFactory;
6861
import javax.xml.parsers.ParserConfigurationException;
62+
6963
import java.io.ByteArrayInputStream;
7064
import java.io.ByteArrayOutputStream;
7165
import java.io.File;
@@ -77,6 +71,14 @@ This file is part of the iText (R) project.
7771
import java.util.HashMap;
7872
import java.util.Map;
7973

74+
import org.w3c.dom.Document;
75+
import org.w3c.dom.Element;
76+
import org.w3c.dom.Node;
77+
import org.w3c.dom.NodeList;
78+
import org.xml.sax.EntityResolver;
79+
import org.xml.sax.InputSource;
80+
import org.xml.sax.SAXException;
81+
8082
/**
8183
* Processes XFA forms.
8284
*/
@@ -331,10 +333,14 @@ public boolean isXfaPresent() {
331333
* @return the complete name or <CODE>null</CODE> if not found
332334
*/
333335
public String findFieldName(String name) {
334-
if (acroFieldsSom == null && xfaPresent) {
336+
if (acroFieldsSom == null && xfaPresent && datasetsSom != null ) {
335337
acroFieldsSom = new AcroFieldsSearch(datasetsSom.getName2Node().keySet());
338+
}
339+
340+
if (acroFieldsSom != null && xfaPresent) {
336341
return acroFieldsSom.getAcroShort2LongName().containsKey(name) ? acroFieldsSom.getAcroShort2LongName().get(name) : acroFieldsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
337342
}
343+
338344
return null;
339345
}
340346

forms/src/test/java/com/itextpdf/forms/xfa/XFAFormTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.kernel.utils.CompareTool;
5050
import com.itextpdf.test.ExtendedITextTest;
5151
import com.itextpdf.test.annotations.type.IntegrationTest;
52+
53+
import java.io.FileInputStream;
54+
import java.io.IOException;
55+
5256
import org.junit.Assert;
5357
import org.junit.BeforeClass;
5458
import org.junit.Test;
5559
import org.junit.experimental.categories.Category;
5660

57-
import java.io.FileInputStream;
58-
import java.io.IOException;
59-
6061
@Category(IntegrationTest.class)
6162
public class XFAFormTest extends ExtendedITextTest {
6263

@@ -119,5 +120,25 @@ public void readXFAFormTest() throws IOException {
119120
PdfAcroForm.getAcroForm(pdfDocument, true);
120121
}
121122

123+
@Test
124+
public void findFieldName() throws IOException {
125+
String inFileName = sourceFolder + "TextField1.pdf";
126+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFileName));
127+
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDocument, true);
128+
XfaForm xfaForm = acroForm.getXfaForm();
129+
xfaForm.findFieldName("TextField1");
130+
String secondRun = xfaForm.findFieldName("TextField1");
131+
Assert.assertNotNull(secondRun);
132+
}
133+
134+
@Test
135+
public void findFieldNameWithoutDataSet() throws IOException {
136+
String inFileName = sourceFolder + "TextField1_empty.pdf";
137+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFileName));
138+
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDocument, true);
139+
XfaForm xfaForm = acroForm.getXfaForm();
140+
String name = xfaForm.findFieldName("TextField1");
141+
Assert.assertNull(name);
142+
}
122143

123144
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)