HTML to Docx or PDF to Docx

Currently I am using HtmlToPdfConverter in my project to convert HTML into PDF. I am trying to add support for Word conversion too... Does DocIO have support for Html to Docx (With Javascript) ​or PDF to Docx (With editable text NOT as image)​...?


3 Replies

GK Gowthamraj Kumar Syncfusion Team July 12, 2021 11:09 AM UTC

Hi Dineshkumar, 
 
Thank you for contacting Syncfusion support.

 
 Does DocIO have support for Html to Docx (With Javascript)  
DocumenteditorContainer : Toolbar options open html document, in backend convert html to Sfdt and load the same in Documenteditor.  
 
Also, if you want to create custom button, in DocumentEditor, you can open html file using Syncfusion Docio library. So, we need to write server side WebAPI either in Asp.Net MVC or Core to achieve your requirement. To open html file, we need to use Syncfusion.Docio library to convert the stream to document editor internal structure. Then convert the internal structure to json. Finally, open that json in document editor.    
   
Sample code snippet to open html in document editor.  
  
        [AcceptVerbs("Post")]  
        [HttpPost]  
        [Route("ImportHtmlToDoc")]  
        public string ImportHtmlToDoc(IFormCollection data)  
         
   
            Stream stream = new MemoryStream();  
            IFormFile file = data.Files[0];  
            file.CopyTo(stream);  
            stream.Position = 0;  
   
            //Loads the HTML document against transitional schema validation  
   
            Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream,Syncfusion.DocIO.FormatType.Html);  
   
            MemoryStream outputStream = new MemoryStream();  
            //Saves the Word document  
   
            document.Save(outputStream, Syncfusion.DocIO.FormatType.Docx);  
   
            //Closes the document  
   
   
            EJ2WordDocument wdocument = EJ2WordDocument.Load(outputStream, GetFormatType("docx"));  
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(wdocument);  
            wdocument.Dispose();  
            return json;  
         
   
 
PDF to Docx (With editable text NOT as image)​...? 
At present, we do not have direct support for PDF to Word conversion. We have already logged a feature request for converting PDF files to editable Word documents. We do not have any immediate plans to implement this feature. We usually have an interval of at least three months between releases and at the planning stage for every release cycle, we review all open features. We will consider this feature and update the details once it is implemented.        
      
You can track the status of this feature request from the following feedback link,          
 
Regards, 
Gowthamraj K 



DI Dineshkumar July 15, 2021 12:37 PM UTC

Hi Gowthamraj,


Thanks for reply. By "With Javascript" in HTML to Docx request I meant to render the HTML after processing Javascript like in HTML to PDF Converter.



KB Kurthis Banu Abdul Majeeth Syncfusion Team July 19, 2021 11:49 AM UTC

 Hi Dineshkumar,  

To Export the document as PDF, you can convert the word document into PDF by using ‘Syncfusion.DocToPDFConverter’ server-side library.   

In client side, using saveASBlob method, you can convert the Word document as blob stream and then send stream to server. In server side, using ‘Syncfusion.DocToPDFConverter’, you can convert the stream to pdf document and save it.    

Kindly refer the below code snippets for your reference.  

Client side:  
 
   saveAsPDF(): void {  
     var obj=this;  
    this.container.documentEditor.saveAsBlob('Docx').then(function (exportedDocument) {  
      // The blob can be processed further  
      var fileName = (obj.container.documentEditor.documentName ? obj.container.documentEditor.documentName : 'Untitled') + ".pdf";  
      var formData = new FormData();  
      formData.append('data', exportedDocument);  
      var httpRequest = new XMLHttpRequest();  
      httpRequest.open('POST', 'http://localhost:50574/api/DocumentEditor/DocToPDF?fileName=' + fileName, true);  
      httpRequest.onreadystatechange = function () {  
        if (httpRequest.readyState === 4) {  
          if (httpRequest.status === 200 || httpRequest.status === 304) {  
            alert('Document saved successFully');  
          } else {  
            //alert('Fail to save the document');  
          }  
        }  
      }  
      httpRequest.send(formData);  
    });  
  }  
   

Server side:  
 
Kindly add the below code snippet in your server-side application in DocumentEditorController.cs file   

Export As PDF:  
        [AcceptVerbs("Post")]  
        [HttpPost]  
        [EnableCors("AllowAllOrigins")]  
        [Route("DocToPDF")]  
        public string DocToPDF(string fileName)  
        {  
            Stream stream = new MemoryStream();  
            IFormFile file = HttpContext.Request.Form.Files[0];  
            file.CopyTo(stream);  
            stream.Position = 0;  
  
            Syncfusion.DocIO.DLS.WordDocument wordDocument = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx);  
  
            //Instantiation of DocIORenderer for Word to PDF conversion  
            DocIORenderer render = new DocIORenderer();  
  
            //Converts Word document into PDF document  
            PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);  
  
            //Saves the PDF file  
            FileStream fileStream = new FileStream(_hostingEnvironment.ContentRootPath + "\\App_Data\\" + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);  
  
            pdfDocument.Save(fileStream);  
  
            //Closes the instance of PDF document object  
            render.Dispose();  
            wordDocument.Dispose();  
            pdfDocument.Dispose();  
            fileStream.Close();  
            return "Success";  
        }  


Please let us know if you have any questions.  

Regards,  
Kurthis Banu A.  


Loader.
Up arrow icon