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