Skip to content
Loading
How to convert Word docx to either Html or PDF from MVC application
  • A solution you can try is using the ImageViewer and DocumentConverter classes provided through the LEADTOOLS SDK. The ImageViewer will allow you to preview your files, and you can then use the DocumentConverter to convert to a PDF or HTML file.
     
    To convert your image, you can try the following code:
    1. // Pass DocumentFormat.PDF to convert to a PDF  
    2. // Pass RasterImageFormat.Htm to convert to a HTML file  
    3.       private void SimpleConvert(string inputFile, IOcrEngine ocrEngine, string outputFile, DocumentFormat docFormat, RasterImageFormat imageFormat)  
    4.       {  
    5.          using (DocumentConverter documentConverter = new DocumentConverter())  
    6.          {  
    7.             documentConverter.SetOcrEngineInstance(ocrEngine, true);  
    8.             documentConverter.SetDocumentWriterInstance(new DocumentWriter());  
    9.    
    10.    
    11.             var inputDocument = DocumentFactory.LoadFromFile(inputFile, new LoadDocumentOptions());  
    12.             DocumentConverterJobData jobData = new DocumentConverterJobData()  
    13.             {  
    14.                Document = inputDocument,  
    15.                OutputDocumentFileName = outputFile,  
    16.                DocumentFormat = docFormat,  
    17.                AnnotationsMode = DocumentConverterAnnotationsMode.Embed,  
    18.                RasterImageBitsPerPixel = 0,  
    19.                RasterImageFormat = imageFormat  
    20.             };  
    21.    
    22.    
    23.             var job = documentConverter.Jobs.CreateJob(jobData);  
    24.             documentConverter.Jobs.RunJob(job);  
    25.          }  
    26.       }  
     https://www.leadtools.com/help/sdk/v21/dh/doxc/documentconverter.html
     
    To view your image, you can load in your created file like so:
     
    1. _imageViewer = new ImageViewer();   
    2. _imageViewer.Dock = DockStyle.Fill;   
    3. _imageViewer.BackColor = Color.Bisque;   
    4. this.Controls.Add(_imageViewer);   
    5. _imageViewer.BringToFront();   
    6.    
    7. // Load an image   
    8. using (var codecs = new RasterCodecs())   
    9. _imageViewer.Image = codecs.Load(Path.Combine(ImagesPath.Path, "image1.pdf"));  
     https://www.leadtools.com/help/sdk/v21/dh/c/imageviewer.html
  • Thanks Leon and Jignesh for your suggestions. But e-iceblue - Spire.Doc is not a free tool. I tried using evaluation version and it generates PDF with it but it adds a e-iceblue watermark as background in all pages in PDF also there is some limit of paragraph and pages i guess in evaluation version. Hence cannot use that. I am looking for a completly free tool. Coversion to PDF or Html. 
     
    Also second link suggested by Jignesh about WordToPDF.dll, I tried using that also. It works fine on local env (with localhost) but it is not working on app server(IIS on AWS VM). It does not generate the PDF on app server. There is no any documentation or help available for this tool. 
     
    Please suggest if there is any alternate way. I basically want to preview Word document report in browser through MVC application.
     
    Thanks. 
  • Hi,
     
    Check the following links:
    https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Word-to-HTML-Convert-Word-to-HTML-with-C-VB.NET.html
     
    https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html 
  • Jignesh Kumar
    Hi ,
     
    Please use this link,
    https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html 
    https://www.c-sharpcorner.com/UploadFile/698727/convert-word-file-to-pdf-using-C-Sharp/