Page 1 of 1

Setting document properties and generating a PDF from a DOCX

PostPosted: Tue Jun 13, 2017 12:20 am
by sarbogast
Here is what I'm trying to do with Docx4J 3.3.3
I would like to set values for 4 custom document properties in a DOCX file loading from byte[] in a database
Then force update the fields
Then export the docx file as a PDF.

Here is the code I'm using for the first 2 steps:
Code: Select all
        byte[] templateContent = template.content
        InputStream inputStream = new ByteArrayInputStream(templateContent)
        WordprocessingMLPackage wordMLPackage = Docx4J.load(inputStream)
        //set document properties
        DocPropsCustomPart docProps = wordMLPackage.getDocPropsCustomPart()
        docProps.setProperty('EmployeeName', employee.fullName)
        docProps.setProperty('EmployeeStaffNumber', employee.staffNumber)
        docProps.setProperty('EmployeeCity', employee.address.city)
        docProps.setProperty('ContractDate', LocalDate.now().toString())
        FieldUpdater fieldUpdater = new FieldUpdater(wordMLPackage)
        fieldUpdater.update(true)
        wordMLPackage.save(new File("/Users/sarbogast/Desktop/${employee.staffNumber}_${new Date().getTime()}.docx"))


But the line where fieldUpdater tries to update throws an exception:
Code: Select all
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
   at java.util.ArrayList.rangeCheck(ArrayList.java:653)
   at java.util.ArrayList.get(ArrayList.java:429)
   at org.docx4j.model.fields.FieldRef.getFldName(FieldRef.java:296)
   at org.docx4j.model.fields.FieldUpdater.updateComplex(FieldUpdater.java:214)
   at org.docx4j.model.fields.FieldUpdater.updatePart(FieldUpdater.java:85)
   at org.docx4j.model.fields.FieldUpdater.update(FieldUpdater.java:56)
   at com.adessa.unbox.signature.SignatureService.$tt__createSignatureRequest(SignatureService.groovy:46)
   at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
   at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
   at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
   at com.adessa.unbox.signature.SignatureController.create(SignatureController.groovy:13)
   ... 34 common frames omitted


Am I doing something wrong? Is this a known issue?

Re: Setting document properties and generating a PDF from a

PostPosted: Wed Jun 14, 2017 9:51 pm
by jason
Looks like an issue identifying field name.

Have a look at the corresponding XML to see whether there is anything unusual about it?