Advertisement
andruhovski

How to generate thumbnail images for PDF documents

Dec 4th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. //see also: https://github.com/aspose-pdf/Aspose.Pdf-for-.NET/blob/master/Examples/CSharp/AsposePDF/Images/PageToPNG.cs
  2. public class UploadController : Controller
  3.     {
  4.         // GET: Upload  
  5.         public ActionResult Index()
  6.         {
  7.             var di = new DirectoryInfo(Server.MapPath("~/Content/Images"));
  8.             var files = di.GetFiles("*.jpg").ToList();
  9.             return View(files);
  10.         }
  11.         [HttpGet]
  12.         public ActionResult UploadFile()
  13.         {
  14.             return View();
  15.         }
  16.  
  17.         [HttpPost]
  18.         public ActionResult UploadFile(HttpPostedFileBase file)
  19.         {
  20.             try
  21.             {
  22.                 if (file.ContentLength > 0)
  23.                 {
  24.                     var fileName = Path.GetFileName(file.FileName);
  25.                     if (fileName != null)
  26.                     {
  27.                         var pdfPath = Path.Combine(Server.MapPath("~/App_Data/Upload"), fileName);                        
  28.                         var jpgPath = Path.ChangeExtension(Path.Combine(Server.MapPath("~/Content/Images"), fileName), "jpg");
  29.                         Stream stream  = new MemoryStream();
  30.                         file.InputStream.CopyTo(stream);
  31.                         file.SaveAs(pdfPath);
  32.  
  33.                         // Open document
  34.                         var pdfDocument = new Document(stream);
  35.  
  36.                         using (var imageStream = new FileStream(jpgPath, FileMode.Create))
  37.                         {
  38.                             // Create JPEG device with specified attributes
  39.                             // Width, Height, Resolution, Quality
  40.                             // Quality [0-100], 100 is Maximum
  41.                             // Create Resolution object
  42.                             var resolution = new Resolution(96);
  43.  
  44.                             var jpegDevice = new JpegDevice(96, 128, resolution, 100);                            
  45.                             // ExStart:ConvertParticularPage
  46.                             // Convert a particular page and save the image to stream
  47.                             jpegDevice.Process(pdfDocument.Pages[1], imageStream);
  48.                             // ExEnd:ConvertParticularPage
  49.                             // Close stream
  50.                             imageStream.Close();
  51.                         }
  52.                     }
  53.                 }
  54.                 ViewBag.Message = "File Uploaded Successfully!!";
  55.                 return View();
  56.             }
  57.             catch
  58.             {
  59.                 ViewBag.Message = "File upload failed!!";
  60.                 return View();
  61.             }
  62.         }
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement