Get started with Full API

The full API is a complete browser side PDF SDK, unlocking viewing, parsing and editing of PDF files. This guide will demonstrate how to set up a basic full API project that outputs the number of pages in a PDF document.

The full API does not require any conversion of documents but can only view PDF documents.

Initial setup

This guide will require the following files:

  1. The WebViewer folder.
  2. An empty HTML document.
  3. An empty JavaScript document.
  4. A PDF document of your choice. For this guide we will be using the newsletter PDF document.
  5. An image to add to the PDF document. For this guide we will be using an image of a butterfly.

Apryse Docs Image

Setting up the HTML file

Open up index.html with a text editor and copy/paste the following code into the HTML document.

HTML

1<!DOCTYPE html>
2<html style="height:100%;">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html">
5 <script src="WebViewer/lib/webviewer.min.js"></script>
6 </head>
7 <body style="width:100%; height:100%; margin:0px; padding:0px; overflow:hidden">
8 <div id="viewer" style="height: 100%; overflow: hidden;"></div>
9 <script src="index.js"></script>
10 </body>
11</html>

Here we include the required WebViewer file and start out with a single "viewer" div that we will add content to using a script.

Setting up your JavaScript document

In our custom script index.js, the WebViewer function is called to create a new WebViewer instance that will be added as a child to our "viewer" div.

JavaScript

1WebViewer({
2 path: "WebViewer/lib",
3 initialDoc: "WebViewer/samples/full-apis/TestFiles/newsletter.pdf",
4 showLocalFilePicker: true,
5 fullAPI: true,
6 licenseKey: 'Insert commercial license key here after purchase',
7}, document.getElementById('viewer'))
8 .then(instance => {
9 // WebViewer instance is ready
10 });

Overview of WebViewer initialization parameters:

  • path - String representing the URL that points to the WebViewer libraries.
  • initialDoc - String representing the URL of the document that will be loaded in WebViewer.
  • showLocalFilePicker - Boolean that determines whether we can open local documents in the viewer.
  • fullAPI - If true, allows the Full API to be used and PDFNet will be available on the instance.
  • licenseKey - String containing the license key (you do not need to set this property if you are just trialing)

If you open index.html from a server, you should be able to see your document displayed in WebViewer.

Apryse Docs Image

Now that we have our pdf displayed, let's use the full API to manipulate the document.

1WebViewer({
2 // ...
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer, PDFNet } = instance.Core;
6
7 documentViewer.addEventListener('documentLoaded', async () => {
8 await PDFNet.initialize();
9 const doc = documentViewer.getDocument();
10 const pdfDoc = await doc.getPDFDoc();
11
12 // Ensure that we have our first page.
13 await pdfDoc.requirePage(1);
14
15 // Run our main function using 'runWithCleanup'
16 await PDFNet.runWithCleanup(async () => await main(pdfDoc));
17
18 // Refresh the cache with the newly updated document
19 documentViewer.refreshAll();
20 // Update viewer with new document
21 documentViewer.updateView();
22 });
23
24 async function main(pdfDoc) {
25 alert("Hello WebViewer!");
26 }
27 });

In order to run this on browsers without ES7 support, you can convert the file to ES5 using ES7-to-ES5 transformers such as Babel.

If you run the project again in a server, you should be able to see "Hello WebViewer" pop up in an alert box once WebViewer has loaded. Before the custom code is run however, several checks and initializations need to be done first.

  • PDFNet.initialize() - Initializes Full API backend. This should be called before any Full API functions are called.
  • doc.getPDFDoc() - Extracts the PDFNet PDFDoc object from the WebViewer document.
  • pdfDoc.requirePage() - Ensures that a particular page of the pdf document is finished downloading before we read or write from it.
    • If the page(s) to be edited cannot be known until the custom script runs, requirePage() can be called instead in the middle of the custom code, but only by unlocking and relocking all operations.
    • An example of this can be seen in the html file of the Viewer Edit test on the samples page.

Writing your custom code

Let us change our main() code to do something more interesting:

JavaScript

1async function main(pdfDoc) {
2 pdfDoc.initSecurityHandler();
3 pdfDoc.lock();
4 const inputPath = "../../samples/full-apis/TestFiles/";
5 const firstPage = await pdfDoc.getPage(1);
6 // create a new page builder that allows us to create new page elements
7 const builder = await PDFNet.ElementBuilder.create();
8 // create a new page writer that allows us to add/change page elements
9 const writer = await PDFNet.ElementWriter.create();
10 writer.beginOnPage(firstPage, PDFNet.ElementWriter.WriteMode.e_overlay);
11
12 // Adding a JPEG image to output file
13 const img = await PDFNet.Image.createFromURL(pdfDoc, inputPath + "butterfly.png");
14
15 const imgWidth = await img.getImageWidth();
16 const imgHeight = await img.getImageHeight();
17 const element = await builder.createImageScaled(img, 100, 600, imgWidth, imgHeight);
18 writer.writePlacedElement(element);
19 writer.end();
20}

This code sample adds the "butterfly.png" image to location (x:100, y:600) relative to the lower left corner of the document's first page.

Once our custom code has finished running, two final functions docViewer.refreshAll() and docViewer.updateView() are called to refresh and update the WebViewer display.

The resulting viewer should look like this:

Apryse Docs Image

Converting async/await code (important for IE usage)

This guide and all other Full API guides use ES7 async/await functions in JavaScript which are currently supported in Chrome, Firefox, Edge, Safari, and Opera but may be unsupported in other browsers. If you are working with a browser without async/await support, you will have to transpile the code from ES7 to ES5 using something like Babel.

Full API samples

The best way to get started with writing your own full API code is to run the full API samples. You can find working samples and their source code on the Full API samples page.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales