//see also: https://github.com/aspose-pdf/Aspose.Pdf-for-.NET/blob/master/Examples/CSharp/AsposePDF/Images/PageToPNG.cs
public class UploadController : Controller
{
// GET: Upload
public ActionResult Index()
{
var di = new DirectoryInfo(Server.MapPath("~/Content/Images"));
var files = di.GetFiles("*.jpg").ToList();
return View(files);
}
[HttpGet]
public ActionResult UploadFile()
{
return View();
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
try
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
if (fileName != null)
{
var pdfPath = Path.Combine(Server.MapPath("~/App_Data/Upload"), fileName);
var jpgPath = Path.ChangeExtension(Path.Combine(Server.MapPath("~/Content/Images"), fileName), "jpg");
Stream stream = new MemoryStream();
file.InputStream.CopyTo(stream);
file.SaveAs(pdfPath);
// Open document
var pdfDocument = new Document(stream);
using (var imageStream = new FileStream(jpgPath, FileMode.Create))
{
// Create JPEG device with specified attributes
// Width, Height, Resolution, Quality
// Quality [0-100], 100 is Maximum
// Create Resolution object
var resolution = new Resolution(96);
var jpegDevice = new JpegDevice(96, 128, resolution, 100);
// ExStart:ConvertParticularPage
// Convert a particular page and save the image to stream
jpegDevice.Process(pdfDocument.Pages[1], imageStream);
// ExEnd:ConvertParticularPage
// Close stream
imageStream.Close();
}
}
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
}
Comments