This article shows how to download an image on a web page as a PDF document. The image (JPG, PNG or any other raster format) to PDF conversion is done on the fly. Download a free trial of PDFKit.NET to try the image to PDF converter to convert JPG (or any other raster format) to PDF and more.
This article shows how to download an image on a web page as a PDF document. The image to PDF conversion is done on the fly.
The aspx page is extremely simple. Basically it consists of an img element and a ActionLink element as shown in the following code snippet.
The code-behind file consists of just the OnClick handler. It creates a document, adds a page that has the same size as the image, adds an image shape to the page and then streams the PDF document to the client.
public class PDFController : Controller
public ActionResult Index()
//show the index page to the client
public FileStreamResult Convert()
//Create a new PDF document and add the image as an overlay to a page
Document document = new Document();
ImageShape image = new ImageShape(
Page page = new Page(image.Width, image.Height);
//since we don't write to disk, write the pdf to a memory stream and return it as a pdf
MemoryStream ms = new MemoryStream();
ms.Position = 0;
return File(ms, "application/pdf");