Template File

Hello,
We have been using the aspose for quite long time and we are paid customer.Recently we are facing a small issue which i would like to highlight and get a solution for it.

1] We need need a feature to attach template file to the document below is the code being used.
sContent is having the transformed XML file

Dim aContentBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(sContent)
Dim oStream As New IO.MemoryStream()
oStream.Write(aContentBytes, 0, aContentBytes.Length)

oWordDocument = New Aspose.Words.Document(oStream, New Aspose.Words.LoadOptions(Aspose.Words.LoadFormat.Unknown)) ’ oWordApplication.Documents.Add(oTemplateFileName, oTypeMissing, oTypeMissing, oTypeMissing)
oStream.Close()
oWordDocument.AttachedTemplate = sTemplateFileName

but this doesnot take into the template file. Need quick solution on how to use it.

2] In word there is a feature of adding a file in particular location (using selection.range) how can that be done in Aspose.
Thanks & Regards
Ganesh K

Hi
Thanks for your request.

  1. What do you mean when say “this doesnot take into the template file”. AttachedTemplate property specifies template attached to the document, i.e. “base document”. If this property is not set Normal.dot template is used.
  2. If you need to insert or append one document to another, you can use the approaches suggested in the following articles:
    https://docs.aspose.com/words/java/insert-and-append-documents/
    https://docs.aspose.com/words/net/insert-and-append-documents/
    Best regards,

Hi,
Thanks for the quick reply, what i meant by that is when the name of the template file “master.dot” it doesn’t take it into consideration i.e master.dot has header information which has the logo of the company and the address. So i need to manually add this in every document separately so need a solution on how to add it.

Hope i was clear.

Thanks & Regards
Ganesh K

Hi
Thanks for your request. You can simply use the template document as a start document for your documents. If you generate documents programmatically, you should open the template and then insert content into this document.
Best regards,

Hi,
Thanks for the reply, i didn’t understand what u meant by “use the template document as a start document for your documents”. because i am generating the document at run time (given the piece of code in my first request). This template master.dot is predefined then at runtime use new document and add the contents which is an (wml) and then later on use AttachedTemplate to attach the template is there any other way to do this ? is there any property start document ? would be better if you could point me to a link.

With related to the insert will get back to you after trying it. Because the requirement is i will be placing a marker like {text01} then will be finding it and replacing it with another document at that location.

Thanks & Regards
Ganesh K

Hi
Thanks for your request. I mean you should open your template and then insert content into it. Please see the following pseudo-code:

// Open your tempalte
Document doc = new Document(@"yourtempalte.dot");
// Insert content into the document here.
// .......................................
// Save output.
doc.Save(@"out.doc");

hope this helps.
Best regards,

Hi,
Thanks for the code.
that is exactly what i was trying to do. but the code i am trying is as below.
it is in vb so,

dim Doc as DOcument = new Document("template.dot")
dim doc2 as document = new DOcument(oStream, ) 
doc.AppendDocument(doc2, keepsource)
doc.save("out.doc");

The output for this is in first page i get the template information of the header and in the next page i get the template information as header and the content which i added.

The output should be as Header (which is defined in the header template with page margins etc.) and then the content as body (which is in string form).

Hope i was clear.

Thanks & Regards
Ganesh K

Hi
Thanks for your inquiry. Let me clarify, documents are appended at the section level therefore the PageSetup.SectionStart property of the Section object defines how the content of the current section is joined in relation to the previous section. If the PageSetup.SectionStart property is set to SectionStart.NewPage for the first section in the source document then the content in this section is forced to start on a new page. Conversely if the property is set to SectionStart.Continuous then the content is allowed to flow on the same page directly after the previous section’s content. Each section in MS Word has its own Header/Footer.
In you case I think you should just set SectionStart to Continuous for source document.
Please see the following code:

Document dst = new Document("in1.doc");
Document src = new Document("in2.doc");
// Set SectionStart to Continuous
src.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
dst.Save("out.doc");

Best regards,

Hi,
Thanks for the replay, Tried your piece of code not working still the same output. i have put the code below. Let me know if i am doing anything wrong.

''Read content and store it in memory of aspose document
Dim oWordDocumentContent As Aspose.Words.Document = Nothing
Dim aContentBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(sContent)
Using oStream As New IO.MemoryStream()
oStream.Write(aContentBytes, 0, aContentBytes.Length)
oWordDocumentContent = New Aspose.Words.Document(oStream) ’ oWordApplication.Documents.Add(oTemplateFileName, oTypeMissing, oTypeMissing, oTypeMissing)
End Using

''Open content of template 
oWordDocument = New Aspose.Words.Document(sTemplateFileName) ’ oWordApplication.Documents.Add(oTemplateFileName, oTypeMissing, oTypeMissing, oTypeMissing)
‘’ Set SectionStart to Continuous
oWordDocumentContent.FirstSection.PageSetup.SectionStart = Aspose.Words.SectionStart.Continuous
oWordDocument.AppendDocument(oWordDocumentContent, Aspose.Words.ImportFormatMode.UseDestinationStyles)

Its in VB u can give me C# solution too no issue.

Thanks & Regards
Ganesh K

Hi,
I thought better will be an example which i am trying and not successful. I have attached a sample which i am experimenting hope u will get what i am trying to achieve. I have removed the Aspose word dll file just to reduce the rar file size.

Thanks & Regards
Ganesh K

Hello
Thank you for additional information. If you try comparing your documents, you will see that these documents have different page size. So to achieve what you need you should add the following line of code to your application:

oWordDocument = New Aspose.Words.Document(sTemplateFileName)
oWordDocumentContent.FirstSection.PageSetup.PageWidth = oWordDocument.FirstSection.PageSetup.PageWidth
oWordDocumentContent.FirstSection.PageSetup.PageHeight = oWordDocument.FirstSection.PageSetup.PageHeight
oWordDocumentContent.FirstSection.PageSetup.SectionStart = Aspose.Words.SectionStart.Continuous

Best regards,

Hi
Thanks a lot the solution worked.

Thanks & Regards
Ganesh K