Add a Watermark Only When Printing

You may want to add a watermark to a document only when printing. At the moment, this is only possible when using PSPDFKit.PrintMode.DOM by executing the drawing commands in renderPageCallback during printing. This can be achieved with a snippet like the following:

let printingPage = -1;
let instance = null;
let isPrinting = false;

PSPDFKit.load({
  renderPageCallback: (ctx, pageIndex, pageSize) => {
    if (!isPrinting) {
      return;
    }

    printingPage++;
    if (printingPage === instance.totalPageCount - 1) {
      // Finished printing. Reset.
      printingPage = -1;
      isPrinting = false;
    }

    // Render the watermark.
  }
}).then((_instance) => {
  instance = _instance;
});

function pspdfkitPrint() {
  isPrinting = true;
  instance.print(PSPDFKit.PrintMode.DOM);
}

function pspdfkitCancelPrint() {
  isPrinting = false;
  instance.abortPrinting();
}

Call pspdfkitPrint() to print.

Information

It might be necessary to replace the built-in print toolbar button.

When pressing Command-P or Control-P, PSPDFKit will print using the default behavior and won’t invoke the pspdfkitPrint function. To override the keyboard shortcut behavior or disable keyboard shortcuts, you’ll need to register a custom event handler in instance.contentDocument.