Add watermark only when printing

Q: We want to add a watermark to the document only when printing.

A: At the moment this is only possible when using PSPDFKit.PrintMode.DOM. The solution is to execute the drawing commands in renderPageCallback only during printing. This can be achieved with a similar snippet:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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.

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

When pressing cmd+p or ctrl+p PSPDFKit will print using the default behavior and won’t invoke the pspdfkitPrint function. You would need to register a custom event handler in instance.contentDocument to override the keyboard shortcut behavior or disable keyboard shortcuts.