Apply normal.dot formatting to documents

Hello,

I am trying to test how Aspose uses templates like normal.dot for its document manipulation. However, formatting defined in normal.dot seems to be ignored.

Is this behaviour by design or am I doing something wrong?

The attached normal.dotm has the Style “heading 1” explicitly defined as being in the color green. The other styles are system default blue etc.

When creating a new document, this color is ignored. All styles in normal.dot are removed and replaced by black default styles. I am currently unclear as to what effects “setAttachedTemplate” has on my document.

@Test
public void shouldCreateDocumentWithTemplateFormatting() throws Exception {

    Path tempDir = Files.createTempDirectory("shouldCreateDocumentWithTemplateFormatting");
    System.out.println(tempDir);

    Document document = new Document();
    // Use default normal.dot
    document.setAttachedTemplate("C:\Users\\AppData\Roaming\Microsoft\Templates\Normal.dotm");

    Paragraph paragraph = new Paragraph(document);
    paragraph.getParagraphFormat().setStyleName("Heading 1");

    Run run = new Run(document, "Meine Überschrift");
    paragraph.appendChild(run);
    document.getFirstSection().getBody().appendChild(paragraph);

    document.save(tempDir + File.separator + "out.docx");
}

Hi Alexander,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you perform the same scenario using MS Word, you will get the same output.

In your case, I suggest you please change the template name from Normal.dotm to some other name e.g NormalTempate.dotm and use Document.AutomaticallyUpdateSyles property as shown below. This will fix the shared issue.

Please let us know if you have any more queries.

Document document = new Document();
// Use default normal.dot
document.setAttachedTemplate(MyDir + "NormalTemplate.dotm");
Paragraph paragraph = new Paragraph(document);
paragraph.getParagraphFormat().setStyleName("Heading 1");
Run run = new Run(document, "Meine Überschrift");
paragraph.appendChild(run);
document.getFirstSection().getBody().appendChild(paragraph);
document.setAutomaticallyUpdateSyles(true);
document.save(MyDir + "Out.docx");

Hi Tahir,

thank you for your quick response.

This solution will only work if document is opened (and saved) by Word afterwards.
If the document is converted to PDF by Aspose before opening, no styling information is applied.
Is there a way around this?

document.setAutomaticallyUpdateSyles(true);
document.save(MyDir + "Out.docx"); // Has expected styling
document.save(MyDir + "Out.pdf"); // Has "wrong" styling

However, I understand now how applying a template to an existing document is probably not the preferred way to do things. Styling information is one thing, page layout etc. is another.
http://shaunakelly.com/word/templates/attachtemplate.html

If I want to create a new document from a .dotm template, I probably have to do this:

Document document = new Document(MyDir + "NormalTemplate.dotm");
document.save(MyDir + "Out.docx"); // Has expected styling

Or is there a difference to MS Word behavior of double clicking on a .dotm?

Thanks, Alexander

Hi Alexander,

Thanks for your
inquiry. Please note that Aspose.Words mimics the same behavior as MS
Word does. You are doing the followings:

  1. Open empty document
  2. Attach template
  3. Add paragraph with ‘Heading 1’ style.

Please repeat this process using MS Word with Normal.dotm and NormalTemplate.dotm. You will get the same output as Aspose.Words generates.

*a.bender:

This solution will only work if document is opened (and saved) by Word afterwards.
If the document is converted to PDF by Aspose before opening, no styling information is applied.
Is there a way around this?

document.setAutomaticallyUpdateSyles(true);
document.save(MyDir + “Out.docx”); // Has expected styling
document.save(MyDir + “Out.pdf”); // Has “wrong” styling*

You are getting the expected output in both documents. You are inserting the paragraph in an empty document. This document does not contain the ‘Heading 1’ style. You need to copy the same ‘Heading 1’ style from template document to new empty document using StyleCollection.addCopy method to get the required output.

*a.bender:

If I want to create a new document from a .dotm template, I probably have to do this:

Document document = new Document(MyDir + “NormalTemplate.dotm”);
document.save(MyDir + “Out.docx”); // Has expected styling*

Yes, you can also load the template document into Aspose.Words DOM. In this case, all styles from template document (Normal.dotm) will be loaded into Aspose.Words DOM.

In your scenario, I suggest you please load the template document (Normal.dotm) into Aspose.Words DOM instead of attaching template to document.

In case you are using older version of Aspose.Words, I suggest you please upgrade to the latest version of Aspose.Words for Java 15.8.0. Please let us know if you have any more queries.