I've found that adding a long (>127) value as a hidden field, will throw a NullPointerException from within PdfName.
Exception in thread "main" java.lang.NullPointerException
at com.lowagie.text.pdf.PdfName.<init>(PdfName.java:782)
at com.lowagie.text.pdf.PdfFormField.setValueAsName()
at com.lowagie.text.pdf.PdfAcroForm.addHiddenField()
at tst03.main(tst03.java:18)I think this is due to a typo in the PdfName constructor where the message string for the IllegalArgumentException is created.
Attached is a small testcase tst03.java and an even smaller patch that fixes the NPE (and throws the correct exception).
At a higher level, I wonder why hidden field values are added as a PdfName instead of a PdfString? I realize that I can added the value as a string myself:
PdfFormField f = PdfFormField.createEmpty(writer);
f.setFieldName("hidden");
f.setValueAsString("...");
acroForm.addFormField(f);but perhaps using a Name is a deliberate choice?
Either way, if my document contains a single hidden field and no other fields, it appears that iText does not write the AcroForm dict into the document. The attached program tst05.java shows this problem.
I'm guessing that the PdfAcroForm.isValue() should return true even when there is no fieldTemplates. In that case the /DR and /DA entries should not be added to the AcroForm object.
regards, finn
Index: PdfName.java
===================================================================
RCS file: /cvsroot/itext/src/com/lowagie/text/pdf/PdfName.java,v
retrieving revision 1.51
diff -u -r1.51 PdfName.java
--- PdfName.java 1 Oct 2003 11:05:41 -0000 1.51
+++ PdfName.java 7 Oct 2003 19:36:28 -0000
@@ -779,7 +779,7 @@
// The minimum number of characters in a name is 0, the maximum is 127 (the
'/' not included)
int length = name.length();
if (length > 127) {
- throw new IllegalArgumentException("The name is too long (" +
bytes.length + " characters).");
+ throw new IllegalArgumentException("The name is too long (" + length + "
characters).");
}
// The name has to be checked for illegal characters
// every special character has to be substituted
import java.io.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*;
public class tst05 {
public static void main(String[] args) throws Exception {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc,
new FileOutputStream("tst05.pdf"));
PdfAcroForm acroForm = writer.getAcroForm();
doc.open();
doc.add(new Paragraph("Hello World"));
acroForm.addHiddenField("hidden", "value");
doc.close();
PdfReader reader = new PdfReader("tst05.pdf");
System.out.println("The acroform object is " + reader.getAcroForm());
}
}
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class tst03 {
public static void main(String[] args) throws Exception {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc,
new FileOutputStream("tst03.pdf"));
PdfAcroForm acroForm = writer.getAcroForm();
doc.open();
doc.add(new Paragraph("Hello World"));
String value = "x";
for (int i = 0; i < 8; i++)
value = value + value;
acroForm.addHiddenField("hidden", value);
doc.close();
}
}
