Using PDFThumbnail.NET in an MVC application


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


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:

1 <%@ Page Language="c#" %> 2 <%@ Register TagPrefix="tc" Assembly=" TallComponents.PDFThumbnail " Namespace="TallComponents.Web.PDF" %> 3 4 <script runat="server"> 5 //this method is called when the display button has been clicked 6 protected void DisplayThumbs_Click(object sender, System.EventArgs e) 7 { 8 thumbnails.Controls.Clear(); //clear previous thumbnails 9 HttpPostedFile postedFile = Request.Files["pdf"]; //get pdf files which were posted 10 if (null != postedFile) 11 { 12 byte[] buffer = new byte[postedFile.ContentLength]; 13 postedFile.InputStream.Read(buffer, 0, buffer.Length); 14 string unique = Guid.NewGuid().ToString(); 15 Session[unique] = buffer; //place pdf at unique location 16 int n = Thumbnail.GetPageCount(postedFile.InputStream); 17 for (int i = 1; i <= n; i++) //create an image for each page 18 { 19 Thumbnail thumbnail = new Thumbnail(); 20 thumbnail.SessionKey = unique; 21 thumbnail.Index = i; //determines which page needs to be rendered 22 thumbnails.Controls.Add(thumbnail); 23 } 24 } 25 } 26 </script> 27 28 <html> 29 <body> 30 <form method="post" runat="server" enctype="multipart/form-data"> 31 <input width="100%" type="file" size="50" name="pdf" /> 32 <br /> 33 <asp:Button runat="server" Text="Display thumbs" ID="DisplayThumbs" OnClick="DisplayThumbs_Click" /><br /> 34 <asp:Panel ID="thumbnails" runat="server" /> 35 </form> 36 </body> 37 </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.