Page 1 of 1

Creating underlined / centered text.

PostPosted: Tue May 21, 2013 12:40 pm
by wreed12345
I have found out I can easily create underlined using
Code: Select all
String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:u>Bold text </w:u></w:t></w:r></w:p>";


Is there a simple way to create underlined text or centered text like this? I have no xml (which i think that is right?) experience besides some minor android stuff, so i am not too sure. THanks in advanced

Re: Creating underlined / centered text.

PostPosted: Tue May 21, 2013 2:12 pm
by jason
Close, but not quite.

Using http://webapp.docx4java.org/OnlineDemo/PartsList.html (see further http://www.docx4java.org/blog/2013/05/d ... eneration/

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:pPr>"
                + "<w:jc w:val=\"center\"/>"
                + "<w:rPr>"
                    + "<w:u w:val=\"single\"/>"
                    + "<w:lang w:val=\"en-AU\"/>"
                +"</w:rPr>"
            +"</w:pPr>"
            + "<w:r>"
                + "<w:rPr>"
                    + "<w:u w:val=\"single\"/>"
                    + "<w:lang w:val=\"en-AU\"/>"
                +"</w:rPr>"
                + "<w:t>Underlined and centred</w:t>"
            +"</w:r>"
        +"</w:p>"

P p = (P)XmlUtils.unmarshalString(openXML);
 
Parsed in 0.017 seconds, using GeSHi 1.0.8.4


or in code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import java.math.BigInteger;
import javax.xml.bind.JAXBElement;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.CTLanguage;
import org.docx4j.wml.CTMarkupRange;
import org.docx4j.wml.Jc;
import org.docx4j.wml.P;
import org.docx4j.wml.PPr;
import org.docx4j.wml.ParaRPr;
import org.docx4j.wml.R;
import org.docx4j.wml.RPr;
import org.docx4j.wml.Text;
import org.docx4j.wml.U;


public class Foo {
public P createIt() {

org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

P p = wmlObjectFactory.createP();
    // Create object for pPr
    PPr ppr = wmlObjectFactory.createPPr();
    p.setPPr(ppr);
        // Create object for rPr
        ParaRPr pararpr = wmlObjectFactory.createParaRPr();
        ppr.setRPr(pararpr);
            // Create object for u
            U u = wmlObjectFactory.createU();
            pararpr.setU(u);
                u.setVal(org.docx4j.wml.UnderlineEnumeration.SINGLE);
            // Create object for lang
            CTLanguage language = wmlObjectFactory.createCTLanguage();
            pararpr.setLang(language);
                language.setVal( "en-AU");
        // Create object for jc
        Jc jc = wmlObjectFactory.createJc();
        ppr.setJc(jc);
            jc.setVal(org.docx4j.wml.JcEnumeration.CENTER);
    // Create object for r
    R r = wmlObjectFactory.createR();
    p.getContent().add( r);
        // Create object for rPr
        RPr rpr = wmlObjectFactory.createRPr();
        r.setRPr(rpr);
            // Create object for u
            U u2 = wmlObjectFactory.createU();
            rpr.setU(u2);
                u2.setVal(org.docx4j.wml.UnderlineEnumeration.SINGLE);
            // Create object for lang
            CTLanguage language2 = wmlObjectFactory.createCTLanguage();
            rpr.setLang(language2);
                language2.setVal( "en-AU");
        // Create object for t (wrapped in JAXBElement)
        Text text = wmlObjectFactory.createText();
        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text);
        r.getContent().add( textWrapped);
            text.setValue( "Underlined and centred");

return p;
}
}
 
Parsed in 0.019 seconds, using GeSHi 1.0.8.4

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 5:40 am
by wreed12345
Thanks for the quick resposne - im getting some errors when trying to run this code however.
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 142; The element type "w:r" must be terminated by the matching end-tag "</w:r>".]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:354)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:334)
at com.reed.america.Main.main(Main.java:23)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 142; The element type "w:r" must be terminated by the matching end-tag "</w:r>".
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
... 7 more

I define this string and try adding it to my document
Code: Select all
      String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
               + "<w:pPr>"
                   + "<w:jc w:val=\"center\"/>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                       + "<w:lang w:val=\"en-AU\"/>"
                   +"</w:rPr>"
               +"</w:pPr>"
               + "<w:r>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                       + "<w:lang w:val=\"en-AU\"/>"
                   +"</w:rPr>"
                   + "<w:t>Underlined and centred</w:t>"
               +"</w:r>"
           +"</w:p>";
      
      wordMLPackage.getMainDocumentPart().addObject(
            org.docx4j.XmlUtils.unmarshalString(openXML));

but no luck :'(

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 6:17 am
by wreed12345
I got things to work underlined by using this by centered is not working :/

Code: Select all
      String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:u w:val=\"single\"/></w:rPr><w:t>Centered and underlined text</w:t></w:r></w:p>";

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 7:37 am
by jason
That code of mine works. And so does the version of it you pasted.

Per the error you got, you obviously left off the closing w:r tag in some other part of your code.

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 8:41 am
by wreed12345
Ok so I found out this code fails when I am doing this:
Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .createPackage();
      //org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
      String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:u>Bold, just at w:r level </w:u></w:t></w:r></w:p>";
      
      wordMLPackage.getMainDocumentPart().addObject(
            org.docx4j.XmlUtils.unmarshalString(str));
      
       String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:pPr><w:jc w:val=\"center\"/><w:rPr><w:u w:val=\"single\"/><w:lang w:val=\"en-AU\"/></w:rPr></w:pPr><w:r><w:rPr><w:u w:val=\"single\"/><w:lang w:val=\"en-AU\"/></w:rPr><w:t>Underlined and centred</w:t></w:r></w:p>";
        
         wordMLPackage.getMainDocumentPart().addObject(
               org.docx4j.XmlUtils.unmarshalString(openXML));

But if i only use the one of those two strings and only add one to the document it works fine. Do I need to create a new object of some sort to make these work or am i not understanding something

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 9:23 am
by jason
Your problem is that "<w:u>Bold, just at w:r level </w:u>" is not valid WordML.

Worse, you have "</w:t>" after it with no corresponding opening tag.

You'll avoid these kinds of errors if you use the code generation tool, instead of crafting your XML manually :-)

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 12:03 pm
by wreed12345
ok so i used that online tool to view how bold text is created. This is what came up for the document
Code: Select all
  <w:document>
            <w:body>
                <w:p>
                    <w:pPr>
                        <w:rPr>
                            <w:b/>
                        </w:rPr>
                    </w:pPr>
                    <w:r>
                        <w:rPr>
                            <w:b/>
                        </w:rPr>
                        <w:t>Bold</w:t>
                    </w:r>
                </w:p>
                <w:p>
                    <w:pPr>
                        <w:rPr>
                            <w:b/>
                        </w:rPr>
                    </w:pPr>
                </w:p>
                <w:p>
                    <w:pPr>
                        <w:jc w:val="center"/>
                    </w:pPr>
                    <w:r>
                        <w:t>Centered</w:t>
                    </w:r>
                </w:p>
                <w:p>
                    <w:pPr>
                        <w:jc w:val="center"/>
                    </w:pPr>
                </w:p>
                <w:p>
                    <w:pPr>
                        <w:jc w:val="both"/>
                        <w:rPr>
                            <w:u w:val="single"/>
                        </w:rPr>
                    </w:pPr>
                    <w:r>
                        <w:rPr>
                            <w:u w:val="single"/>
                        </w:rPr>
                        <w:t>Underlined</w:t>
                    </w:r>
                </w:p>
                <w:p>
                    <w:pPr>
                        <w:jc w:val="both"/>
                        <w:rPr>
                            <w:u w:val="single"/>
                        </w:rPr>
                    </w:pPr>
                </w:p>
                <w:p>
                    <w:pPr>
                        <w:jc w:val="both"/>
                        <w:rPr>
                            <w:b/>
                            <w:u w:val="single"/>
                        </w:rPr>
                    </w:pPr>
                    <w:bookmarkStart w:name="_GoBack" w:id="0"/>
                    <w:r>
                        <w:rPr>
                            <w:b/>
                            <w:u w:val="single"/>
                        </w:rPr>
                        <w:t>Bold and underlined</w:t>
                    </w:r>
                    <w:bookmarkEnd w:id="0"/>
                </w:p>
                <w:sectPr>
                    <w:pgSz w:w="12240" w:h="15840"/>
                    <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
                    <w:cols w:space="720"/>
                    <w:docGrid w:linePitch="360"/>
                </w:sectPr>
            </w:body>
        </w:document>


What exactly would i transfer to a string to make this work?

Re: Creating underlined / centered text.

PostPosted: Wed May 22, 2013 3:10 pm
by jason
Did you notice the hyperlinks?

Basically, you click on the w:p or w:r of interest.

See http://www.docx4java.org/blog/2013/05/d ... eneration/ for walk through.

Re: Creating underlined / centered text.

PostPosted: Thu May 23, 2013 5:39 am
by wreed12345
Oh wow I actually did not notice those. That helps a lot! My final question is how to set the text of one of these things strings:
Code: Select all
      String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">"
               + "<w:pPr>"
                   + "<w:jc w:val=\"center\"/>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                   +"</w:rPr>"
               +"</w:pPr>"
               + "<w:r>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                   +"</w:rPr>"
                   + "<w:t>Underlined</w:t>"
               +"</w:r>"
               + "<w:r>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                   +"</w:rPr>"
                   + "<w:t xml:space=\"preserve\"> and Centered</w:t>"
               +"</w:r>"
           +"</w:p>";

to another pre defined string. Would I have to use the java version of this to get it working?

Re: Creating underlined / centered text.

PostPosted: Thu May 23, 2013 5:55 am
by wreed12345
By that I mean the text that that above string returns or creates rather - where it says "and centered" could I set that to the value of a String apples = "apples"; or would I have to do this the java way.

Re: Creating underlined / centered text.

PostPosted: Thu May 23, 2013 8:01 am
by jason
You can just make a method out of the thing, using basic Java techniques:

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

public P paraWithFormattedText(String contents) {

      String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">"
               + "<w:pPr>"
                   + "<w:jc w:val=\"center\"/>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                   +"</w:rPr>"
               +"</w:pPr>"
               + "<w:r>"
                   + "<w:rPr>"
                       + "<w:u w:val=\"single\"/>"
                   +"</w:rPr>"
                   + "<w:t xml:space=\"preserve\">" + contents + "</w:t>"
               +"</w:r>"
           +"</w:p>";

return (P)XmlUtils.unmarshalString(openXML);

}
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: Creating underlined / centered text.

PostPosted: Thu May 23, 2013 1:10 pm
by wreed12345
I tried doing something like that but without the method just the string part. Then I got an error when it came to saving the document:(

Re: Creating underlined / centered text.

PostPosted: Thu May 23, 2013 2:05 pm
by jason
Well, you do need to take care to make sure your string is still valid WordML.

XML is very picky:
- it must be well-formed
- it must be valid according to the schema
- and then, in Word, there are additional app level requirements (ie just because it is valid and well-formed, doesn't mean Word will like it!)