0

What I am wanting to do is take in a word doc/docx template which already has pre-designed headers and footers and replace certain words with words applicable with that document generated from what a user has input and has been saved through MySQL. I already have a program that works to get the user input and saves to the MySQL. However, I'm a little confused at how the word manipulation would work into this. I found docx4j and a tutorial that shows what I am looking for here and have found on another question on this site example code here. As I'm a beginner in using this, the things I'm confused on are:

  • I understand JAXB is used for converting to and from XML. Why is this relevant in a situation like this? Or if it's not, in what case would it be?
  • I am seeing two versions of loading:

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("P:\\Engineering\\Projects\\Naming\\EX_TEMP.docx"));
    

........ and the second example:

    private WordprocessingMLPackage getTemplate(String name) throws Docx4JException, FileNotFoundException {
    WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(new File(name)));
    return template;
}

(where would you put the file directory on the second code, or how can you specify the file you want to load?)

  • what does hyperlinkresolver do and why is it necessary? (second link)
  • what is applying binding in this situation? (second link)
  • what is the content accessor? (first link)
  • am I going about this the right way, or is there an easier/better way of doing this?

I am using Eclipse with Java on a Windows 7 if that helps. I would appreciate any help, thanks! Also if anyone has any examples with good comments or explanations, that would be helpful!

Sam
  • 7,252
  • 16
  • 46
  • 65
  • I'm only allowed to use two links above, but I wanted to include [this link](http://www.docx4java.org/docx4j/plutext-docx4j_on_a_page-v300.pdf) as a useful breakdown that makes some things a little simpler to understand, but doesn't really answer most of my questions. I just figured it could be useful if someone else is struggling to understand this stuff too. – user3482733 Mar 31 '14 at 21:23

1 Answers1

0

You probably ought to take a step back and decide which approach to injecting your data you want to take. Docx4j supports three approaches:

  1. replacing variables on the document surface (brittle but simple)
  2. mail merge (using MERGEFIELD), good for legacy documents
  3. content control data binding (your 2nd link; the modern/sophisticated/powerful approach, but you need to understand XML, and may be overkill here)

For answers to most of your specific questions, please take the time to read docx4j's Getting Started guide.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • Thank you Jason your beginners guide was helpful. I will be attempting to go with the first method of just replacing variables on the document surface provided I can get to the header and footer. One thing I am still stuck on is the JAXBElement. I understand that this is part of the zip that makes up the word doc, but I'm not understanding where it is defined. Every example I have tried to implement lacks an origin from what I could tell. Perhaps I was missing something. I read you need to use the get/setContents, but how? Thanks again! – user3482733 Apr 02 '14 at 00:13
  • JAXB has javax.xml.bind.JAXBElement which is used to wrap some objects to provide extra info about the corresponding Xml Element. In docx4j, each "Part" which has contents represented using JAXB has a field containing the root object for its content model which you can access via the getContents(). Unfortunately, we called that field jaxbElement, which might be a bit confusing. – JasonPlutext Apr 02 '14 at 01:25
  • getContents() gets you whatever object is tied to the part in question. So for the Main Document Part, its a org.docx4j.wml.Document object; for a header or footer it is something else. You then manipulate the Document object using its methods. – JasonPlutext Apr 02 '14 at 01:27