source: trunk/docx4j/src/main/java/org/docx4j/samples/ImportForeignPart.java @ 1302

Revision 1302, 2.9 KB checked in by jharrop, 19 months ago (diff)

Pass Relationship to newPartForContentType so AlternativeFormatInputPart? can be detected.

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;
25import java.io.InputStream;
26
27import org.docx4j.openpackaging.Base;
28import org.docx4j.openpackaging.contenttype.ContentTypeManager;
29import org.docx4j.openpackaging.io.Load;
30import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
31import org.docx4j.openpackaging.parts.Part;
32
33/**
34 * Import foreign parts
35 *
36 * @author Jason Harrop
37 * @version 1.0
38 */
39public class ImportForeignPart {
40
41        public static void main(String[] args) throws Exception {
42               
43                System.out.println( "Creating package..");
44                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
45               
46                // Need to know how what type of part to map to         
47                InputStream in = new FileInputStream("/home/dev/workspace/docx4j/foregin_parts/[Content_Types].xml");
48                ContentTypeManager externalCtm = new ContentTypeManager();
49                externalCtm.parseContentTypesFile(in);
50               
51                // Example of a part which become a rel of the word document
52                in = new FileInputStream("/home/dev/workspace/docx4j/foreign_parts/word/settings.xml");
53                attachForeignPart(wordMLPackage, wordMLPackage.getMainDocumentPart(),
54                                externalCtm, "word/settings.xml", in);
55
56                // Example of a part which become a rel of the package
57                in = new FileInputStream("/home/dev/workspace/docx4j/foreign_parts/docProps/app.xml");
58                attachForeignPart(wordMLPackage, wordMLPackage,
59                                externalCtm, "docProps/app.xml", in);
60               
61                // Now save it
62                wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/out.docx") );
63                               
64                System.out.println("Done.");
65                               
66        }
67       
68       
69        public static void attachForeignPart( WordprocessingMLPackage wordMLPackage, 
70                        Base attachmentPoint,
71                        ContentTypeManager foreignCtm, 
72                        String resolvedPartUri, InputStream is) throws Exception{
73               
74               
75                Part foreignPart = Load.getRawPart(is, foreignCtm,  resolvedPartUri, null);
76                        // the null means this won't work for an AlternativeFormatInputPart
77                attachmentPoint.addTargetPart(foreignPart);
78                // Add content type
79                ContentTypeManager packageCtm = wordMLPackage.getContentTypeManager();
80                packageCtm.addOverrideContentType(foreignPart.getPartName().getURI(), foreignPart.getContentType());
81               
82                System.out.println("Attached foreign part: " + resolvedPartUri);
83               
84        }
85       
86}
Note: See TracBrowser for help on using the repository browser.