Download and convert image to PDF

Generate PDF, Images, Shapes

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.


ASPX page

The aspx page is extremely simple. Basically it consists of an img element and a ActionLink element as shown in the following code snippet.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
        <img src="../image.png" />
        <br />
        @Html.ActionLink("Click here!", "Convert")

Code-behind file

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TallComponents.PDF;
using TallComponents.PDF.Shapes;
using System.Web.Mvc;
using System.IO;

namespace ImageToPDFWeb.Controllers
    public class PDFController : Controller
        public ActionResult Index()
            //show the index page to the client
            return View();

        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");