How to attach template to document using C#

Dear all,

I have problem to generate document from template

using (Stream stream = new MemoryStream(byteArrayTemplate))
{
//load the document in memory
Aspose.Words.Document doc = new Aspose.Words.Document(stream);
using (MemoryStream docStream = new MemoryStream())
{
//Save document to stream
doc.Save(docStream, Aspose.Words.SaveFormat.Docx);
byte[] docBytes = docStream.ToArray();


When I open the document generated I don’t have access to the building block , is there a method similar to VSTO to attach a template to a document

private Template AttachTemplateToDocument(string documentType, string documentLanguage, int entity, string idEntity)
{
// TODO modifier le code pour incorporer l’appel à documentum une fois disponible.
string documentLocation = Path.GetTempPath() + “\” + documentType + " - " + documentLanguage + " - " + entity + " - " + idEntity + “.dotx”;
if (!File.Exists(documentLocation))
{
using (FileStream fs = File.Create(documentLocation))
{
}
}
this.Document.set_AttachedTemplate(documentLocation);

Template template = (Template)this.Document.get_AttachedTemplate();
return template;
}

Hi Elisabeth,

Please accept my apologies for late response.

Thanks for your query. It would be great, If you share your template along with some more information for investigation purposes.

thank you for your help.

I attach an example of my template.

You can see some building block defined

Here the example of my code

Aspose.Words.Document doc = new Aspose.Words.Document(@" path of the template.dotx");
doc.Save(@“path of the word.docx”,Aspose.Words.SaveFormat.Docx);



what I see if you look at in attachment the picture: document_template.png the template is not attached

when I look at in the file generated , in the docx, there is no folder word_rels\settings.xml.rels

my question is: how do you create a docx from a dotx?

how do you attached a template?

how to preserve building block in the docx.

in attachment you can: pb template.zip

3 files:

  • template.dotx: the template I want to use
  • manually.docx the document generated manually when I do a right click on the template file, select New item.
in this file you can see I can select my building block, and when I click on the ruban in word, developer menu, document tempalte option the template.dotx is installed


  • word_aspose.docx the document generated by aspose

I hope to have a quick answer, the project should be already finish.
thanks in advance of your help


this post is closed

I’m ridiculous and lost a lot of time

doc.AttachedTemplate = “the path of template”

Hi Elisabeth,

Please accept my apologies for your inconvenience and misunderstanding this issue. Yes, you can get or set the full path of the template attached to the document.

Document doc = new Document(MyDir + "template.dotx");

doc.AttachedTemplate = MyDir + "template.dotx";

doc.Save(MyDir + "AsposeOut.docx");

Please let us know, If you have any more queries. We are really sorry for your inconvenience.