Page 1 of 1

problem with JasperReports docx output

PostPosted: Fri Mar 18, 2011 6:28 pm
by cristifl
Hello everyone,
I have a task to add header and footer to our docx files that are created using other free source solution based on Java,
called Jasper Reports.
This solution does not allow adding header/footer so I thought of using docx4j.
Now the problem is that the files exported by Jasper are somehow incomplete or corrupted.
I attached 2 files for example.
The code that I'm trying to use is HeaderFooterCreate. This is working fine for docx files created by MS Word or docx4j samples, but not for my files :cry: .
On the other hand, MS Word opens them without any problems and if I hit Save As "filename2.docx" then docx4j can open them.

My question is, can I somehow fix these files so that I can edit them using docx4j to add header/footer?
By fixing I mean editing them with java code, not manually with MS Word.

Thank you,
Cristi

Re: problem with other docx files

PostPosted: Sun Mar 20, 2011 12:18 pm
by jason
Hello Cristi

Running your first docx through the docx4j sample OpenAndSaveRoundTripTest results in content Word 2007 doesn't want to open :-(

On closer inspection, the problem is that the input docx (ie the one created by Jasper Reports), includes in its styles.xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
       <w:rPr>
        <w:sz w:val="1.0" />
       </w:rPr>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


(There is similar content in document.xml as well eg <w:sz w:val="24.0" /> )

The wml xsd defines sz:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting

                        <xsd:element name="sz" type="CT_HpsMeasure" minOccurs="0">
                                <xsd:annotation>
                                        <xsd:documentation>Font Size</xsd:documentation>
                                </xsd:annotation>
                        </xsd:element>

        <xsd:complexType name="CT_HpsMeasure">
                <xsd:annotation>
                        <xsd:appinfo>
                                <jaxb:class name="HpsMeasure"/>
                        </xsd:appinfo>
                </xsd:annotation>
                <xsd:attribute name="val" type="xsd:unsignedLong" use="required">
                        <xsd:annotation>
                                <xsd:documentation>Half Point Measurement</xsd:documentation>
                        </xsd:annotation>
                </xsd:attribute>
        </xsd:complexType>

 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


which in docx4j becomes:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting

@XmlType(name = "CT_HpsMeasure")
public class HpsMeasure
    implements Child
{

    @XmlAttribute(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", required = true)
    @XmlSchemaType(name = "unsignedLong")
    protected BigInteger val;
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


When docx4j opens the docx and encounters the invalid attribute value (a decimal, not an integer), it is dropped.

When the docx is saved, you get an empty <w:sz/> element, which Word doesn't like.

So the solution is an upstream fix in JasperReports. (That should be easy enough to do since JasperReports is open source, but if not, you could traverse your docx in docx4j, looking for empty sz elements and removing them.)

Please feel free to copy the contents of this post if you make a new issue in their tracker.

cheers .. Jason

cheers .. Jason

Re: problem with JasperReports docx output

PostPosted: Tue Mar 22, 2011 2:41 am
by cristifl
Thank you jason!

You are right, after editing the contained xml files and recreating the docx file with them (manually), I could open/edit with docx4j library.
But I don't have enough time to modify our Jasper Reports code, but rather
I'm thinking of writing code to extract and edit the XML files, create a zip archive with them and overwrite the initial docx file.

I've posted messages in the past to the forum at Jasper Reports, so I'll know to tell them about this problem.

Good luck,
Cristi

Re: problem with JasperReports docx output

PostPosted: Tue Mar 22, 2011 8:40 am
by jason
It occurred to me that you could change HpsMeasure so that it round trips a float.

This is wrong of course, but it does enable docx4j to write out the docx created by Jasper Reports.

The patch below does the trick. I won't apply it to docx4j proper, but you could apply it to your own copy.

cheers .. Jason


Index: src/main/java/org/docx4j/wml/HpsMeasure.java
===================================================================
--- src/main/java/org/docx4j/wml/HpsMeasure.java (revision 1440)
+++ src/main/java/org/docx4j/wml/HpsMeasure.java (working copy)
@@ -57,7 +57,7 @@

@XmlAttribute(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", required = true)
@XmlSchemaType(name = "unsignedLong")
- protected BigInteger val;
+ protected Float val;
@XmlTransient
private Object parent;

@@ -69,7 +69,7 @@
* {@link BigInteger }
*
*/
- public BigInteger getVal() {
+ public Float getVal() {
return val;
}

@@ -81,7 +81,7 @@
* {@link BigInteger }
*
*/
- public void setVal(BigInteger value) {
+ public void setVal(Float value) {
this.val = value;
}