Reputation: 51
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.
Upvotes: 3
Views: 36533
Reputation: 573
You can try to embed the PDF file using the "object"-Tag in ASP.NET.
here's a little example:
//Markup
<asp:LinkButton ID="view" runat="server" Text="Click me!" onClick="ViewPDF"></asp:LinkButton>
<asp:Literal ID="embedPdf" runat="server"/>
//C#
private void ViewPDF(object sender, EventArgs e)
{
string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"1200px\" height=\"1000px\"></object>";
embedPdf.Text = string.Format(embed, ResolveUrl("/your/pdf/file/path.pdf"));
}
after clicking on the ASP-LinkButton the PDF-reader should appear.
Upvotes: -1
Reputation: 869
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
});
});
});
Upvotes: 6
Reputation: 1210
You can use iframe to view your pdf in browser as follows
<iframe src="mypdf.pdf"></iframe>
Upvotes: 3