ASP.NET PDF Viewer

Muhammad Hammad picture Muhammad Hammad · Dec 28, 2012 · Viewed 25.9k times · Source

I am looking for a ASP.NET control to load PDFs in browser. It should allow to control the number of pages to show to user, and also it should able to do some bookmark stuff like when user click on a button, then I could get the page number of viewer and save it, and then next time reload the PDF from that page number.

Answer

Ole K picture Ole K · Oct 27, 2015

As an alternative to IFRAME use PDFJS library (https://mozilla.github.io/pdf.js/)

It allows you to display the PDF document with Javascript/HTML5 Canvas only.

HTML5 Canvas browser compatibility: http://caniuse.com/#feat=canvas

Example to display a specific page - NOT TESTED

// pdf document file
var pdfDocument = 'yourfile.pdf';
// page Number you want to display
var pageNo = 1;
// name of the HTML5 Canvas
var canvasName = 'pdfCanvas';

PDFJS.getDocument( pdfDocument ).then(function (pdf) {
    pdf.getPage( pageNo ).then(function (page) {
        var scale = 1.5;
        var viewport = page.getViewport(scale);

        var canvas = document.getElementById(canvasName);
        var context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        var renderContext = {
            canvasContext: context,
            viewport: viewport
        };

        page.render(renderContext).promise.then(function () {
            // do something when rendering is completed
        });
    });
});