source: trunk/docx4j/src/main/java/org/docx4j/samples/CopyPart.java @ 1004

Revision 1004, 3.3 KB checked in by jharrop, 2 years ago (diff)

Remove dom4j stuff

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
21
22package org.docx4j.samples;
23
24import java.io.FileInputStream;
25
26import javax.xml.bind.JAXBContext;
27import javax.xml.bind.JAXBElement;
28import javax.xml.bind.Unmarshaller;
29
30import org.docx4j.jaxb.Context;
31import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
32import org.docx4j.openpackaging.parts.Part;
33import org.docx4j.openpackaging.parts.PartName;
34import org.docx4j.openpackaging.parts.relationships.Namespaces;
35import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
36import org.docx4j.relationships.Relationship;
37
38/**
39 * Import foreign parts
40 *
41 * @author Jason Harrop
42 * @version 1.0
43 */
44public class CopyPart {
45
46        public static void main(String[] args) throws Exception {
47               
48                // Source package               
49                String inputfilepath = System.getProperty("user.dir") + "/sample-docs/embedded-spreadsheet.docx";
50                WordprocessingMLPackage wordMLPackage1;// = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
51                if (inputfilepath.endsWith(".xml")) {
52                        // You can create one of these in Word 2007, by
53                        // choosing Save As .xml
54                        // These are easier to look at / edit in a text editor than a zipped up docx
55                        JAXBContext jc = Context.jcXmlPackage;
56                        Unmarshaller u = jc.createUnmarshaller();
57                        u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
58
59                        org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package)((JAXBElement)u.unmarshal(
60                                        new javax.xml.transform.stream.StreamSource(new FileInputStream(inputfilepath)))).getValue(); 
61
62                        org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter( wmlPackageEl); 
63
64                        wordMLPackage1 = (WordprocessingMLPackage)xmlPackage.get(); 
65               
66                } else {
67                        // Its just a docx
68                        wordMLPackage1 = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
69                }
70               
71                // Destination
72                System.out.println( "Creating package..");
73                WordprocessingMLPackage wordMLPackage2 = WordprocessingMLPackage.createPackage();
74               
75                // Find the part we want to copy
76                RelationshipsPart rp = wordMLPackage1.getMainDocumentPart().getRelationshipsPart();
77                Relationship rel = rp.getRelationshipByType(Namespaces.EMBEDDED_PKG);           
78                Part p = rp.getPart(rel);
79               
80                System.out.println(p.getPartName().getName() );
81               
82                p.setPartName( new PartName("/word/embeddings/MySpreadsheet.xlsx") );
83               
84//              System.out.println(p.getContentType() );
85//              System.out.println(p.getRelationshipType() );
86               
87                // Now try adding it
88                wordMLPackage2.getMainDocumentPart().addTargetPart(p);
89                System.out.println("Done" );
90               
91//              p.setOwningRelationshipPart(owningRelationshipPart)
92//              p.setPackage(pack)
93               
94                               
95        }
96       
97}
Note: See TracBrowser for help on using the repository browser.