Reading a sub template from memory stream

Hello,

In word template, I have a reference to a subtemplate from C drive
<<doc ["C:\\Aspose\\Sample\\SubTemplate\\Subtemplate.docx"] -build>>
But, I am not sure how I can reference the sub template from memory stream.

Regards,

@NaraSg

To reference a subtemplate from a memory stream in Aspose.Words, you can utilize the Document class to load the subtemplate directly from the memory stream. Below is an example demonstrating this approach:

using Aspose.Words;
using System.IO;

// Load your main template
Document mainTemplate = new Document("path_to_your_main_template.docx");

// Create a memory stream for the subtemplate
using (MemoryStream subTemplateStream = new MemoryStream(File.ReadAllBytes("path_to_your_subtemplate.docx")))
{
    // Load the subtemplate from the memory stream
    Document subTemplate = new Document(subTemplateStream);

    // Insert the subtemplate into the main template
    mainTemplate.AppendDocument(subTemplate, ImportFormatMode.KeepSourceFormatting);
}

// Save the final document
mainTemplate.Save("path_to_your_final_document.docx");

In this example, ensure to replace "path_to_your_main_template.docx" and "path_to_your_subtemplate.docx" with the actual paths to your templates. This method allows you to effectively use a subtemplate stored in memory without needing to reference it from the file system.

If you have any further questions or require additional assistance, please feel free to ask!

@NaraSg You can insert as document from stream. The expression must return a value of one of the following types:

  • A byte array containing document data
  • A Stream instance able to read document data
  • An instance of the Document class
  • A string containing a document URI, path, or Base64-encoded document data

Please see our documentation for more information:
https://docs.aspose.com/words/net/inserting-documents-dynamically/

Thank you for the update

1 Like