source: trunk/docx4j/src/main/java/org/docx4j/samples/ContentControlBindingExtensions.java @ 1702

Revision 1702, 4.6 KB checked in by jharrop, 7 months ago (diff)

Bug fix: honour no hyperlink creation.
Extend hyperlink creation to  https:// and mailto:
Add hyperlink style in whether processing pkg or part.

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
23import java.io.File;
24import java.io.FileInputStream;
25import java.util.List;
26import java.util.Map;
27
28import javax.xml.bind.JAXBContext;
29import javax.xml.bind.JAXBElement;
30import javax.xml.bind.JAXBException;
31import javax.xml.bind.Unmarshaller;
32
33import org.docx4j.XmlUtils;
34import org.docx4j.fonts.IdentityPlusMapper;
35import org.docx4j.jaxb.Context;
36import org.docx4j.model.datastorage.BindingHandler;
37import org.docx4j.model.datastorage.CustomXmlDataStorage;
38import org.docx4j.model.datastorage.CustomXmlDataStorageImpl;
39import org.docx4j.model.datastorage.OpenDoPEHandler;
40import org.docx4j.model.datastorage.OpenDoPEIntegrity;
41import org.docx4j.model.datastorage.RemovalHandler;
42import org.docx4j.model.datastorage.RemovalHandler.Quantifier;
43import org.docx4j.openpackaging.io.LoadFromZipFile;
44import org.docx4j.openpackaging.io.SaveToZipFile;
45import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
46import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
47import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
48import org.docx4j.wml.Body;
49
50
51/**
52 * This sample demonstrates populating content controls
53 * from a custom xml part (based on the xpaths given
54 * in the content controls).
55 *
56 * Word does this itself automatically, for if there is
57 * a w:databinding element in the sdtPr.
58 *
59 * However, out of the box, Word doesn't allow for
60 * repeating things (table rows, paragraphs etc), nor
61 * conditional inclusion/exclusion.
62 *
63 * The OpenDoPE conventions support that; and this sample
64 * demonstrates docx4j's implementation of that.
65 * 
66 */
67public class ContentControlBindingExtensions {
68       
69        public static JAXBContext context = org.docx4j.jaxb.Context.jc; 
70
71        /**
72         * @param args
73         */
74        public static void main(String[] args) throws Exception {
75
76//              String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/hyperlink-binding-test.docx";
77               
78                String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";
79               
80//              String inputfilepath = System.getProperty("user.dir") + "/src/test/resources/OpenDoPE/hyperlinks.docx";
81
82//              String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/CountryRegions.xml";
83
84//              String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/IT inventory.docx";
85               
86//              String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/word/databinding/MedicalChartSample.docx";
87               
88//              String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/repeat-containing-condition.docx";
89               
90                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));         
91               
92                String filepathprefix = inputfilepath.substring(0, inputfilepath.lastIndexOf("."));
93                System.out.println(filepathprefix);
94
95                // Process conditionals and repeats
96                OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
97                odh.preprocess();
98               
99                OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
100                odi.process(wordMLPackage);
101               
102                System.out.println(
103                                XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
104                                );             
105                SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
106                saver.save(filepathprefix + "_preprocessed.docx");
107                System.out.println("Saved: " + filepathprefix + "_preprocessed.docx");
108               
109                // Apply the bindings
110                BindingHandler.setHyperlinkStyle("Hyperlink");                                         
111                BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
112                System.out.println(
113                                XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
114                                );
115                saver.save(filepathprefix + "_bound.docx");
116                System.out.println("Saved: " + filepathprefix + "_bound.docx");
117               
118                // Strip content controls: you MUST do this
119                // if you are processing hyperlinks
120                RemovalHandler rh = new RemovalHandler();
121                rh.removeSDTs(wordMLPackage, Quantifier.ALL);
122                saver.save(filepathprefix + "_stripped.docx");
123                System.out.println("Saved: " + filepathprefix + "_stripped.docx");
124        }               
125                               
126
127}
Note: See TracBrowser for help on using the repository browser.