source: trunk/docx4j/src/main/java/org/docx4j/samples/CompareDocuments.java @ 1654

Revision 1654, 5.0 KB checked in by jharrop, 9 months ago (diff)

save the comparison output as pdf

Line 
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
21package org.docx4j.samples;
22
23
24import java.io.OutputStream;
25import java.util.ArrayList;
26import java.util.Calendar;
27import java.util.List;
28
29import javax.xml.bind.JAXBContext;
30import javax.xml.bind.JAXBElement;
31import javax.xml.bind.JAXBException;
32import javax.xml.bind.Unmarshaller;
33
34import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
35import org.docx4j.diff.Differencer;
36import org.docx4j.fonts.IdentityPlusMapper;
37import org.docx4j.openpackaging.io.LoadFromZipFile;
38import org.docx4j.openpackaging.io.SaveToZipFile;
39import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
40import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
41import org.docx4j.openpackaging.parts.relationships.Namespaces;
42import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
43import org.docx4j.relationships.Relationship;
44import org.docx4j.wml.Body;
45import org.docx4j.wml.Document;
46import org.docx4j.wml.SdtContentBlock;
47
48
49/**
50 * This sample compares the 2 input documents, and renders
51 * the result using PDF viewer.
52 *
53 */
54public class CompareDocuments {
55       
56        public static JAXBContext context = org.docx4j.jaxb.Context.jc; 
57
58        /**
59         * @param args
60         */
61        public static void main(String[] args) throws Exception {
62
63                String newerfilepath = System.getProperty("user.dir") + "/102.docx";
64                String olderfilepath = System.getProperty("user.dir") + "/103.docx";
65                                               
66                // 1. Load the Packages
67                WordprocessingMLPackage newerPackage = WordprocessingMLPackage.load(new java.io.File(newerfilepath));
68                WordprocessingMLPackage olderPackage = WordprocessingMLPackage.load(new java.io.File(olderfilepath));
69               
70                Body newerBody = ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).getBody();
71                Body olderBody = ((Document)olderPackage.getMainDocumentPart().getJaxbElement()).getBody();
72               
73                System.out.println("Differencing..");
74               
75                // 2. Do the differencing
76                java.io.StringWriter sw = new java.io.StringWriter();
77                javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(
78                                sw);
79                Calendar changeDate = null;
80
81                Differencer pd = new Differencer();
82                pd.setRelsDiffIdentifier("blagh"); // not necessary in this case
83                pd.diff(newerBody, olderBody, result, "someone", changeDate,
84                                newerPackage.getMainDocumentPart().getRelationshipsPart(),
85                                olderPackage.getMainDocumentPart().getRelationshipsPart() 
86                                );
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                RelationshipsPart rp = newerPackage.getMainDocumentPart().getRelationshipsPart(); 
99                handleRels(pd, rp);                                             
100               
101               
102                newerPackage.setFontMapper(new IdentityPlusMapper());           
103                org.docx4j.convert.out.pdf.PdfConversion c
104                        = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(newerPackage);
105
106                OutputStream os = new java.io.FileOutputStream(System.getProperty("user.dir") +  "/COMPARED.pdf");                     
107                c.output(os, new PdfSettings() );
108                System.out.println("Saved " + System.getProperty("user.dir") +  "/COMPARED.pdf");
109                               
110        }
111
112        /**
113                 In the general case, you need to handle relationships.
114                 Although not necessary in this simple example,
115                 we do it anyway for the purposes of illustration.
116                 
117         */
118        private static void handleRels(Differencer pd, RelationshipsPart rp) {
119                // Since we are going to add rels appropriate to the docs being
120                // compared, for neatness and to avoid duplication
121                // (duplication of internal part names is fatal in Word,
122                //  and export xslt makes images internal, though it does avoid duplicating
123                //  a part ),
124                // remove any existing rels which point to images
125                List<Relationship> relsToRemove = new ArrayList<Relationship>();
126                for (Relationship r : rp.getRelationships().getRelationship() ) {
127                        //  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
128                        if (r.getType().equals(Namespaces.IMAGE)) {
129                                relsToRemove.add(r);
130                        }
131                }                                               
132                for (Relationship r : relsToRemove) {                           
133                        rp.removeRelationship(r);
134                }
135               
136                // Now add the rels we composed
137                List<Relationship> newRels = pd.getComposedRels();
138                for (Relationship nr : newRels) {                                                       
139                        rp.addRelationship(nr);
140                }
141        }
142       
143       
144               
145
146}
Note: See TracBrowser for help on using the repository browser.