source: trunk/docx4j/src/main/java/org/docx4j/samples/MergeDocx.java @ 1343

Revision 1343, 3.8 KB checked in by jharrop, 18 months ago (diff)

Update spiel on how to acquire the extension.

Line 
1package org.docx4j.samples;
2
3import java.lang.reflect.InvocationTargetException;
4import java.lang.reflect.Method;
5import java.util.ArrayList;
6import java.util.List;
7
8import org.docx4j.XmlUtils;
9import org.docx4j.openpackaging.exceptions.Docx4JException;
10import org.docx4j.openpackaging.io.SaveToZipFile;
11import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
12
13/**
14 * This sample demonstrates how the MergeDocx utility can
15 * be used to merge docx documents. It is one of two such
16 * samples, the other being AltChunkProcessingExtension.
17 *
18 * The MergeDocx utility is a paid extension to docx4j.
19 * Purchases of this extension support the docx4j project.
20 * @see <a href="http://dev.plutext.org/blog/2010/11/merging-word-documents/">
21 * merging-word-documents blog post</a> for more info, or
22 * @see <a href="http://www.plutext.com/">www.plutext.com</a>
23 * or email sales@plutext.com if you want to buy it.
24 *
25 * To run the utility, you simply pass it a list of the
26 * docx you want to merge; it returns a new pkg containing
27 * the merged documents.
28 *
29 * This example looks a little more complex, since it
30 * has to use reflection, so that docx4j can still be
31 * built by users who don't have the MergeDocx code.
32 *
33 */
34public class MergeDocx extends AbstractSample {
35
36        final static String BASE_DIR = System.getProperty("user.dir") + "/sample-docs/";
37
38        final static String[] sourceDocxNames = { "Table.docx", "Images.docx"}; 
39
40        static boolean save = true;
41        static String outputfilepath = BASE_DIR+"MergeDocx_OUT.docx";           
42       
43        /**
44         * @param args
45         * @throws Docx4JException
46         */
47        public static void main(String[] args) throws Docx4JException {
48               
49                // Create list of docx packages to merge
50                List<WordprocessingMLPackage> wmlPkgList=new ArrayList<WordprocessingMLPackage>();
51                for (int i=0; i<sourceDocxNames.length; i++){
52                        String filename = BASE_DIR + sourceDocxNames[i] ;
53                        System.out.println("Loading " + filename); 
54                        wmlPkgList.add(WordprocessingMLPackage
55                                        .load(new java.io.File(filename)));
56                }
57               
58                try {
59                        // Use reflection, so docx4j can be built
60                        // by users who don't have the MergeDocx utility
61                        Class<?> documentBuilder = Class.forName("com.plutext.merge.DocumentBuilder");                 
62                        //Method method = documentBuilder.getMethod("merge", wmlPkgList.getClass());                   
63                        Method[] methods = documentBuilder.getMethods(); 
64                        Method method = null;
65                        for (int j=0; j<methods.length; j++) {
66                                System.out.println(methods[j].getName());
67                                if (methods[j].getName().equals("merge")) {
68                                        method = methods[j];
69                                        break;
70                                }
71                        }                       
72                        if (method==null) throw new NoSuchMethodException();
73                       
74                        WordprocessingMLPackage resultPkg = (WordprocessingMLPackage)method.invoke(null, wmlPkgList);
75
76                        if (save) {             
77                                SaveToZipFile saver = new SaveToZipFile(resultPkg);
78                                saver.save(outputfilepath);
79                                System.out.println("Generated " + outputfilepath);
80                        } else {
81                                String result = XmlUtils.marshaltoString(resultPkg.getMainDocumentPart().getJaxbElement(), true, true);
82                                System.out.println(result);                             
83                        }
84                       
85                } catch (SecurityException e) {
86                        e.printStackTrace();
87                } catch (ClassNotFoundException e) {
88                        extensionMissing(e);
89                } catch (IllegalArgumentException e) {
90                        e.printStackTrace();
91                } catch (NoSuchMethodException e) {
92                        extensionMissing(e);
93                } catch (IllegalAccessException e) {
94                        e.printStackTrace();
95                } catch (InvocationTargetException e) {
96                        e.printStackTrace();
97                } 
98
99        }
100       
101        public static void extensionMissing(Exception e) {
102                System.out.println("\n" + e.getClass().getName() + ": " + e.getMessage() + "\n");
103                System.out.println("* You don't appear to have the MergeDocx paid extension,");
104                System.out.println("* which is necessary to merge docx, or process altChunk.");
105                System.out.println("* Purchases of this extension support the docx4j project.");
106                System.out.println("* Please visit www.plutext.com if you want to buy it.");
107        }
108
109}
Note: See TracBrowser for help on using the repository browser.