Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 127.9k

convert a jpg/png/txt or any file format to pdf using mvc5

Apr 25 2017 9:10 AM
How do i convert a jpg/png/txt or any file format to pdf using mvc c#. Here is the code:
 
  1. public ActionResult SaveProfileDocument(string code)  
  2.         {  
  3.             bool isSavedSuccessfully = true;  
  4.             string fName = "";  
  5.             string _documentname = String.Empty;  
  6.   
  7.             try  
  8.             {  
  9.                 foreach (string fileName in Request.Files)  
  10.                 {  
  11.                     HttpPostedFileBase file = Request.Files[fileName];  
  12.                     //Save file content goes here  
  13.                     fName = file.FileName;  
  14.                     if (file != null && file.ContentLength > 0)  
  15.                     {  
  16.   
  17.                         var originalDirectory = new DirectoryInfo(string.Format("{0}Documents\\Profile\\" + code, Server.MapPath(@"\")));  
  18.   
  19.                         string pathString = System.IO.Path.Combine(originalDirectory.ToString());  
  20.   
  21.                         var fileName1 = Path.GetFileName(file.FileName);  
  22.   
  23.                         bool isExists = System.IO.Directory.Exists(pathString);  
  24.   
  25.                         if (!isExists)  
  26.                             System.IO.Directory.CreateDirectory(pathString);  
  27.   
  28.                         _documentname=fName;  
  29.   
  30.                         var path = string.Format("{0}\\{1}", pathString, file.FileName);  
  31.                         if (System.IO.File.Exists(path)) {  
  32.                             _documentname=Guid.NewGuid()+"_"+file.FileName;  
  33.   
  34.                             var path2 = string.Format("{0}\\{1}", pathString,_documentname );  
  35.                             file.SaveAs(path2);  
  36.                         }  
  37.                         else {  
  38.                             file.SaveAs(path);  
  39.                         }  
  40.   
  41.                     }  
  42.   
  43.                 }  
  44.   
  45.             }  
  46.             catch (Exception ex)  
  47.             {  
  48.                 isSavedSuccessfully = false;  
  49.             }  
  50.   
  51.   
  52.             if (isSavedSuccessfully)  
  53.             {  
  54.                 return Json(new { Message = fName, documentname = _documentname });  
  55.             }  
  56.             else  
  57.             {  
  58.                 return Json(new { Message = "Error in saving file", documentname=""});  
  59.             }  
  60.         }  

In the above code i am saving the file.but here i need to convert the file and then save.

so for convert i need a separate class or method here only call that method.

The thing is that while upload a file inthat time need to convert pdf any file to convert pdf. and save in folder or whatever.

 

Answers (6)