Page 1 of 1

Bullets

PostPosted: Mon Nov 29, 2010 6:20 pm
by GrandPoohBear
Hi all!

First off, I'm really happy to have found this project. It seems to be exactly what I need for my work. That being said, I'm having some trouble getting started.

I need to create a docx file from scratch that contains bulleted lists and endnotes. I can see from the files I've exported from Word that I'll need to make a Numbering part and an Endnotes part, and I've partially done some of this in code, but I keep hitting walls. Are there any simple examples that would show how to do something like this?

Many thanks!
--Andy

Re: Bullets

PostPosted: Mon Nov 29, 2010 9:02 pm
by jason
There's basically 3 approaches you can take to create your numbering part:

1. build the JAXB objects one by one using the object factory. this can be a bit tedious, but for the contents of the numbering part, at least some of it is repetitive, which makes it easier.

2. make a String containing the content, and unmarshall it

3. read the part in from a file on disk - see the ImportForeignPart sample. This might be the neatest way for you.

The first 2 approaches are documented in the Getting Started guide.

Feel free to post again once you have zeroed in on one of the above approaches.

cheers .. Jason

Re: Bullets

PostPosted: Tue Nov 30, 2010 7:13 pm
by GrandPoohBear
Hey Jason,

Thanks for the fast reply! I've had a bit of success creating the document part using JAXB objects, and then just unmarshalling the numbering part from a sample I had created in Word (actually, I tried creating the numbering part by hand first, but like you said, it was getting a bit tedious, and it didn't seem I could ignore the bits that I thought were unimportant :P ). As I'm going through this exercise, the structure of the whole thing is making a little more sense, though it just seems massive! I'll post my code below - please let me know if you have any suggestions for making things easier. I did have some larger questions though:

1 - I'm not quite sure when to use the WMLObjectFactory to create objects or when to do so via their constructor. Some don't seem to have a creation method, or at least I'm not seeing it in the list. Is there a rule of thumb as to which are done that way and which aren't?

2 - You mentioned the OpenXML spec at documentinteropinitiative.org in your other post. I may not be looking at the right thing, but I'm finding what I get through that site extremely difficult to read and use. I'm sure most of that is par for the course. Are there any guides to the format that you know of that are more grokkable?

Here's my basic bullet code:
Code: Select all
public BasicTest() throws Docx4JException, JAXBException {
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .createPackage();

      ObjectFactory factory = Context.getWmlObjectFactory();

      NumberingDefinitionsPart numPart = new NumberingDefinitionsPart();
      wordMLPackage.getMainDocumentPart().addTargetPart(numPart);

      P para = factory.createP();
      Text t = factory.createText();
      t.setValue("This should be a bullet");
      R run = factory.createR();
      run.getRunContent().add(t);
      para.getParagraphContent().add(run);

      PPr paraProps = factory.createPPr();
      para.setPPr(paraProps);

      NumPr paraNum = new NumPr();
      paraProps.setNumPr(paraNum);

      Ilvl paraNumIlvl = new Ilvl();
      paraNum.setIlvl(paraNumIlvl);
      paraNumIlvl.setVal(BigInteger.ZERO);

      NumId paraNumId = new NumId();
      paraNum.setNumId(paraNumId);
      paraNumId.setVal(BigInteger.ONE);

      Body body = wordMLPackage.getMainDocumentPart().getJaxbElement()
            .getBody();
      body.getEGBlockLevelElts().add(para);

      String numberingString = "<insert numbering section from word sample here>";
      numPart.setJaxbElement((Numbering) XmlUtils
            .unmarshalString(numberingString));

      wordMLPackage.save(new File("BasicTestOutput.docx"));
}


... now to just combine this with the endnote sample you gave me in my other post and I'm set!

Cheers,
--Andy

Re: Bullets

PostPosted: Tue Nov 30, 2010 9:52 pm
by jason
GrandPoohBear wrote:1 - I'm not quite sure when to use the WMLObjectFactory to create objects or when to do so via their constructor. Some don't seem to have a creation method, or at least I'm not seeing it in the list. Is there a rule of thumb as to which are done that way and which aren't?


This is a JAXB general question, which you can find discussed a little on the web, but the basic summary is that you should use the WML ObjectFactory. If you know what you are doing, you can use a constructor directly, but using the WML ObjectFactory is the safer approach. The reason for this is that in some cases, what you actually need is a JAXBElement: have a look at the source code for the ObjectFactory class and you will see what I mean.

GrandPoohBear wrote:2 - You mentioned the OpenXML spec at documentinteropinitiative.org in your other post. I may not be looking at the right thing, but I'm finding what I get through that site extremely difficult to read and use. I'm sure most of that is par for the course. Are there any guides to the format that you know of that are more grokkable?


The dii site I find convenient when accessed via the search interface I provide to it; for direct access to some particular section. Its not good for browsing, I agree. My Getting Started guide contains pointers to more convenient formats:

You can find a very readable introduction in 1st edition Part 3 (Primer) at http://www.ecma-international.org/publi ... ma-376.htm or http://www.ecma-international.org/news/ ... e_docs.htm (a better link for the 1st edition (Dec 2006), since its not zipped up).


hth .. Jason