Page 1 of 1

Number Format Exception

PostPosted: Wed Jun 11, 2025 7:37 pm
by nagalakshmi_hn
Hello Team,

We use Docx4j in our application to add/remove new paragraphs and to replace some placeholders. Sometimes while replacing placeholders we encounter the NumberFormatException when we invoke Variables.prepare method on Docx4j. A sample exception trace is given below. In the below trace it has happened while saving the signature part, but other times this has happened with some other part. Not sure why this happens exactly. Our customers upload and manipulate the documents in our portal and it becomes very difficult to debug what the actual issue is. Please let us know if there is a solution to this.

Problem saving part /word/ink/ink1.xml
java.lang.NumberFormatException: For input string: "541.22"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.math.BigInteger.<init>(BigInteger.java:536)
at java.base/java.math.BigInteger.<init>(BigInteger.java:674)
at org.glassfish.jaxb.runtime.DatatypeConverterImpl._parseInteger(DatatypeConverterImpl.java:57)
at org.glassfish.jaxb.runtime.v2.model.impl.RuntimeBuiltinLeafInfoImpl$22.parse(RuntimeBuiltinLeafInfoImpl.java:823)
at org.glassfish.jaxb.runtime.v2.model.impl.RuntimeBuiltinLeafInfoImpl$22.parse(RuntimeBuiltinLeafInfoImpl.java:819)
at org.glassfish.jaxb.runtime.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.parse(TransducedAccessor.java:214)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:180)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:530)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:509)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:217)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:149)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:376)
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:347)
at org.docx4j.openpackaging.parts.JaxbXmlPart.unmarshal(JaxbXmlPart.java:1078)
at org.docx4j.openpackaging.parts.JaxbXmlPart.getContents(JaxbXmlPart.java:201)
at org.docx4j.openpackaging.parts.JaxbXmlPart.getJaxbElement(JaxbXmlPart.java:221)

Thanks,

Lakshmi

Re: Number Format Exception

PostPosted: Thu Jun 12, 2025 11:51 am
by jason
Typically this happens when a document has been edited by something other than Microsoft Word, and saved with invalid content.

Usually you'd correct this using the pre-processor.

For example, see the default: https://github.com/plutext/docx4j/blob/ ... essor.xslt

which you can override: https://github.com/plutext/docx4j/blob/ ... rties#L197

I'd suggest you change your exception handling so you can associate the error with the docx which caused it. Then it'll be straightforward to address it.

Re: Number Format Exception

PostPosted: Thu Jun 12, 2025 4:00 pm
by nagalakshmi_hn
Thank you Json. You mentioned about custom pre-processor. I saw the property setting under the link you have given,

docx4j.jaxb.JaxbValidationEventHandler=custom-preprocessor.xslt.

I believe this custom-preprocessor.xslt is part of the Docx4j library or do we have to include any other jar file for this.

Secondly you have suggested to handle it in the exception block. When you say handle, what is that we need to do exactly when we catch NumberFormatException. Do you mean to say log such errors and find out which document caused the issue and manually correct the document.

It will be great if you can clarify these.

Thanks

Re: Number Format Exception

PostPosted: Fri Jun 13, 2025 8:51 am
by jason
docx4j contains mc-preprocessor.xslt

You can copy and change that to make your custom-preprocessor, which then overrides it.

Alternatively you can copy and change the custom-preprocessor.xslt example in docx4j-samples-resources

Regarding logging, yes, the idea is to understand which docx is causing the issue so you can reproduce it as a step on the way to fixing it.

Re: Number Format Exception

PostPosted: Fri Jun 13, 2025 3:44 pm
by nagalakshmi_hn
Thank you Json