Skip to Content
NexPDF

Getting started

Open a PDF in a NexPDF window from an Electron app, let the user edit it, and save the result with NexPDF for Electron.

English

This example opens a PDF passed on the command line in a NexPDF window. The user can annotate, fill, sign and organise it there, and save it with File, then Save As. It runs without a license key too: viewing is always allowed, and pages then carry a watermark.

Install the SDK

Add @nexdevit/nexpdf-electron and the engine binary as described in Installation.

Write the main process

main.js
const { app } = require('electron');
const path = require('node:path');
const { openPdfWindow } = require('@nexdevit/nexpdf-electron');

app.whenReady().then(async () => {
  // 1. The document to open (any PDF, picture or Office file).
  const file = process.argv.slice(2).find((a) => !a.startsWith('-') && a !== '.');

  // 2. Open a window with the NexPDF reader. The license is optional for viewing.
  const pdf = await openPdfWindow({
    file,
    license: process.env.NEXPDF_LICENSE,            // key, or path to the XML license file
    window: { title: 'Acme Docs' },
  });

  // 3. Save a copy with every edit, for example from your own code or menu item.
  // await pdf.save(path.join(app.getPath('documents'), 'contract-edited.pdf'));
  console.log('NexPDF engine at', pdf.engine.url);
});

app.on('window-all-closed', () => app.quit());

Run it

npx electron main.js contract.pdf

What the code does

  • openPdfWindow(options) starts the engine, opens a BrowserWindow (1280 x 860 by default) on it and resolves to a PdfWindow. window takes extra BrowserWindow options.
  • file opens a document at start: a PDF, a PNG, JPEG or TIFF picture, or a Word, Excel or PowerPoint file. Without it the window opens empty, with its Open button.
  • license is the license key, or the path of the XML license file. See License.
  • pdf.bytes() resolves to the current document, with every edit, as a Buffer; pdf.save(path) writes it to a file and pdf.open(path) opens another file in the same window.

Work with documents in code

The window is for the user. To process documents in the main process without a window, for example to read the tables of an invoice before showing it, use the document API (documents() and openDocumentFile(), 1.1 and later):

const { openDocumentFile } = require('@nexdevit/nexpdf-electron');

const doc = await openDocumentFile(file);
console.log(doc.pageCount, 'pages:', doc.text(0).slice(0, 200));
doc.close();

See Opening and saving.

Next steps

On this page