source: trunk/docx4j/src/main/java/org/docx4j/samples/AltChunk.java @ 1503

Revision 1503, 3.0 KB checked in by jharrop, 12 months ago (diff)

Test altChunk in header.

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.FileInputStream;
25
26import org.docx4j.jaxb.Context;
27import org.docx4j.openpackaging.contenttype.ContentType;
28import org.docx4j.openpackaging.io.SaveToZipFile;
29import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
30import org.docx4j.openpackaging.parts.PartName;
31import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
32import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
33import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
34import org.docx4j.relationships.Relationship;
35import org.docx4j.wml.CTAltChunk;
36
37
38public class AltChunk {
39       
40        public static void main(String[] args) throws Exception {
41               
42                boolean ADD_TO_HEADER = true;
43                HeaderPart hp = null;
44               
45                String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.xml";
46               
47                String chunkPath = System.getProperty("user.dir") + "/sample-docs/word/chunk.docx";
48               
49                boolean save = true;
50                String outputfilepath = System.getProperty("user.dir") + "/altChunk_out.docx";
51               
52               
53                // Open a document from the file system
54                // 1. Load the Package
55                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
56                MainDocumentPart main = wordMLPackage.getMainDocumentPart();
57               
58                if (ADD_TO_HEADER) {
59                        hp = wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getDefaultHeader();
60                }
61               
62                AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/chunk.docx") );
63                afiPart.setBinaryData(
64                                new FileInputStream(chunkPath) );
65               
66                afiPart.setContentType(new ContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")); //docx
67                //afiPart.setContentType(new ContentType("application/xhtml+xml")); //xhtml
68
69                Relationship altChunkRel = null;
70                if (ADD_TO_HEADER) {
71                        altChunkRel = hp.addTargetPart(afiPart);
72                } else {
73                        altChunkRel = main.addTargetPart(afiPart);                     
74                }
75               
76                CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
77                ac.setId(altChunkRel.getId());
78
79                if (ADD_TO_HEADER) {
80                        hp.getJaxbElement().getEGBlockLevelElts().add(ac);
81                } else {
82                        main.addObject(ac);
83                }
84               
85                // Save it
86               
87                if (save) {             
88                        SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
89                        saver.save(outputfilepath);
90                        System.out.println("Saved " + outputfilepath);
91                }
92        }
93               
94
95}
Note: See TracBrowser for help on using the repository browser.