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.
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;
} |
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-blobAdd the below code snippet in controller file. Then open the return json string in documenteditor.open() method.
public string MergeDocument(){//Opens the source documentStream 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 documentdestinationDocument.ImportContent(sourceDocument, ImportOptions.UseDestinationStyles);MemoryStream outputStream = new MemoryStream();//Saves the destination documentdestinationDocument.Save(outputStream, FormatType.Docx);//closes the document instancessourceDocument.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 editorreturn json;}Documentation link for your reference: https://help.syncfusion.com/file-formats/docio/working-with-word-document#merging-word-documentsRegards,Ramya T