by gkurady » Fri Mar 08, 2013 11:30 pm
I am trying to copy styles, theme, fonts from the template 'temp1.dotx' to docx 'Docx1.docx' . I have set the theme of the template to 'Apex' (The default theme is Office). This theme will have its own font Scheme and color scheme selected. When I had the deafult theme in the template, by just copying the style part from template to docx brought all the style definition so that when I open the docx file, I could see the style gallery exactly similar to that of template. In this case, when I try to copy style, theme, fontTable part form template to docx, the docx file becomes corrupt and word says there is some unreadable data and ask user to recover the doc. If I just copy the style definition, obviously font scemes, color schemes etc are not copied and style gallery of docx is dissimilar to that of template.
Another thing to note is that when a different theme is selected, you can not make style look exactly to that of template by attaching the template and applying the style in the Document Template AddIn dialog. You can do so only by trying to create a new document all together referring to the template.
The code goes as below:
import org.docx4j.XmlUtils;
import org.docx4j.dml.Theme;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.ThemePart;
import org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart;
import org.docx4j.wml.Fonts;
import org.docx4j.wml.Styles;
public class TemplateStyleToDocx {
/** copy/replace style from the template into docx using docx4J by replacing the style part, theme part */
public static void main(String[] args) throws Exception {
String inputfilepath = "D:\\Temp\\Templates\\temp1.dotx";
boolean save =
(inputfilepath == null ? false : true);
WordprocessingMLPackage wordMLPackage =
WordprocessingMLPackage.load(new java.io.File(inputfilepath));
// 2. Fetch the document part
MainDocumentPart tempDocPart = wordMLPackage.getMainDocumentPart();
StyleDefinitionsPart sdp = tempDocPart.getStyleDefinitionsPart();
Styles tempStyle = sdp.getJaxbElement();
//themes part
ThemePart tp = tempDocPart.getThemePart();
Theme tempTheme = tp.getJaxbElement();
//fontTable part
FontTablePart ftp = tempDocPart.getFontTablePart();
Fonts tempFont = ftp.getJaxbElement();
String marshaltoString = XmlUtils.marshaltoString(tempStyle, true);
System.out.println(marshaltoString);
System.out.println(tempStyle.toString());
String existingDoc = "D:\\Temp\\Templates\\Docx1.docx";
WordprocessingMLPackage existingPackage =
WordprocessingMLPackage.load(new java.io.File(existingDoc));
MainDocumentPart etempDocPart = existingPackage.getMainDocumentPart();
StyleDefinitionsPart esdp = etempDocPart.getStyleDefinitionsPart();
ThemePart etp = etempDocPart.getThemePart();
FontTablePart eftp = etempDocPart.getFontTablePart();
System.out.println("BEFORE SETTING....................");
esdp.setJaxbElement(tempStyle);
etp.setJaxbElement(tempTheme);
eftp.setJaxbElement(tempFont);
System.out.println("AFTER SETTING....................");
//save the existing doc
SaveToZipFile saver = new SaveToZipFile(existingPackage);
saver.save(existingDoc);
}
}
- Attachments
-
- Docx1.docx
- Document where template style definitions (with theme etc) needs to be copied
- (9.68 KiB) Downloaded 500 times
-
- temp1.dotx
- template from where we intend to copy the style definition.
- (199.96 KiB) Downloaded 450 times