Page 1 of 1

attaching a template

PostPosted: Thu Jan 31, 2013 11:34 pm
by gkurady
I want to attach a word template to document. I run this example in the sample 'TemplateAttach.java' and it successfully creates the document and associate the template with the document.

But the style, content, page settings etc are not brought to the document. I had header-footer/ watermark / background color set etc in the template which I wished to see in the document generated. but it didn't.
Can we achieve this in Docx4J? In word, when you are creating a new document based on a template all the contents, page settings, styles are inherited into the document. I am trying similar behavior in the docx4J.

Also, In word Templates and Add-Ins Dialog (screenshot attached) we can update the docuemnt style to that of attached template. Can we set this property using Docx4J apis programatically when we are attaching the template to the docuemnt?

-Thanks.

Re: attaching a template

PostPosted: Fri Feb 01, 2013 5:53 pm
by jason
You could do both the things you describe, but you'd have to write code yourself which mimics what Word is doing.

It would be easier to start your process with an empty docx which contains your header-footer/ watermark / background color set etc. Is there any reason you can't do that?

Re: attaching a template

PostPosted: Fri Feb 01, 2013 10:19 pm
by gkurady
You could do both the things you describe, but you'd have to write code yourself which mimics what Word is doing.
: Did you mean we do both using Docx4J? Can we achieve this without having to write own implementation of xml manipulations?

It would be easier to start your process with an empty docx which contains your header-footer/ watermark / background color set etc. Is there any reason you can't do that?
: I did not understand. My intension is to generate a docx inheriting header-footer/ watermark / background color from the template. Does Docx4J has the support for this?

One other requirement is to attach a template with an existing document.. but that is next step for what i am trying above (attaching template in an existing document).

Re: attaching a template

PostPosted: Mon Feb 04, 2013 6:02 am
by jason
Yes, using docx4j. I don't think Word is doing much more than replacing the styles part from the template, and getting its headers/footers and watermark, is it?

Another way to go would be to take the template dotx, and rename it to docx, and (if necessary), attach the template to this new docx.

Re: attaching a template

PostPosted: Mon Feb 04, 2013 11:37 pm
by gkurady
thanks.. I will give a try to achieve both my requirements using Docx4J apis.. Will update my findings and issues if any.

Re: attaching a template

PostPosted: Tue Feb 12, 2013 11:40 pm
by gkurady
I am trying to copy the styles from the template into my docx. It was pleasant to know that docx4J could read a dotx file and understand the StyleDefinitionsPart of it. I am just trying to copy the entire style from the template into my docx,.. so what am doing is:


//step1: store the template style

MainDocumentPart templateDocumentPart = wordMLPackage.getMainDocumentPart();
StyleDefinitionsPart sdp = documentPart.getStyleDefinitionsPart();
Styles templateStyle = sdp.getJaxbElement();

String marshaltoString = XmlUtils.marshaltoString(tempStyle, true);
System.out.println(marshaltoString);


//step2: existing document
String existingDoc = "D:\\Temp\\Templates\\Test2.docx";
WordprocessingMLPackage existingPackage =
WordprocessingMLPackage.load(new java.io.File(existingDoc));

MainDocumentPart eDocumentPart = wordMLPackage.getMainDocumentPart();
StyleDefinitionsPart esdp = eDocumentPart.getStyleDefinitionsPart();

//step3: set the template style into the existing docuemnt style part
esdp.setJaxbElement(tempStyle);

- It doesn't seem to set the template style into the document... Am I missing something?


Thanks.

Re: attaching a template

PostPosted: Wed Feb 13, 2013 8:05 am
by jason
That looks fine, except for the error in line 2, where documentPart.getStyleDefinitionsPart() should say templateDocumentPart.getStyleDefinitionsPart();

existingDoc needs to be saved, of course.

Re: attaching a template

PostPosted: Fri Mar 08, 2013 12:19 am
by gkurady
I have been able to apply the template style into docx as mentioned above. This works with the default theme. When I change the theme of the template and then try to copy the theme along with style using docx4J the resulting document seem to be corrupt and Microsoft word complains for having unreadable data. I tried copying fontsTable part too manually but to no avail. When we need to copy the themes (which has impact on the style definitions) and styles, is there any references anywhere inside the package which needs to be satisfied for successful copy?

Thanks.

Re: attaching a template

PostPosted: Fri Mar 08, 2013 5:11 am
by jason
To get help you'll need to post an input docx (sounds like a "hello world" docx, but with a different theme?), and code that can be copy/pasted into an IDE and executed.

Re: attaching a template

PostPosted: Fri Mar 08, 2013 11:30 pm
by gkurady
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);

}


}