| 1 | package org.docx4j.samples; |
|---|
| 2 | |
|---|
| 3 | import java.lang.reflect.InvocationTargetException; |
|---|
| 4 | import java.lang.reflect.Method; |
|---|
| 5 | import java.util.ArrayList; |
|---|
| 6 | import java.util.List; |
|---|
| 7 | |
|---|
| 8 | import org.docx4j.XmlUtils; |
|---|
| 9 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 10 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 11 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * This sample demonstrates how the MergeDocx utility can |
|---|
| 15 | * be used to convert a w:altChunk to normal docx |
|---|
| 16 | * content. |
|---|
| 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 | */ |
|---|
| 34 | public class AltChunkProcessingExtension extends AbstractSample { |
|---|
| 35 | |
|---|
| 36 | final static String BASE_DIR = System.getProperty("user.dir") + "/sample-docs/"; |
|---|
| 37 | |
|---|
| 38 | static String inputfilepath = BASE_DIR+"altChunk_docx.docx"; |
|---|
| 39 | |
|---|
| 40 | static boolean save = true; |
|---|
| 41 | static String outputfilepath = BASE_DIR+"altChunk_docx_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 | WordprocessingMLPackage srcPackage = WordprocessingMLPackage |
|---|
| 51 | .load(new java.io.File(inputfilepath)); |
|---|
| 52 | |
|---|
| 53 | try { |
|---|
| 54 | // Use reflection, so docx4j can be built |
|---|
| 55 | // by users who don't have the MergeDocx utility |
|---|
| 56 | Class<?> documentBuilder = Class.forName("com.plutext.merge.ProcessAltChunk"); |
|---|
| 57 | //Method method = documentBuilder.getMethod("merge", wmlPkgList.getClass()); |
|---|
| 58 | Method[] methods = documentBuilder.getMethods(); |
|---|
| 59 | Method method = null; |
|---|
| 60 | for (int j=0; j<methods.length; j++) { |
|---|
| 61 | System.out.println(methods[j].getName()); |
|---|
| 62 | if (methods[j].getName().equals("process")) { |
|---|
| 63 | method = methods[j]; |
|---|
| 64 | break; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | if (method==null) throw new NoSuchMethodException(); |
|---|
| 68 | |
|---|
| 69 | WordprocessingMLPackage resultPkg = (WordprocessingMLPackage)method.invoke(null, srcPackage); |
|---|
| 70 | |
|---|
| 71 | if (save) { |
|---|
| 72 | SaveToZipFile saver = new SaveToZipFile(resultPkg); |
|---|
| 73 | saver.save(outputfilepath); |
|---|
| 74 | System.out.println("Generated " + outputfilepath); |
|---|
| 75 | } else { |
|---|
| 76 | String result = XmlUtils.marshaltoString(resultPkg.getMainDocumentPart().getJaxbElement(), true, true); |
|---|
| 77 | System.out.println(result); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | } catch (SecurityException e) { |
|---|
| 81 | e.printStackTrace(); |
|---|
| 82 | } catch (ClassNotFoundException e) { |
|---|
| 83 | extensionMissing(e); |
|---|
| 84 | } catch (IllegalArgumentException e) { |
|---|
| 85 | e.printStackTrace(); |
|---|
| 86 | } catch (NoSuchMethodException e) { |
|---|
| 87 | extensionMissing(e); |
|---|
| 88 | } catch (IllegalAccessException e) { |
|---|
| 89 | e.printStackTrace(); |
|---|
| 90 | } catch (InvocationTargetException e) { |
|---|
| 91 | e.printStackTrace(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public static void extensionMissing(Exception e) { |
|---|
| 97 | System.out.println("\n" + e.getClass().getName() + ": " + e.getMessage() + "\n"); |
|---|
| 98 | System.out.println("* You don't appear to have the MergeDocx paid extension,"); |
|---|
| 99 | System.out.println("* which is necessary to merge docx, or process altChunk."); |
|---|
| 100 | System.out.println("* Purchases of this extension support the docx4j project."); |
|---|
| 101 | System.out.println("* Please visit www.plutext.com if you want to buy it."); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | |
|---|