#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Docx4j Test - Probably a File Path Problem


    Hi there guys. 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.

    Code:
    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) {
            } catch (Exception e) {
    
            }
        }
    
    }
    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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    You are catching the exceptions; presumably there is one.

    Add e.printStackTrace() to see what the problem is.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Makes sense, catch the exceptions to see the errors. I've changed lines 54-60 to read:

    Code:
    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();
            }
    Still not seeing anything printed to the browser screen nor am I seeing anything in the Tomcat log. The Tomcat log level is set at ALL. Am I catching the exceptions incorrectly?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    OK, so there is no exception.

    Great. I'm sure the file will be where you wrote it.

    How are you checking? Using 'ls'? What is your OS.

    btw, this discussion would be better at the docx4j forums, or StackOverflow. At least if you want me to reply further :-)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Thanks for your help, plutext. I've moved my question to Stackoverflow. The link is below in-case anybody else is following this question is looking for the solution.

    http://stackoverflow.com/questions/1...file-is-output

IMN logo majestic logo threadwatch logo seochat tools logo