tags:

views:

73

answers:

2

the following code (using iText library):

PdfStamper stamp = new PdfStamper(reader, outputStream);
AcroFields form = stamp.getAcroFields();
String name = "form1[0].#subform[0].Table1[0].#subformSet[0].Row[2].#field[0]";
form.setField(name, "");

produces the following error:

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
 at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkQName(CoreDocumentImpl.java:2571)
 at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(ElementNSImpl.java:117)
 at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(ElementNSImpl.java:80)
 at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElementNS(CoreDocumentImpl.java:2084)
 at com.lowagie.text.pdf.XfaForm$Xml2SomDatasets.insertNode(Unknown Source)
 at com.lowagie.text.pdf.AcroFields.setField(Unknown Source)
 at com.lowagie.text.pdf.AcroFields.setField(Unknown Source)

obviously this is because of '#' sign in field name. This field's name come from AcroFields.getFields() collection and it seems very strange that setting back this value produces an error.

Are there any ways of dealing with this error without changing real field name?

+1  A: 

You could consider using the unicode (UTF-8) character code for this instead, or perhaps escaping the character itself.

Generic Prodigy
What do you mean by "unicode (UTF-8) character code"? `#` is U+0023 and since that is less than 128, it is the same value in ASCII and in UTF-8.
Joachim Sauer
A: 

to be more concrete:

String name = "form1[0].#subform[0].Table1[0].#subformSet[0].Row[2].#field[0]"; 
form.setField(name.replace("#", "&#23;"), ""); 
kilonet