//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
string imagePath = _hostingEnvironment.WebRootPath + "image.jpg";
Stream imageStream = new FileStream(imagePath, FileMode.Open);
//Load the image from the disk.
PdfBitmap image = new PdfBitmap(imageStream);
//Draw the image
graphics.DrawImage(image, 0, 0);
//Saving the PDF to the MemoryStream
MemoryStream stream = new MemoryStream();
doc.Save(stream);
doc.Close(true);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
fileStreamResult.FileDownloadName = "Sample.pdf"; |