We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Insert a .docx or doc file into another docuemnt file

Hi,

Is there any provision to insert a document file into another document file. Actually their is a list of document files(templates). I need to insert the selected template file into the currently opened document file.

I tried read the file and insert it using "documentEditor.editor.insertText()" method. But it showing ascii characters.

And I'm return a json string from my api

 WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
 string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);

How can I render this to my documenteditor done in angular. I think we can do it using "this.container.documentEditor.editor.insertSection();"

Thanks in advance
Bobbin

3 Replies

RT Ramya Thirugnanam Syncfusion Team July 2, 2019 12:42 PM UTC

Hi Bobbin, 
 
Thanks for contacting Syncfusion support. 
  
We can achieve this by using server-side approach. We are merging the document with already existing document using Syncfusion.Docio library, then open the merged document in document editor.  
 
Export the current opened document in DocumentEditor to server by using SaveAsBlob API. Please check the below documentation link: https://ej2.syncfusion.com/angular/documentation/document-editor/export/?no-cache=1#export-as-blob  
 
Add the below code snippet in controller file. Then open the return json string in documenteditor.open() method. 
 
public string MergeDocument()  
        {  
            //Opens the source document   
Stream stream = new MemoryStream();  
IFormFile file = HttpContext.Request.Form.Files[0];  
file.CopyTo(stream);  
stream.Position = 0;  
Syncfusion.DocIO.DLS.WordDocument sourceDocument = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx);  
  
//Opens the existing document which you want to insert with another document by providing path.  
  
WordDocument destinationDocument = new WordDocument(targetFileName);  
//Imports the contents of source document at the end of destination document  
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);  
MemoryStream outputStream = new MemoryStream();  
//Saves the destination document  
destinationDocument.Save(outputStream, FormatType.Docx);  
//closes the document instances  
sourceDocument.Close();  
destinationDocument.Close();  
  
  
Syncfusion.EJ2.DocumentEditor.WordDocument wdocument = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(outputStream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx);  
string json = Newtonsoft.Json.JsonConvert.SerializeObject(wdocument);  
wdocument.Dispose();  
//open the string in document editor               
return json;  
        }  
  
 
 
Regards, 
Ramya T 



BO Bobbin replied to Ramya Thirugnanam July 2, 2019 01:49 PM UTC

Hi Bobbin, 
 
Thanks for contacting Syncfusion support. 
  
We can achieve this by using server-side approach. We are merging the document with already existing document using Syncfusion.Docio library, then open the merged document in document editor.  
 
Export the current opened document in DocumentEditor to server by using SaveAsBlob API. Please check the below documentation link: https://ej2.syncfusion.com/angular/documentation/document-editor/export/?no-cache=1#export-as-blob  
 
Add the below code snippet in controller file. Then open the return json string in documenteditor.open() method. 
 
public string MergeDocument()  
        {  
            //Opens the source document   
Stream stream = new MemoryStream();  
IFormFile file = HttpContext.Request.Form.Files[0];  
file.CopyTo(stream);  
stream.Position = 0;  
Syncfusion.DocIO.DLS.WordDocument sourceDocument = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx);  
  
//Opens the existing document which you want to insert with another document by providing path.  
  
WordDocument destinationDocument = new WordDocument(targetFileName);  
//Imports the contents of source document at the end of destination document  
destinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);  
MemoryStream outputStream = new MemoryStream();  
//Saves the destination document  
destinationDocument.Save(outputStream, FormatType.Docx);  
//closes the document instances  
sourceDocument.Close();  
destinationDocument.Close();  
  
  
Syncfusion.EJ2.DocumentEditor.WordDocument wdocument = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(outputStream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx);  
string json = Newtonsoft.Json.JsonConvert.SerializeObject(wdocument);  
wdocument.Dispose();  
//open the string in document editor               
return json;  
        }  
  
 
 
Regards, 
Ramya T 


Hi Ramya,

Thanks for your reply....

Actually I need to append the document at the cursor point. That means if the cursor is pointed between the text of first document then second document should display at that point. Is it possible?


RT Ramya Thirugnanam Syncfusion Team July 3, 2019 12:01 PM UTC

Hi Bobbin, 
 
Currently, we do not have any API to insert document at current cursor position. From using Syncfusion.Docio library you insert the document at end of previous document.  
   
We have implemented paste method from this method you can insert the block at current cursor position. This paste method will be available in our Volume 2 release on end of June 2019.   
   
Note: From this paste method, you can merge the document blocks at current position not able to insert at separate section.  
   
Regards,  
Ramya T  


Loader.
Up arrow icon