| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.samples; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import java.io.File; |
|---|
| 25 | import java.io.OutputStream; |
|---|
| 26 | import java.util.ArrayList; |
|---|
| 27 | import java.util.Calendar; |
|---|
| 28 | import java.util.List; |
|---|
| 29 | |
|---|
| 30 | import javax.xml.bind.JAXBContext; |
|---|
| 31 | import javax.xml.bind.JAXBElement; |
|---|
| 32 | import javax.xml.bind.JAXBException; |
|---|
| 33 | import javax.xml.bind.Unmarshaller; |
|---|
| 34 | |
|---|
| 35 | import org.docx4j.XmlUtils; |
|---|
| 36 | import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings; |
|---|
| 37 | import org.docx4j.diff.Differencer; |
|---|
| 38 | import org.docx4j.fonts.IdentityPlusMapper; |
|---|
| 39 | import org.docx4j.openpackaging.io.LoadFromZipFile; |
|---|
| 40 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 41 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 42 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 43 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 44 | import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; |
|---|
| 45 | import org.docx4j.relationships.Relationship; |
|---|
| 46 | import org.docx4j.wml.Body; |
|---|
| 47 | import org.docx4j.wml.Document; |
|---|
| 48 | import org.docx4j.wml.SdtContentBlock; |
|---|
| 49 | |
|---|
| 50 | import com.topologi.diffx.Docx4jDriver; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * This sample compares the 2 input documents, and renders |
|---|
| 55 | * the result using PDF viewer. |
|---|
| 56 | * |
|---|
| 57 | */ |
|---|
| 58 | public class CompareDocumentsUsingDriver { |
|---|
| 59 | |
|---|
| 60 | public static JAXBContext context = org.docx4j.jaxb.Context.jc; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * @param args |
|---|
| 64 | */ |
|---|
| 65 | public static void main(String[] args) throws Exception { |
|---|
| 66 | |
|---|
| 67 | String newerfilepath = System.getProperty("user.dir") + "/temp2/102.docx"; |
|---|
| 68 | String olderfilepath = System.getProperty("user.dir") + "/temp2/102Ver2.docx"; |
|---|
| 69 | |
|---|
| 70 | // 1. Load the Packages |
|---|
| 71 | WordprocessingMLPackage newerPackage = WordprocessingMLPackage.load(new java.io.File(newerfilepath)); |
|---|
| 72 | WordprocessingMLPackage olderPackage = WordprocessingMLPackage.load(new java.io.File(olderfilepath)); |
|---|
| 73 | |
|---|
| 74 | Body newerBody = ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).getBody(); |
|---|
| 75 | Body olderBody = ((Document)olderPackage.getMainDocumentPart().getJaxbElement()).getBody(); |
|---|
| 76 | |
|---|
| 77 | System.out.println("Differencing.."); |
|---|
| 78 | |
|---|
| 79 | // 2. Do the differencing |
|---|
| 80 | java.io.StringWriter sw = new java.io.StringWriter(); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | Docx4jDriver.diff( XmlUtils.marshaltoW3CDomDocument(newerBody).getDocumentElement(), |
|---|
| 84 | XmlUtils.marshaltoW3CDomDocument(olderBody).getDocumentElement(), |
|---|
| 85 | sw); |
|---|
| 86 | // The signature which takes Reader objects appears to be broken |
|---|
| 87 | |
|---|
| 88 | // 3. Get the result |
|---|
| 89 | String contentStr = sw.toString(); |
|---|
| 90 | System.out.println("Result: \n\n " + contentStr); |
|---|
| 91 | Body newBody = (Body) org.docx4j.XmlUtils |
|---|
| 92 | .unmarshalString(contentStr); |
|---|
| 93 | |
|---|
| 94 | // 4. Display the result as a PDF |
|---|
| 95 | // To do this, we'll replace the body in the newer document |
|---|
| 96 | ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).setBody(newBody); |
|---|
| 97 | |
|---|
| 98 | // In the general case, you need to handle relationships. Not done here! |
|---|
| 99 | |
|---|
| 100 | // RelationshipsPart rp = newerPackage.getMainDocumentPart().getRelationshipsPart(); |
|---|
| 101 | // handleRels(pd, rp); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | newerPackage.setFontMapper(new IdentityPlusMapper()); |
|---|
| 105 | org.docx4j.convert.out.pdf.PdfConversion c |
|---|
| 106 | = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(newerPackage); |
|---|
| 107 | |
|---|
| 108 | OutputStream os = new java.io.FileOutputStream(System.getProperty("user.dir") + "/COMPARED.pdf"); |
|---|
| 109 | c.output(os, new PdfSettings() ); |
|---|
| 110 | System.out.println("Saved " + System.getProperty("user.dir") + "/COMPARED.pdf"); |
|---|
| 111 | |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | In the general case, you need to handle relationships. |
|---|
| 116 | Although not necessary in this simple example, |
|---|
| 117 | we do it anyway for the purposes of illustration. |
|---|
| 118 | |
|---|
| 119 | */ |
|---|
| 120 | private static void handleRels(Differencer pd, RelationshipsPart rp) { |
|---|
| 121 | // Since we are going to add rels appropriate to the docs being |
|---|
| 122 | // compared, for neatness and to avoid duplication |
|---|
| 123 | // (duplication of internal part names is fatal in Word, |
|---|
| 124 | // and export xslt makes images internal, though it does avoid duplicating |
|---|
| 125 | // a part ), |
|---|
| 126 | // remove any existing rels which point to images |
|---|
| 127 | List<Relationship> relsToRemove = new ArrayList<Relationship>(); |
|---|
| 128 | for (Relationship r : rp.getRelationships().getRelationship() ) { |
|---|
| 129 | // Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" |
|---|
| 130 | if (r.getType().equals(Namespaces.IMAGE)) { |
|---|
| 131 | relsToRemove.add(r); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | for (Relationship r : relsToRemove) { |
|---|
| 135 | rp.removeRelationship(r); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | // Now add the rels we composed |
|---|
| 139 | List<Relationship> newRels = pd.getComposedRels(); |
|---|
| 140 | for (Relationship nr : newRels) { |
|---|
| 141 | rp.addRelationship(nr); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | } |
|---|