Skip to content
Loading
 
  1. public ActionResult Upload(HttpPostedFileBase File)  
  2.       {  
  3.           object documentFormat = 8;  
  4.           string randomName = DateTime.Now.Ticks.ToString();  
  5.           object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";  
  6.           string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";  
  7.           object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(File.FileName);  
  8.   
  9.           //If Directory not present, create it.  
  10.           if (!Directory.Exists(Server.MapPath("~/Temp/")))  
  11.           {  
  12.               Directory.CreateDirectory(Server.MapPath("~/Temp/"));  
  13.           }  
  14.   
  15.           //Upload the word document and save to Temp folder.  
  16.           File.SaveAs(fileSavePath.ToString());  
  17.   
  18.           //Open the word document in background.  
  19.           _Application applicationclass = new Application();  
  20.           applicationclass.Documents.Open(ref fileSavePath);  
  21.           applicationclass.Visible = false;  
  22.           Document document = applicationclass.ActiveDocument;  
  23.   
  24.           //Save the word document as HTML file.  
  25.           document.SaveAs(ref htmlFilePath, ref documentFormat);  
  26.   
  27.           //Close the word document.  
  28.           document.Close();  
  29.   
  30.           //Read the saved Html File.  
  31.           string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());  
  32.   
  33.           //Loop and replace the Image Path.  
  34.           foreach (Match match in Regex.Matches(wordHTML, "", RegexOptions.IgnoreCase))  
  35.           {  
  36.               wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);  
  37.           }  
  38.   
  39.           //Delete the Uploaded Word File.  
  40.           System.IO.File.Delete(fileSavePath.ToString());  
  41.   
  42.           return Json(wordHTML ,JsonRequestBehavior.AllowGet);  
  43.   
  44.            
  45.       } 
+3
  • Jignesh Kumar
    +1
    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/ 
    +1
  • +1
    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 
    +1
  • +1
    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. 
    +1
  • +1
    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
    +1