0

Attempting to write my first class with docx4j (http://www.docx4java.org). Basically the idea is to find a string of text in the .docx file and replace it with another string of text. Essentially a mail merge. While I'm not receiving any errors, the merged document itself is not being saved in the path I've suggested. This makes me think it's a file path problem but I don't see anything wrong with it.

package efi.mailmerge.servlets;

import java.util.List;
import javax.xml.bind.JAXBElement;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.Text;

public class WordDocTest {

    /**
     * Open word document /Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample.docx, replace a piece of text and save
     * the result to /Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample-Out.docx.
     *
     * The text <<CUS_FNAME>> will be replaced with John.
     *
     * @param args
     */
    public static void main(String[] args) {

        // Text nodes begin with w:t in the word document
        final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";

        try {
            // Open the input file
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("/Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample.docx"));

            // Build a list of "text" elements
            List texts = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);

            // Loop through all "text" elements
            for (Object obj : texts) {
                Text text = (Text) ((JAXBElement) obj).getValue();

                // Get the text value
                String textValueBefore = text.getValue();

                // Perform the replacement
                String textValueAfter = textValueBefore.replaceAll("<<CUS_FNAME>>", "John");

                // Show the element before and after the replacement
                System.out.println("textValueBefore = " + textValueBefore);
                System.out.println("textValueAfter = " + textValueAfter);

                // Update the text element now that we have performed the replacement
                text.setValue(textValueAfter);

            }

            wordMLPackage.save(new java.io.File("/Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample-Out.docx"));

        } catch (Docx4JException e) {
            Logger.getLogger(WordDocTest.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        } catch (Exception e) {
            Logger.getLogger(WordDocTest.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }
    }

}

On lines 26 and 50 you can see the input/output paths. I've confirmed that the Sample.docx input file does exist and that the uploads directory has write permissions. Can you see anything wrong with my file paths here? I could be completely on the wrong path, but this is all very new to me so I'm learning as I go.

Any and all help is very much appreciated.

Jeff Walden
  • 311
  • 1
  • 4
  • 19
  • Maybe you are getting an exception, but not seeing it. Try adding System.out.println(e.getMessage()). printStackTrace() writes to the standard error stream. – JasonPlutext Mar 27 '13 at 22:01
  • Are you running this from the command line? From an IDE? or a servlet environment? – JasonPlutext Mar 27 '13 at 22:03

1 Answers1

0

At first sight, I would suggest trying with your path written the following way :

wordMLPackage.save(new java.io.File("\\Users\\Jeff\\Development\\ReServe-Unleashed\\Dev\\MailMerge\\uploads\\Sample-Out.docx"));

If it still not works, please provide the stack traces ? It could help. (if no doc is saved, there must be an exception thrown)

PhilippeAuriach
  • 2,418
  • 2
  • 22
  • 41