/*
* Copyright 2007-2008, Plutext Pty Ltd.
*
* This file is part of docx4j.
docx4j is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cn.flightfeather.supervision.docx4j.simpleDemo;
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.contenttype.CTOverride;
import org.docx4j.openpackaging.contenttype.ContentTypeManager;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.samples.AbstractSample;
import org.docx4j.wml.CTRel;
import org.docx4j.wml.CTSettings;
import java.io.File;
import java.net.URI;
/**
* Creates a WordprocessingML document from the template (dotx file),
* and optionally, attaches that template (for example, instead of Normal.dot)
*
* Be sure to set String templatePath.
*
* In Flat OPC terms, aim is to produce:
*
*
*
* @author Jason Harrop
*/
public class TemplateAttach extends AbstractSample {
static boolean attachTemplate = true; // whether to set w:attachedTemplate
public static void main(String[] args) throws Exception {
String dotx = "C:\\Users\\jharrop\\AppData\\Roaming\\Microsoft\\Templates\\mytemplate.dotx";
String templatePath = "file:///" + dotx;
try {
getInputFilePath(args);
} catch (IllegalArgumentException e) {
inputfilepath = System.getProperty("user.dir") + "/OUT_TemplateAttach.docx";
}
boolean save =
(inputfilepath == null ? false : true);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(dotx));
// NB: clone here if your use case requires it
// Replace dotx content type with docx
ContentTypeManager ctm = wordMLPackage.getContentTypeManager();
// Get
CTOverride override = ctm.getOverrideContentType().get(new URI("/word/document.xml")); // note this assumption
if (dotx.endsWith("dotm")) // // macro enabled?
{
override.setContentType(org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
} else {
override.setContentType(org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT);
}
if (attachTemplate) {
// Create settings part, and init content
DocumentSettingsPart dsp = new DocumentSettingsPart();
CTSettings settings = Context.getWmlObjectFactory().createCTSettings();
dsp.setJaxbElement(settings);
wordMLPackage.getMainDocumentPart().addTargetPart(dsp);
// Create external rel
RelationshipsPart rp = RelationshipsPart.createRelationshipsPartForPart(dsp);
org.docx4j.relationships.Relationship rel = new org.docx4j.relationships.ObjectFactory().createRelationship();
rel.setType( Namespaces.ATTACHED_TEMPLATE );
rel.setTarget(templatePath);
rel.setTargetMode("External");
rp.addRelationship(rel); // addRelationship sets the rel's @Id
settings.setAttachedTemplate(
(CTRel)XmlUtils.unmarshalString("", Context.jc, CTRel.class)
);
// or (yuck)...
// CTRel id = new CTRel();
// id.setId( rel.getId() );
// JAXBElement je = new JAXBElement(
// new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "attachedTemplate"),
// CTRel.class, null, id);
// settings.setAttachedTemplate(je.getValue());
}
// Now save it
if (save) {
wordMLPackage.save(new File(inputfilepath) );
}
}
}