Attach Template Document using Java | AutomaticallyUpdateStyle

Hello :slight_smile:
The posted link is dead (attached template).
Any ideas what is the working one?

@iraklisd

You can use Document.AttachedTemplate property to set the full path of the template attached to the document. Please check the following API reference link.
Document.AttachedTemplate - Aspose.Words for Java - API Reference

Please let us know if you have any more queries.

Hello.
Yeah I went through your docs but what I do not understand is how can I apply a template to a new Word document? So, when you open Word it gets the template’s settings (like font style attributes etc) and applies them to the newly opened Word document.

How can I do that with Aspose (or Microsoft.Word.Interop for that matter)?
My understanding is this does not happen when you create a newWord document from code aka the default Normal.dotm template is not applied.

@iraklisd

You can use following code example to attach template document to Word document. Please make sure that the template document (“Normal.dotx”) should exist at correct path that you set in the code.

Document doc = new Document(MyDir + "input.docx");

// Attach a template, then set the flag to apply style changes
// within the template to styles in our document.
doc.setAttachedTemplate(MyDir + "Normal.dotx");
doc.setAutomaticallyUpdateStyles(true);

doc.save(MyDir + "output.docx"); 

If the value of Document.AttachedTemplate property is empty string, it means that the document is attached to the Normal template.

Great!

I was also looking at this one: Document | Aspose.Words for Java

I will look at the procided options and come back later if I have further questions on that (thanks for thaT).
One last question: If you open an existing document how can you apply the template to that document?
Do you do it the same way as when creating a new blank MS Word document?

@iraklisd

You do not need to attach any template document to existing document. Aspose.Words uses the default template for it.

Aspose.Words mimics the behavior of MS Word.

Please do not use Document.AttachedTemplate property if you want to use default template. However, if you want to apply styles from your own custom template document, you can use this property.

Hello again.

After further development I have some more questions:

Document doc = new Document(MyDir + "input.docx");

// Attach a template, then set the flag to apply style changes
// within the template to styles in our document.
doc.setAttachedTemplate(MyDir + "Normal.dotx");
doc.setAutomaticallyUpdateStyles(true);

doc.save(MyDir + "output.docx"); 

I did this and it does not work. However if I do this:

public static string MsWordUserDefaultTemplate { get; private set; } = (
    Environment.GetFolderPath(
        Environment.SpecialFolder.ApplicationData
    ) + string.Join(
        Path.DirectorySeparatorChar.ToString(),
        new string[] { Path.DirectorySeparatorChar.ToString(), "Microsoft", "Templates", "Normal_alt.dotm" }
    )
);

// Main
var srcDoc = new Document(@"..\..\..\source.doc");
srcDoc.AttachedTemplate = MsWordUserDefaultTemplate;
srcDoc.AutomaticallyUpdateStyles = true;
DocumentBuilder srcDocBuilder = new DocumentBuilder(srcDoc);
DocumentBuilder temDocBuilder;
Document temDocument;
if (srcDoc.AttachedTemplate == "")
    temDocument = new Document(MsWordUserDefaultTemplate);
else
    temDocument = new Document(srcDoc.AttachedTemplate);
temDocBuilder = new DocumentBuilder(temDocument);

srcDocBuilder.ParagraphFormat.StyleIdentifier = temDocBuilder.ParagraphFormat.StyleIdentifier;
srcDocBuilder.ParagraphFormat.Alignment = temDocBuilder.ParagraphFormat.Alignment;

This applies the style religiously.

What am I missing here? What exactly does is the functionality of the AutomaticallyUpdateStyles? Essentially, how templates do really work? I was thinking that when you attached a template and set the AutomaticallyUpdateStyles to true that this would apply retroactively to any new MS Word document you opened and saved with Aspose as this is whatr MS Word does by default. However, that is not the case unless you go the way I stated by explicitly assigning style settings to the MS Word document.

In addition, trying to apply other formatting elements directly on the document based on a template, like Font name and size do not work. So if I do something like this:

var srcDoc = new Document(@"..\..\..\source.doc");
srcDoc.AttachedTemplate = MsWordUserDefaultTemplate;
srcDoc.AutomaticallyUpdateStyles = true;
DocumentBuilder srcDocBuilder = new DocumentBuilder(srcDoc);
DocumentBuilder temDocBuilder;
Document temDocument;
if (srcDoc.AttachedTemplate == "")
    temDocument = new Document(MsWordUserDefaultTemplate);
else
    temDocument = new Document(srcDoc.AttachedTemplate);
temDocBuilder = new DocumentBuilder(temDocument);

// These work
srcDocBuilder.ParagraphFormat.StyleIdentifier = temDocBuilder.ParagraphFormat.StyleIdentifier;
srcDocBuilder.ParagraphFormat.Alignment = temDocBuilder.ParagraphFormat.Alignment;

// These below do not work but it was suggested in another thread
srcDocBuilder.Document.Styles[srcDocBuilder.ParagraphFormat.StyleIdentifier].Font.Name = temDocBuilder.Font.Name;
srcDocBuilder.Document.Styles[srcDocBuilder.ParagraphFormat.StyleIdentifier].Font.Size = temDocBuilder.Font.Size;
srcDocBuilder.Document.Styles[srcDocBuilder.ParagraphFormat.StyleIdentifier].Font.Bold = temDocBuilder.Font.Bold;
srcDocBuilder.Document.Styles[srcDocBuilder.ParagraphFormat.StyleIdentifier].Font.Italic = temDocBuilder.Font.Italic;
srcDocBuilder.Document.Styles[srcDocBuilder.ParagraphFormat.StyleIdentifier].Font.Underline = temDocBuilder.Font.Underline;
srcDocBuilder.Document.Styles[srcDocBuilder.ParagraphFormat.StyleIdentifier].Font.StrikeThrough = temDocBuilder.Font.StrikeThrough;

srcDocBuilder.Document.Save(@"..\..\..\source2.doc");

It fails to work.

I tried other stuff too like directly changing the font of the source document to match the font of the template document but it did not work. Anything else I tried seems either not to work or work unreliably.

@iraklisd

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word and template documents.
  • Please share the screenshots of problematic sections of output document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

A post was split to a new topic: Attach template to Word document

A post was split to a new topic: AttachedTemplate in Aspose Java does not work with VBA