The JavaScript Client API |
The page that hosts a viewer can drive it from JavaScript and follow what the user does - for example to build its own navigation, show the current page elsewhere in the page, or react when the document has loaded.
The viewer serves the script of its client API:
ASP.NET Core: /expertpdf-viewer/expertpdf-viewer-client.js (under the PathPrefix of UseExpertPdfViewer).
ASP.NET Web Forms: the ClientScriptUrl property of the control.
<!-- ASP.NET Core --> <expertpdf-viewer id="viewer" src="~/files/report.pdf" height="700px"></expertpdf-viewer> <script src="/expertpdf-viewer/expertpdf-viewer-client.js"></script> <!-- ASP.NET Web Forms --> <epdf:PdfViewer ID="viewer" runat="server" ClientIDMode="Static" PdfSourceURL="~/files/report.pdf" Height="700px" /> <script src="<%= viewer.ClientScriptUrl %>"></script>
ExpertPdfViewer.get(id) returns the object that drives the viewer with that element id (the ClientID of the Web Forms control). A page can host several viewers.
Method | Description |
|---|---|
goToPage(n) | Go to page n (the first page is 1). |
firstPage(), previousPage(), nextPage(), lastPage() | Move to the first, previous, next or last page. |
goToNamedDestination(name) | Go to a named destination of the document. |
back(), forward() | Back and forward through the positions left by a jump (a link, an outline entry, a destination, goToPage). |
zoom(percent) | Set the zoom (100 = 100%). |
fit(mode) | "Fit", "FitH" or "FitV". |
layout(mode) | "SinglePage", "OneColumn", "TwoColumnLeft" or "TwoColumnRight". |
displayMode(mode) | "thumbs", "bookmarks", "attachments" or "none". |
showToolbar(on), showNavigationPanel(on) | Show or hide the toolbar or the navigation panel button. |
theme(mode) | "light", "dark" or "system" (as set in the browser). |
showToolbarButton(button, on) | Show or hide a toolbar button: "open", "sidebar", "find", "pageNavigation", "zoom", "print", "download", "presentationMode" or "documentProperties". Its keyboard shortcut does nothing while it is hidden. |
find(text, options) | Search the document, highlight every match and show the first one from the current page on. options: { matchCase: true, wholeWord: true } (both false by default). An empty text removes the highlights. The result comes in the searchcompleted event. |
findNext(), findPrevious() | Show the next or the previous match of the last search (after the last match, the first one). |
highlight(left, top, width, height) | Highlight a rectangle of the current page (points from the top left corner of the page). A rectangle with no size removes it. |
print() | Open the print dialog of the browser for the document. |
on(event, handler), off(event, handler) | Add or remove an event handler (see below). |
The methods return the viewer object, so calls can be chained. To act as soon as the document is displayed, use the loaded event.
Property | Description |
|---|---|
isDocumentLoaded | True when the document is displayed. |
pageCount | The number of pages. |
currentPage | The page shown. |
zoomPercentage | The current zoom. |
searchResult | The result of the last search (as in the searchcompleted event), or null. |
Event | Handler argument |
|---|---|
loaded | The page count. Raised when the document is displayed. |
pagechange | The page number. Raised when the page shown changes. |
zoomchange | The zoom percentage. |
error | The message, when the document could not be opened. |
searchcompleted | { text, matchCount, currentMatch, pageNumber }: raised when a search is complete - after find, findNext and findPrevious, and after a search made in the find bar of the viewer. currentMatch (from 1) and pageNumber are 0 when nothing was found. |
passwordrequired | { incorrect, cancel }: the document being opened needs a password. The handler returns the password (a string, or a Promise of one); returning nothing opens the password prompt of the viewer. incorrect is true when the password given before was wrong; set cancel = true to stop opening the document (the error event is raised). Add the handler before the viewer loads its document (in a script after the client script). |
var viewer = ExpertPdfViewer.get("viewer"); viewer.on("loaded", function (pageCount) { document.getElementById("pages").textContent = pageCount; }); viewer.on("pagechange", function (page) { document.getElementById("page").textContent = page; }); document.getElementById("chapter2").onclick = function () { viewer.goToNamedDestination("chapter2"); }; document.getElementById("print").onclick = function () { viewer.print(); };
Find, theme and the password of a protected document:
var viewer = ExpertPdfViewer.get("viewer"); viewer.on("searchcompleted", function (r) { document.getElementById("found").textContent = r.matchCount ? r.currentMatch + " of " + r.matchCount + " (page " + r.pageNumber + ")" : "Not found"; }); document.getElementById("find").onclick = function () { viewer.find(document.getElementById("text").value, { wholeWord: true }); }; document.getElementById("next").onclick = function () { viewer.findNext(); }; viewer.on("passwordrequired", function (e) { if (e.incorrect) { e.cancel = true; return; } // the password from the server, for example return fetch("/api/document-password").then(function (r) { return r.text(); }); }); viewer.theme("dark"); viewer.showToolbarButton("download", false);