We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Changing Aspose default template it uses to create document

Hi,


I want to change the the template Aspose uses to create any document and change the default styles in this template . For eg. through Microsoft word we can change ‘’ Normal.dotm" to change the document template . Similar thing I want to achieve through Aspose, where I can also have the ability to change the default styles in the document.

Is there any way to achieve this?
Please provide the solution.

Thanks
Shubhangi


Hi Shubhangi,

Thanks for your inquiry. Please use Document.AttachedTemplate property to get or set the full path of the template attached to the document. If you are creating document from scratch, please load your template document into Aspose.Words DOM.

Please let us know if you have any more queries.

Hi,


I tried attaching a template, where in the template I have few heading with different heading styles, template document also has few styles corresponding to StyleIdentifier.TOC1, StyleIdentifier.TOC2, StyleIdentifier.TOC3. Somehow these styles are not applied to the document.
I am using the following code.

Document doc1 = new Document(getFilePath(TEST_DATA_DIR, “test.docx”));
doc1.setAttachedTemplate(getFilePath(TEST_DATA_DIR, “TocWithStyles.docx”));

DocumentBuilder builder = new DocumentBuilder(doc1);
builder.insertTableOfContents("\o “1-3” \h \z \u");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

builder.writeln(“Heading 1”);
builder.writeln(“Heading 1.1”);

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

builder.writeln(“Heading 2”);
builder.writeln(“Heading 2.1”);
doc1.updateFields();

doc1.save(getFilePath(TEST_DATA_DIR, “headingInserted.docx”));


I am also attaching the
sample template : TocWithStyles.docx
document in which style to be applied: test.docx

Could you provide me the correct code if I am doing it wrong.

Thanks
Shubhangi

Hi Shubhangi,


Thanks for your inquiry. Please use Document.AutomaticallyUpdateSyles property as shown below to get the desired output. This property is used to get or set a flag indicating whether the styles in the document are updated to match the styles in the attached template each time the document is opened in MS Word.

<span style=“font-size: 9pt; font-family: “Courier New”; background: rgb(228, 228, 255);”>Document<span style=“font-size: 9pt; font-family: “Courier New”;”> doc1 = <span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:navy;mso-bidi-language:HE”>new
<span style=“font-size: 9pt; font-family: “Courier New”; background: rgb(228, 228, 255);”>Document<span style=“font-size: 9pt; font-family: “Courier New”;”>(<span style=“font-size:9.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:#660E7A;
mso-bidi-language:HE”>MyDir
<span style=“font-size: 9pt; font-family: “Courier New”;”>+ <span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>“test.docx”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);

doc1.setAttachedTemplate(<span style=“font-size:9.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:#660E7A;
mso-bidi-language:HE”>MyDir
<span style=“font-size: 9pt; font-family: “Courier New”;”>+ <span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>“TocWithStyles.docx”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);



DocumentBuilder builder = <span style=“font-size:9.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:navy;mso-bidi-language:
HE”>new
<span style=“font-size: 9pt; font-family: “Courier New”;”>DocumentBuilder(doc1);



builder.insertTableOfContents(<span style=“font-size:9.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:green;
mso-bidi-language:HE”>"
<span style=“font-size:9.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:navy;
mso-bidi-language:HE”>\
<span style=“font-size:9.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:
HE”>o
<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:navy;mso-bidi-language:HE”>"
<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:green;mso-bidi-language:HE”>1-3
<span style=“font-size:9.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:navy;
mso-bidi-language:HE”>" \
<span style=“font-size:9.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:green;
mso-bidi-language:HE”>h
<span style=“font-size:9.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:navy;mso-bidi-language:
HE”>\
<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>z
<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:navy;mso-bidi-language:HE”>\
<span style=“font-size:9.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:green;
mso-bidi-language:HE”>u"
<span style=“font-size: 9pt; font-family: “Courier New”;”>);

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;mso-bidi-language:HE”>HEADING_1
<span style=“font-size: 9pt; font-family: “Courier New”;”>);



builder.writeln(<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>“Heading
1”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);

builder.writeln(<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>“Heading
1.1”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);



builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#660E7A;mso-bidi-language:HE”>HEADING_2
<span style=“font-size: 9pt; font-family: “Courier New”;”>);



builder.writeln(<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>“Heading
2”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);

builder.writeln(<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:green;mso-bidi-language:HE”>“Heading
2.1”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);



doc1.updateFields();

doc1.setAutomaticallyUpdateSyles(<span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:navy;background:yellow;mso-highlight:yellow;mso-bidi-language:HE”>true
<span style=“font-size: 9pt; font-family: “Courier New”; background: yellow;”>);<span style=“font-size: 9pt; font-family: “Courier New”;”>

doc1.save(<span style=“font-size:9.0pt;font-family:“Courier New”;
mso-fareast-font-family:“Times New Roman”;color:#660E7A;mso-bidi-language:HE”>MyDir
<span style=“font-size: 9pt; font-family: “Courier New”;”>+ <span style=“font-size:9.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:green;mso-bidi-language:HE”>“headingInserted.docx”
<span style=“font-size: 9pt; font-family: “Courier New”;”>);<o:p></o:p>