Concept
This project converts txt, doc, docx, xls, xlsx, ppt, pptx and rtf extension files into a PDF and then converts the created PDF into a JPEG format. When a user uploads the file and clicks on the Submit button then first page of the uploaded file is converted into the image file and shown on the browser without a postback. For converting from a doc, docx, xls, xlsx, ppt, pptx or rtf file, Microsoft Office 2007 or later should be installed in your system. The uploaded file and the converted PDF file will be stored in the Attachments Folder and the converted JPEG file will be stored in the Preview Folder of this project.
1. First of all open Visual Studio 2010 or later and then select "File" -> "New" -> "Project..." and click on ASP.NET MVC3 Web Application in Visual C#, name the project FileUploader. Then add the following folders to your project.
- Classes
- UploadBooks
Attachments (Inside the UploadBooks folder)
Preview (Inside the Attachments folder)
2. Create a controller named HomeController and in this controller create an ActionResult method named FileUploader.
- namespace FileUploader.Controllers
- public class HomeController : Controller
- {
- // GET: /Home/
- public ActionResult FileUploader()
- {
- return View();
- }
- }
3. Now create a view, right-click on the Fileuploader() and select Add View and then click OK.
Write the following code in this view.
- @{<br /> ViewBag.Title = "FileUploader";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>
- FileUploader</h2>
- @using (Html.BeginForm())
- {
- <div>
- <img src="" width="100" height="100" id="msg" />
- </div>
- <div>
- <p id="message">
- </p>
- </div>
- <input type="file" name="file" id="file" />
- <input type="button" class="clickClass" value="submit" id="submit" />
- }
4. Create a model named FileUploaderModel.cs and add the following code to this. Also add the class file “PdfConvert.cs” to the Classes folder, add the “gsdll32.dll”, in other words Ghost Script, in the bin folder of your project and the reference file TextPdf.dll. This code converts the PDF file into single a JPEG file. Download the required files from the link in the last.
- namespace FileUploader.Models
- {
- public class FileUploaderModel
- {
- //public string guid { get; set; }
- PdfToImage.PDFConvert PdfConverter = new PdfToImage.PDFConvert();
- //Convert the pdf into Single Image.
- public void ConvertSingleImage(string filename)
- {
- try
- {
- PdfConverter.RenderingThreads = 5;
- PdfConverter.TextAlphaBit = 4;
- PdfConverter.OutputToMultipleFile = false;
- PdfConverter.FirstPageToConvert = -1;
- PdfConverter.LastPageToConvert = -1;
- PdfConverter.FitPage = false;
- PdfConverter.JPEGQuality = 10;
- PdfConverter.OutputFormat = "jpeg";
- System.IO.FileInfo input = new FileInfo(filename);
- string[] str = input.Name.Split('.');
- string output = string.Format("{0}\\{1}{2}", input.Directory + "\\Preview", str[0].ToString(), ".jpg");
- while (System.IO.File.Exists(output))
- {
- File.Delete(output);
- output = string.Format("{0}\\{1}{2}", input.Directory + "\\Preview", str[0].ToString(), ".jpg");
- }
- }
- }
- catch (Exception ex)
- {
- }
- return;
- }
- }
Converting the text file into JPEG file
1. Add the following code to your controller to convert the text file that will come from the FileUploader view into a PDF and then into the JPEG format and finally pass the converted JPEG file path back to the FileUploader view.
Converting the text file into JPEG file
2. Add the following code to your controller to convert the text file that will come from the FileUploader view into the PDF and then into the JPEG format and finally pass the converted JPEG file path back to the FileUploader view.
- [HttpPost]
- public ActionResult FileUploader(FileUploaderModel upload)
- {
- string guid = Guid.NewGuid().ToString();
- //upload.guid = guid;
- string msg = "Something went wrong, Your file was not uploaded.";
- HttpPostedFileBase file = Request.Files[0];
- string FinalPath = "";
- if (file != null && file.ContentLength > 0)
- {
- var fileName = Path.GetFileName(file.FileName); // extract only the fielname
- var ext = Path.GetExtension(fileName.ToLower()); //extract only the extension of filename and then convert it into lower case.
- //var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
- var path = Server.MapPath("~/UploadBooks/Attachments/" + guid + ext);
- file.SaveAs(path);
- if (ext == ".txt") //If the file is a "text" file.
- {
- string filePath = "";
- string filePath12 = "";
- filePath = Server.MapPath("~/UploadBooks/Attachments/" + guid + ext);
- filePath12 = Server.MapPath("~/UploadBooks/Attachments/");
- FinalPath = Server.MapPath("~/UploadBooks/Attachments/" + guid + ".pdf");
- TextPDF.PdfWriter pdfWriter = new TextPDF.PdfWriter(842.0f, 1190.0f, 10.0f, 10.0f);
- pdfWriter.Getfilename(guid, filePath12); //Convert the "txt" file into "pdf" file
- if (filePath.Length > 0)
- pdfWriter.Write(filePath); //Store the converted pdf file into Attachments folder
- upload.ConvertSingleImage(FinalPath); //Convert the pdf file into "jpg" file, the code is written in FileUploaderModel.cs
- string str = "../../UploadBooks/Attachments/Preview/" + guid + ".jpg"
- return Content(str);
- }
- }
- }
3. Now add the following JavaScript to your view that will pass the file information to your controller and then return the path of the image that will be built from your controller HTTPPOST FileUploader method.
- <script type="text/javascript">
- document.getElementById('submit').onclick = function () {
- var formdata = new FormData(); //FormData object
- var fileInput = document.getElementById('file');
- //Iterating through each files selected in fileInput
- for (i = 0; i < fileInput.files.length; i++) {
- //Appending each file to FormData object
- formdata.append(fileInput.files[i].name, fileInput.files[i]);
- }
- //Creating an XMLHttpRequest and sending
- var xhr = new XMLHttpRequest();
- xhr.open('POST', '/Home/FileUploader');
- xhr.send(formdata);
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && xhr.status == 200) {
- var msg = "Your file was successfully uploaded";
- $("#msg").attr('src', xhr.responseText);
- $("#message").text(msg)
- }
- }
- return false;
- }
- </script>
Converting the xls or xlsx file into a JPEG file
1. Add the following method code to your model to convert the xls or xlsx file into a PDF file and also add the reference file Microsoft.Office.Interop.Excel.dll.

Amol ManePosted Nov 5, 2014, 6:03 AM
I am using same code for the asp.net but getting error while converting pdf to image as "The gsdll32.dll wasn't found in default dlls search pathor is not in correct version (doesn't expose the required methods). Please download at least the version 8.64 from the original website" can you suggest how to resolve this error,can you please tell me how to add reference of gsdll32.dll in to asp.net application.
Sandeep Singh ShekhawatPosted Mar 14, 2014, 3:01 AM
It would be good if you include some images for output.
Yogesh TyagiPosted Mar 14, 2014, 12:32 AM
@Sandeep Singh Shekhawat,Mahesh Chand Thanx Sir....
Sandeep Singh ShekhawatPosted Mar 13, 2014, 7:51 AM
Good start.. Keep it up my friend...!!!
Mahesh ChandPosted Mar 13, 2014, 12:37 AM
One point you want to mention, your app will need write permissions on the folder you are uploading files to. Also, is this a web application? In that case, Office 2007 should be on the server. Good start. Cheers!
Yogesh TyagiPosted Mar 12, 2014, 5:29 AM
Thanx all of you..
Rajveersingh SodhaPosted Mar 12, 2014, 5:28 AM
nice artical dude keep it up , very helping material
Dinesh BeniwalPosted Mar 12, 2014, 5:25 AM
Good Start Yogesh, Welcome to the C# Corner.
Vinit JainPosted Mar 12, 2014, 5:23 AM
good post. keep it up