Prevent printing PDF annotations | Nutrient
Pass excludeAnnotations: true to instance.print() to print a document without annotations. This option doesn’t set flags on annotations, and it applies only to the current print job.
It also excludes annotations added after the document loads:
// Print the document without its annotations.instance.print({ excludeAnnotations: true });This option also excludes form field values. The SDK displays form fields with widget annotations. Refer to the print without annotations guide for more information.
Keep form fields on the printout
Set the noPrint flag on annotations to exclude annotations and still print form field values and signatures. The flag only affects annotations that have it when printing starts.
Run the following function instead of instance.print():
async function printWithoutAnnotations() { const annotations = [];
for (let pageIndex = 0; pageIndex < instance.totalPageCount; pageIndex++) { const pageAnnotations = (await instance.getAnnotations(pageIndex)) // Skip form field widgets and signatures so that they still print. .filter( (annotation) => !(annotation instanceof NutrientViewer.Annotations.WidgetAnnotation) && !annotation.isSignature && !annotation.noPrint ) .toArray();
annotations.push(...pageAnnotations); }
// Set the flags in one update, and wait for it before printing. await instance.update( annotations.map((annotation) => annotation.set("noPrint", true)) );
// Restore the flags when the print dialog closes. window.addEventListener( "afterprint", () => { instance.update(annotations); }, { once: true } );
instance.print();}Updating annotations requires a license that includes the Annotations component.
Until the flags are restored, the SDK saves them with the document like any other annotation change.
The browser only fires afterprint on your page with NutrientViewer.PrintMode.DOM, the default print mode. With NutrientViewer.PrintMode.EXPORT_PDF, the SDK prints from a hidden frame or opens the PDF in a new tab.
In that case, your page doesn’t get afterprint, and the flags stay set. The flags also stay set when printing is canceled before the print dialog opens.
With Document Engine, the flag only takes effect with NutrientViewer.PrintMode.DOM.
Toolbar button and keyboard shortcut
The built-in print button and the Control-P shortcut (Command-P on Mac) use the default print options, and neither waits for your code before printing.
To use either approach from the toolbar, replace the print button with a custom toolbar item. Make the custom item call instance.print({ excludeAnnotations: true }) or printWithoutAnnotations().
Replacing the button doesn’t change the shortcut. Refer to the print button guide for more information.