source: trunk/docx4j/src/main/java/org/docx4j/samples/Filter.java @ 869

Revision 869, 2.8 KB checked in by jharrop, 3 years ago (diff)

NamespacePrefixMappers? which work with Java 6 (ie if you don't have JAXB in your endorsed dir, or can't (eg Java Web Start)). The rest of it.

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
24
25import java.io.FileOutputStream;
26
27import javax.xml.bind.JAXBContext;
28import javax.xml.bind.Marshaller;
29
30import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
31import org.docx4j.jaxb.Context;
32import org.docx4j.jaxb.NamespacePrefixMapperUtils;
33import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
34import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
35
36
37public class Filter {
38
39        /**
40         * @param args
41         */
42        public static void main(String[] args) throws Exception {
43
44                String inputfilepath = System.getProperty("user.dir") + "/sample-docs/contentcontrols.docx";
45                               
46                // Do we want to save output?
47                boolean save = false;
48                // If so, whereto?
49                String outputfilepath = System.getProperty("user.dir") + "/sample-docs/qformat.pkg";           
50               
51               
52                // Open a document from the file system
53                WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
54               
55               
56                // Apply the filter
57                WordprocessingMLPackage.FilterSettings filterSettings = new WordprocessingMLPackage.FilterSettings();
58                filterSettings.setRemoveProofErrors(true);
59                filterSettings.setRemoveContentControls(true);
60                filterSettings.setRemoveRsids(true);
61                wmlPackage.filter(filterSettings);
62               
63                // Create a org.docx4j.wml.Package object
64                FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wmlPackage);
65                org.docx4j.xmlPackage.Package pkg = worker.get();
66       
67        // Now marshall it
68                JAXBContext jc = Context.jcXmlPackage;
69                Marshaller marshaller=jc.createMarshaller();
70               
71                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
72                NamespacePrefixMapperUtils.setProperty(marshaller, 
73                                NamespacePrefixMapperUtils.getPrefixMapper());                 
74               
75                //org.w3c.dom.Document doc = org.docx4j.XmlUtils.neww3cDomDocument();   
76                if (save) {
77                        marshaller.marshal(pkg, new FileOutputStream(outputfilepath));                         
78                        System.out.println( "\n\n .. written to " + outputfilepath);
79                } else {
80                        // Display its contents
81                        System.out.println( "\n\n OUTPUT " );
82                        System.out.println( "====== \n\n " );   
83                        marshaller.marshal(pkg, System.out);                           
84                }
85               
86               
87                               
88        }
89       
90       
91
92}
Note: See TracBrowser for help on using the repository browser.