How to display/generate PDF pages as thumbnails in ASP.NET MVC?
Displaying PDF document pages as thumbnails
ASP.NET MVC PDF Viewer does not support displaying thumbnails of the PDF file loaded, however as a workaround, the PDF pages are displayed as thumbnail by exporting the pages as images. Clicking the thumbnail image will navigate to the corresponding page in PDF Viewer.
You can export the Pdf document pages as images using the Syncfusion PDF Base library’s PdfLoadedDocument.ExportAsImage() method. Refer to the following steps to create and display the PDF pages as thumbnail.
Step 1: Create the thumbnail pane using the HTML.
<div style="width:100%; height:760px;"> <div style="left:2px;"> <div id="thumbnailpane" style="overflow:scroll; width:15%;height:640px"> </div> </div> <div style="left:20%; position: absolute; width:80%; top:50px; height:780px"> <div class="content-container-fluid"> <div class="row"> <div class="cols-sample-area"> <div class="control"> <div id="container"></div> </div> </div> </div> </div> </div> </div>
Step 2: Sending AJAX request to create the thumbnail images in the server-side and in the success of the AJAX request, PDF Viewer will be initialized.
$.ajax({ url: '../api/PdfViewer/ExportAsImage', type: 'POST', dataType: 'json', crossDomain: true, traditional: true, contentType: 'application/json; charset=utf-8', data: '', success: function (data) { thumbnail(data); //Initializes PDF viewer $('#container').ejPdfViewer({ serviceUrl: '../api/PdfViewer', documentLoad: 'onDocumentLoad' }) }, error: function (msg, textStatus, errorThrown) { alert('Exception' + msg.responseText); } });
Step 3: Export the PDF pages as images using the PdfLoadedDocument’s ExportAsImage() API.
public object ExportAsImage() { PdfLoadedDocument loadedDocument = new PdfLoadedDocument(HttpContext.Current.Server.MapPath("~/Data/HTTP Succinctly.pdf")); //Exports the loaded PDF document to as images Bitmap[] extportedImages = loadedDocument.ExportAsImage(0, loadedDocument.Pages.Count - 1); string outputPath = HttpContext.Current.Server.MapPath("~/Images/"); for (int i = 0; i < extportedImages.Length; i++) { extportedImages [i].Save(outputPath + "\\Image" + (i + 1) + ".png"); } string output = (loadedDocument.Pages.Count).ToString(); return output; }
Step 4: Place the images in the thumbnail pane.
function thumbnail(totalpage) { //Place the exported images in thumbnail pane var thumbdiv = document.getElementById("thumbnailpane"); for (var i = 1; i <= totalpage; i++) { var thumbimg = document.createElement('div'); thumbimg.className = 'thumbnail'; var img = document.createElement('img'); img.src = '../images/image' + i + '.png'; thumbimg.onclick = function (e) { var pagenum = e.currentTarget.id; gotopage(pagenum); }; thumbimg.id = i; thumbimg.appendChild(img); thumbdiv.appendChild(thumbimg); } }
Step 5: Click the thumbnail image to navigate to the corresponding page in PDF Viewer.
function gotopage(pagenum) { var pdfviewerObject = $('#container').data('ejPdfViewer'); //Navigates to given page number pdfviewerObject.goToPage(pagenum); }
Sample:
Executing the previously given sample will provide the output as follows. Clicking the thumbnail image will display the corresponding page in PDF Viewer.
You can find the PDF Viewer’s other options like adding Toolbar Customization, TextMarkup annotation, and Handwritten Signature features here with code examples.
Refer here to explore the rich set of PDF Viewer features.
An online demo link to view PDF documents using Syncfusion PDF Viewer.
See Also:
https://www.syncfusion.com/jquery/aspnet-web-forms-ui-controls/pdf-viewer
https://www.syncfusion.com/jquery-ui-widgets/pdf-viewer
https://www.syncfusion.com/uwp-ui-controls/pdf-viewer
https://www.syncfusion.com/winforms-ui-controls/pdf-viewer
https://www.syncfusion.com/wpf-controls/pdf-viewer
Conclusion
I hope you enjoyed learning about how to display/generate PDF pages as thumbnails in ASP.NET MVC.
You can refer to our ASP.NET
MVC PDFViewer feature tour page to
know about its other groundbreaking feature representations and documentation, and
how to quickly get started for configuration specifications. You can also
explore our ASP.NET
MVC PDFViewer example to
understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!