In both methods shown below, a local PDF opens in an iframe on the selected page number when the focus leaves the TextBox.
Method 1
In this method, the iframe source URL is set in two steps. The first step resets the source, the second sets it to the actual URL. This second step must be done asynchronously to work (here with setTimeout ).
Markup for iframe and TextBox:
<iframe id="iframePdfViewer" name="iframePdfViewer" width="700px" height="700px" ></iframe> <asp:TextBox ID="txtPageNumber" runat="server" Text="1" onchange="showPDF(this.value);" />
Javascript Function:
function showPDF(pageNumber) { var iframe = document.getElementById("iframePdfViewer"); iframe.src = ''; setTimeout(function () { iframe.src = 'MyFolder/MyDoc.pdf#page=' + pageNumber }, 0); }
Method 2
In this method, the iframe is replaced with a new one every time a new PDF page is displayed. To reduce screen flickering: (1) a new iframe is inserted under the old one, and (2) the old iframe is only removed when the new one is fully loaded.
Markup:
<div id="divPdfViewer" style="position: relative; width: 700px; height: 700px;"></div> <asp:TextBox ID="txtPageNumber" runat="server" Text="1" onchange="showPDF(this.value);" />
Javascript Code:
function showPDF(pageNumber) { var divPdfViewer = document.getElementById('divPdfViewer'); var ifrm = document.createElement("IFRAME"); ifrm.id = 'iframePdfViewer'; ifrm.style.position = 'absolute'; ifrm.style.width = '100%'; ifrm.style.height = '100%'; var oldPdfViewer = divPdfViewer.firstChild; if (oldPdfViewer) { ifrm.onload = function () { divPdfViewer.removeChild(oldPdfViewer); };
ConnorsFan
source share