Page 1 of 1

Custom properties and PDF-output

PostPosted: Sat Nov 15, 2014 3:12 am
by jimka
Hello,

I'm trying to convert a docx-file to PDF. The docx-file contains a number of custom document properties and these are used throughout the document. The problem is that the properties values vanishes when I convert the document to PDF.

The code I'm currently using to convert to PDF is the following
Code: Select all

    public InputStream toPDF(InputStream is) throws DocumentProcessorException {
        WordprocessingMLPackage doc;

        try {
            doc = WordprocessingMLPackage.load(is);
        } catch (Docx4JException ex) {
            throw new DocumentProcessorException(ex);
        }

        FOSettings foSettings = Docx4J.createFOSettings();
        foSettings.setWmlPackage(doc);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            Docx4J.toFO(foSettings, baos, Docx4J.FLAG_EXPORT_PREFER_XSL);
        } catch (Docx4JException ex) {
            throw new DocumentProcessorException(ex);
        }

        byte[] ba = baos.toByteArray();
        try {
            baos.close();
        } catch (IOException ex) {
            throw new DocumentProcessorException(ex);
        }

        return new ByteArrayInputStream(ba);
    }



I'm not expecting the properties themselves to be transferred to the PDF, but I am expecting the values to show up as ordinary text in the PDF.

Is there something other then the above I need to do to archieve this?

Re: Custom properties and PDF-output

PostPosted: Mon Nov 17, 2014 5:18 pm
by jason
Hi, you need to invoke FieldUpdater first.

See https://github.com/plutext/docx4j/blob/ ... dater.java

Re: Custom properties and PDF-output

PostPosted: Mon Nov 17, 2014 11:33 pm
by jimka
That did get me a bit further. The first property shows up but the rest are still missing. Now I'm also getting a curious error message in the resulting PDF-file. No exception is thrown.

Please see the attachments.

Re: Custom properties and PDF-output

PostPosted: Tue Nov 18, 2014 12:15 am
by jimka
I just noticed another curious error, I'm getting these exceptions.

Code: Select all

javax.xml.transform.TransformerException: org.docx4j.model.fields.FieldValueException: No value found for DOCPROPERTY TEST2  \* MERGEFORMAT
   at org.docx4j.convert.out.common.writer.AbstractFldSimpleWriter$DocPropertyHandler.toString(AbstractFldSimpleWriter.java:124)
   at org.docx4j.convert.out.common.writer.AbstractFldSimpleWriter.toNode(AbstractFldSimpleWriter.java:243)
   at org.docx4j.convert.out.common.AbstractWriterRegistry.toNode(AbstractWriterRegistry.java:138)
   at org.docx4j.convert.out.common.AbstractWriterRegistry.toNode(AbstractWriterRegistry.java:109)
   at org.docx4j.convert.out.common.XsltCommonFunctions.toNode(XsltCommonFunctions.java:152)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:343)
   at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:440)
   at org.apache.xalan.extensions.ExtensionsTable.extFunction(ExtensionsTable.java:222)
   at org.apache.xalan.transformer.TransformerImpl.extFunction(TransformerImpl.java:473)
   at org.apache.xpath.functions.FuncExtFunction.execute(FuncExtFunction.java:208)
   at org.apache.xpath.XPath.execute(XPath.java:337)
   at org.apache.xalan.templates.ElemCopyOf.execute(ElemCopyOf.java:134)
   at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:395)
   at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:178)
   at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
   at org.apache.xalan.transformer.TransformerImpl.transformToRTF(TransformerImpl.java:1988)
   at org.apache.xalan.transformer.TransformerImpl.transformToRTF(TransformerImpl.java:1910)
   at org.apache.xalan.templates.ElemVariable.getValue(ElemVariable.java:312)
   at org.apache.xalan.templates.ElemVariable.execute(ElemVariable.java:248)
   at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:395)
   at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:178)
   at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
   at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:1376)
   at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
   at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:1376)
   at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:395)
   at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:178)
   at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
   at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:1376)
   at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:395)
   at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:178)
   at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
   at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java:2270)
   at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1356)
   at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:709)
   at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1273)
   at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1251)
   at org.docx4j.XmlUtils.transform(XmlUtils.java:1024)
   at org.docx4j.XmlUtils.transform(XmlUtils.java:915)
   at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.process(AbstractXsltExporterDelegate.java:66)
   at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:63)
   at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:32)
   at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:78)
   at org.docx4j.Docx4J.toFO(Docx4J.java:460)
Caused by: org.docx4j.model.fields.FieldValueException: No value found for DOCPROPERTY TEST2  \* MERGEFORMAT
   at org.docx4j.model.fields.docproperty.DocPropertyResolver.getValue(DocPropertyResolver.java:73)
   at org.docx4j.convert.out.common.writer.AbstractFldSimpleWriter$DocPropertyHandler.toString(AbstractFldSimpleWriter.java:116)
   ... 49 more


Don't know why it claims this since they're obviously in there.

Re: Custom properties and PDF-output

PostPosted: Tue Nov 18, 2014 12:28 am
by jimka
Seems to be a bug in the AbstractFldSimpleWriter-class at line 113.

Code: Select all
         List<String> params = model.getFldParameters();
         
         //String key = params.get(0);
         String key = model.getFldParameterString();


Should instead be

Code: Select all
         List<String> params = model.getFldParameters();
         
         String key = params.get(0);


It has obviously been correct at one point in time.

Re: Custom properties and PDF-output

PostPosted: Tue Nov 18, 2014 11:39 am
by jason

Re: Custom properties and PDF-output

PostPosted: Thu Nov 20, 2014 12:48 am
by jimka
Thanks!

Re: Custom properties and PDF-output

PostPosted: Tue Oct 10, 2017 11:27 pm
by bs_dellacqua
I'm using docx4j 3.3.3 and the example class ConvertOutPDF.java, if I require to convert into PDF the file doc.docx (attached in previous answer to this item) I have the error
Exception in thread "main" org.docx4j.model.fields.FieldFormattingException: TODO: convert org.docx4j.docProps.custom.Properties$Property to string
at org.docx4j.model.fields.docproperty.DocPropertyResolver.getValue(DocPropertyResolver.java:81)
at org.docx4j.model.fields.FieldUpdater.updateSimple(FieldUpdater.java:119)
at org.docx4j.model.fields.FieldUpdater.updatePart(FieldUpdater.java:84)
at org.docx4j.model.fields.FieldUpdater.update(FieldUpdater.java:56)
at carlo.ConvertOutPDF.main(ConvertOutPDF.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Do I have to modify the file DocPropertyResolver.java to extract the value contained into the Property ?
Thanks

Re: Custom properties and PDF-output

PostPosted: Wed Oct 11, 2017 3:07 am
by jason
attach a short sample docx exhibiting the issue?

Re: Custom properties and PDF-output

PostPosted: Wed Oct 11, 2017 3:50 am
by bs_dellacqua
I've used the file doc.docx attached to this thread on "Mon Nov 17, 2014 11:33 pm "

Re: Custom properties and PDF-output

PostPosted: Wed Oct 11, 2017 3:57 am
by bs_dellacqua
... I've just now downloaded it, executed the conversion with the same error, and now I'm attaching it.

Re: Custom properties and PDF-output

PostPosted: Fri Oct 13, 2017 12:45 am
by jason
bs_dellacqua wrote:Do I have to modify the file DocPropertyResolver.java to extract the value contained into the Property ?


Yes, done now: https://github.com/plutext/docx4j/commi ... f6618dc251

Re: Custom properties and PDF-output

PostPosted: Fri Oct 13, 2017 3:07 am
by bs_dellacqua
Thanks a lot for the solution.
Do you know when this correction will be contained into an official library jar file ?

Re: Custom properties and PDF-output

PostPosted: Sat Oct 14, 2017 3:17 am
by jason
As you might have noticed, 3.3.6 was released just a week ago now, so other things being equal, it would usually be some time before enough changed to warrant a new release.

That said, I be happy to post a "nightly" build (though that's probably only useful if you aren't building docx4j yourself).

cheers .. Jason