Page 1 of 1

new user issues

PostPosted: Thu Jan 21, 2016 3:24 pm
by denov
all,

i'm trying to create a somewhat simple word doc. and i'm ending up with a lot of extra lines between paragraphs. here's what i've done.

i created a word doc as a template. i then uploaded to http://webapp.docx4java.org/ (great tool!) to explore get the code needed to create via docx4j. I copied the XML (method 2) code from the first 4 paragraphs. I clicked on document and then the P elements. Created methods where I could pass in a string to replace the templated value.

Code: Select all
    private P getP(String part) throws JAXBException {
        String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
                + "<w:pPr>"
                + "<w:rPr>"
                    + "<w:rFonts w:ascii=\"PT Serif\" w:hAnsi=\"PT Serif\"/>"
                    + "<w:sz w:val=\"18\"/>"
                    + "<w:szCs w:val=\"18\"/>"
                +"</w:rPr>"
                +"</w:pPr>"
                + "<w:r>"
                    + "<w:rPr>"
                    + "<w:rFonts w:ascii=\"PT Serif\" w:hAnsi=\"PT Serif\"/>"
                    + "<w:sz w:val=\"18\"/>"
                    + "<w:szCs w:val=\"18\"/>"
                    +"</w:rPr>"
                    + "<w:t>"+ part +"</w:t>"
                +"</w:r>"
                +"</w:p>";

        return (P) XmlUtils.unmarshalString(openXML);
    }


And then assembling the document with

Code: Select all
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

        mdp.addObject(getP("Population: Some Population"));
        mdp.addObject(getP("Intervention: Some intervention"));
        mdp.addObject(getP("Comparator: Some comparator"));

        String filename = System.getProperty("user.dir") + "/OUT_samplePICO.docx";
        wordMLPackage.save(new java.io.File(filename));



I haven't noticed any difference in the XML from the template in explorer app vs what this outputs.

Code: Select all
       
System.out.println(XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true));


What am i missing and why do I have extra spaces?

Re: new user issues

PostPosted: Thu Jan 21, 2016 3:52 pm
by jason
Styles part.

Re: new user issues

PostPosted: Thu Jan 21, 2016 4:00 pm
by denov
jason wrote:Styles part.


how do I add a style part? i tried

Code: Select all
wordMLPackage.getMainDocumentPart().addObject()

Re: new user issues

PostPosted: Thu Jan 21, 2016 4:23 pm
by denov
this doesn't seem to work either

Code: Select all
       
StyleDefinitionsPart styles = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart();
styles.setContents(getStyles());

Re: new user issues

PostPosted: Thu Jan 21, 2016 4:44 pm
by jason
createPackage() adds a styles part: https://github.com/plutext/docx4j/blob/ ... .java#L397

But the default paragraph style and docdefaults might well be different from what you had in your docx

You'll need to make sure they are the same.

You can either alter the values (fetch then change), or replace the entire styles part with your own.

Here is some code which uses your own styles part, from your classpath (or some other input stream):

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

                java.io.InputStream is = ResourceUtils.getResource("your/styles.xml");
               
                Styles styles = (Styles)XmlUtils.unmarshal(is);
               
                WordprocessingMLPackage pkg = createdPkgWithStyles(styles);
               
       
        private static WordprocessingMLPackage createdPkgWithStyles(Styles styles) throws InvalidFormatException {
               
                // Create a package
                WordprocessingMLPackage wmlPack = new WordprocessingMLPackage();

                // Create main document part
                MainDocumentPart wordDocumentPart = new MainDocumentPart();            
               
                // Create main document part content
                org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
                org.docx4j.wml.Body  body = factory.createBody();              
                org.docx4j.wml.Document wmlDocumentEl = factory.createDocument();
               
                wmlDocumentEl.setBody(body);
                                               
                // Put the content in the part
                wordDocumentPart.setJaxbElement(wmlDocumentEl);
                                               
                // Add the main document part to the package relationships
                // (creating it if necessary)
                wmlPack.addTargetPart(wordDocumentPart);
                               
                // Create a styles part
                StyleDefinitionsPart stylesPart = new org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart();
                try {
                       
                        stylesPart.setJaxbElement(styles);
                       
                        // Add the styles part to the main document part relationships
                        // (creating it if necessary)
                        wordDocumentPart.addTargetPart(stylesPart); // NB - add it to main doc part, not package!                      
                       
                } catch (Exception e) {
                        e.printStackTrace();   
                }
               
                return wmlPack;
        }

 
Parsed in 0.017 seconds, using GeSHi 1.0.8.4

Re: new user issues

PostPosted: Thu Jan 21, 2016 5:31 pm
by denov
great! now i've got my style sheet attached but it didn't do anything for my layout.

Re: new user issues

PostPosted: Thu Jan 21, 2016 5:35 pm
by denov
you should add that createdPkgWithStyles method to WordprocessingMLPackage. one for themes would also be useful.

Re: new user issues

PostPosted: Thu Jan 21, 2016 5:57 pm
by denov
the code the explore tool outputs throws a can't cast JAXBElement to CTSettings

Code: Select all
String openXML = "<w:settings mc:Ignorable=\"w14\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\">"
            + "<w:zoom w:percent=\"100\"/>"
            + "<w:defaultTabStop w:val=\"708\"/>"
            + "<w:hyphenationZone w:val=\"425\"/>"
            + "<w:characterSpacingControl w:val=\"doNotCompress\"/>"
            + "<w:savePreviewPicture/>"
            + "<w:compat>"
                + "<w:useFELayout/>"
                + "<w:compatSetting w:name=\"compatibilityMode\" w:uri=\"http://schemas.microsoft.com/office/word\" w:val=\"14\"/>"
                + "<w:compatSetting w:name=\"overrideTableStyleFontSizeAndJustification\" w:uri=\"http://schemas.microsoft.com/office/word\" w:val=\"1\"/>"
                + "<w:compatSetting w:name=\"enableOpenTypeFeatures\" w:uri=\"http://schemas.microsoft.com/office/word\" w:val=\"1\"/>"
                + "<w:compatSetting w:name=\"doNotFlipMirrorIndents\" w:uri=\"http://schemas.microsoft.com/office/word\" w:val=\"1\"/>"
            +"</w:compat>"
            + "<w:rsids>"
                + "<w:rsidRoot w:val=\"000E1E37\"/>"
                + "<w:rsid w:val=\"000D28AC\"/>"
                + "<w:rsid w:val=\"000E1E37\"/>"
                + "<w:rsid w:val=\"0021724C\"/>"
                + "<w:rsid w:val=\"0026569E\"/>"
                + "<w:rsid w:val=\"00817F49\"/>"
                + "<w:rsid w:val=\"008842D9\"/>"
                + "<w:rsid w:val=\"009049DC\"/>"
                + "<w:rsid w:val=\"00A55A64\"/>"
                + "<w:rsid w:val=\"00DD56F0\"/>"
            +"</w:rsids>"
            + "<m:mathPr>"
                + "<m:mathFont m:val=\"Cambria Math\"/>"
                + "<m:brkBin m:val=\"before\"/>"
                + "<m:brkBinSub m:val=\"--\"/>"
                + "<m:smallFrac m:val=\"0\"/>"
                + "<m:dispDef/>"
                + "<m:lMargin m:val=\"0\"/>"
                + "<m:rMargin m:val=\"0\"/>"
                + "<m:defJc m:val=\"centerGroup\"/>"
                + "<m:wrapIndent m:val=\"1440\"/>"
                + "<m:intLim m:val=\"subSup\"/>"
                + "<m:naryLim m:val=\"undOvr\"/>"
            +"</m:mathPr>"
            + "<w:themeFontLang w:eastAsia=\"ja-JP\" w:val=\"nb-NO\"/>"
            + "<w:clrSchemeMapping w:accent1=\"accent1\" w:accent2=\"accent2\" w:accent3=\"accent3\" w:accent4=\"accent4\" w:accent5=\"accent5\" w:accent6=\"accent6\" w:bg1=\"light1\" w:bg2=\"light2\" w:followedHyperlink=\"followedHyperlink\" w:hyperlink=\"hyperlink\" w:t1=\"dark1\" w:t2=\"dark2\"/>"
            + "<w:doNotAutoCompressPictures/>"
            + "<w:decimalSymbol w:val=\",\"/>"
            + "<w:listSeparator w:val=\";\"/>"
            + "<w14:docId w14:val=\"4BA3CE7B\"/>"
            + "<w14:defaultImageDpi w14:val=\"300\"/>"
        +"</w:settings>";
CTSettings settings = (CTSettings)XmlUtils.unmarshalString(openXML);


and should i be setting this in your
Code: Select all
createPkgWithStyles
method with

Code: Select all
wordDocumentPart.getDocumentSettingsPart().setContents(settings)

Re: new user issues

PostPosted: Thu Jan 21, 2016 8:02 pm
by jason
denov wrote:What am i missing and why do I have extra spaces?


Feel free to attach your original docx, and your docx4j docx, and I'll take a look.

Re: new user issues

PostPosted: Fri Jan 22, 2016 7:31 am
by denov
that would be great! thx.

Re: new user issues

PostPosted: Tue Feb 02, 2016 10:52 am
by denov
are there any more troubleshooting steps you could suggest or anything i might want to look at? i would love to get this working.

Re: new user issues

PostPosted: Wed Feb 03, 2016 9:02 pm
by jason
I've just opened your documents now;

I can see that "Simple" contains a few empty paragraphs not present in "OUT", but that's about it (and easily addressed).

If you point to the specific differences of concern (eg by the referencing the actual text), I'll look more closely.

Re: new user issues

PostPosted: Thu Feb 04, 2016 12:09 pm
by denov
thanks for looking. but you're not seeing a bunch of extra lines between lines 3-6 (population, intervention, comparator)?

maybe it's on open office thing. i'm using open office and not ms-word. i'll try to open my out doc on a real copy of word next.

Re: new user issues

PostPosted: Thu Feb 04, 2016 12:16 pm
by denov
so it is open office. i got access to a machine with word 2010 on it. it opens and looks fine.

so there's something not right about the output and open office :(