Created
2/11/2016
Tags

In this tutorial we will take a look at how you can create your own webform to display thumbails of your PDF.

pdf-thumbnail-demo.png

We will create an ASPX web form where you can upload your PDF after which it will be displayed in the web browser. To do this, we will need to create an ASP.NET web application project in visual studio. Make sure you select an empty template so we don't have a large collection of useless files. Also make sure to un-select the field 'Host in the cloud', this will require special privileges of your Microsoft account.

Next you can add a new webform to the project (right-click on project -> add -> webform) and put the following code in it:

<%@ Page Language="c#" %>
<%@ Register TagPrefix="tc" Assembly=" TallComponents.PDFThumbnail " Namespace="TallComponents.Web.PDF" %>

<script runat="server">
    //this method is called when the display button has been clicked
    protected void DisplayThumbs_Click(object sender, System.EventArgs e)
    {
        thumbnails.Controls.Clear(); //clear previous thumbnails
        HttpPostedFile postedFile = Request.Files["pdf"]; //get pdf files which were posted
        if (null != postedFile)
        {
            byte[] buffer = new byte[postedFile.ContentLength];
            postedFile.InputStream.Read(buffer, 0, buffer.Length);
            string unique = Guid.NewGuid().ToString();
            Session[unique] = buffer; //place pdf at unique location
            int n = Thumbnail.GetPageCount(postedFile.InputStream);
            for (int i = 1; i <= n; i++) //create an image for each page
            {
                Thumbnail thumbnail = new Thumbnail();
                thumbnail.SessionKey = unique;
                thumbnail.Index = i; //determines which page needs to be rendered
                thumbnails.Controls.Add(thumbnail);
            }
        }
    }
</script>

<html>
<body>
    <form method="post" runat="server" enctype="multipart/form-data">
        <input width="100%" type="file" size="50" name="pdf" />
        <br />
        <asp:Button runat="server" Text="Display thumbs" ID="DisplayThumbs" OnClick="DisplayThumbs_Click" /><br />
        <asp:Panel ID="thumbnails" runat="server" />
    </form>
</body>
</html>

Obviously you should add the PDFThumbnail DLL to this project, which enables the rendering of the thumbnails. Now you can use this application by clicking the 'run' button, after which a webpage will open. You can select here the desired PDF file and after clicking on the 'Display thumbs' button, the thumbnails will be rendered on the webpage. Note that this may take some time especially with PDFs which have a lot of pages.