---
title: "Headless redactions from text selection | Nutrient Web SDK"
canonical_url: "https://www.nutrient.io/guides/web/headless/redactions-from-selection/"
md_url: "https://www.nutrient.io/guides/web/headless/redactions-from-selection.md"
last_updated: "2026-05-15T19:10:05.084Z"
description: "Create redaction annotations from the user’s current text selection in Nutrient Web SDK without using the default inline toolbar."
---

# Redactions from text selection

The `instance.annotations.redaction.createFromCurrentTextSelection()` method creates redaction annotations directly from the user’s text selection with no menu, no popover, and no built-in chrome. Pair it with the existing [`createRedactionsBySearch`](https://www.nutrient.io/api/web/classes/NutrientViewer.Instance.html#createredactionsbysearch) and [`applyRedactions`](https://www.nutrient.io/api/web/classes/NutrientViewer.Instance.html#applyredactions) APIs to build a redaction workflow entirely from your own UI.

Looking for the visual side of this? See the `annotations.textMarkupInline` [slot](https://www.nutrient.io/guides/web/user-interface/ui-customization/supported-slots.md#annotations) for replacing the inline toolbar that appears when the user selects text. This page covers the programmatic surface you’ll call from inside that custom UI.

## When to use this

Reach for selection-driven redactions when you’re building:

- A custom inline button that appears on text selection and converts the selection to a redaction in a single click.

- A bulk-redaction workflow that combines selection-driven redactions with pattern-driven ones.

- A custom confirmation flow that previews the redaction before applying it permanently.

- A keyboard shortcut your application defines for “redact this selection.”

## API reference

| Method                                                            | Notes                                                                                                       |
| ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `instance.annotations.redaction.createFromCurrentTextSelection()` | Creates redaction annotations covering the current text selection. Throws if no text is currently selected. |
| `instance.annotations.redaction.getColorPresets()`                | Returns the resolved color presets for redaction fill, outline, and overlay-text colors.                    |
| `instance.createRedactionsBySearch(term, options?)`               | Existing, creates redactions for every match of a string or regex across the document.                      |
| `instance.applyRedactions()`                                      | Existing, applies the pending redactions, permanently removing the underlying content.                      |

Because `createFromCurrentTextSelection()` throws when there’s no selection, gate the call on an active text selection before invoking it.

## Example: A custom inline redaction button

This example wires a host-app button to convert the user’s text selection into a redaction. A second button applies all pending redactions when the user is ready:

```js

const instance = await NutrientViewer.load({
  container: "#viewer",

  document: "contract.pdf"
});

const toolbar = document.getElementById("redaction-toolbar");
const redactButton = toolbar.querySelector("#redact-selection");

const applyButton = toolbar.querySelector("#apply-redactions");

instance.addEventListener("textSelection.change", (selection) => {
  // The event emits either a `TextSelection` or `null`. A null check is enough.
  redactButton.disabled =!selection;
});

redactButton.onclick = () => {
  const created = instance.annotations.redaction.createFromCurrentTextSelection();
  console.log(`Created ${created.size} redaction(s).`);
};

applyButton.onclick = async () => {
  const confirmed = window.confirm(
    "Applying redactions removes the underlying content permanently. Continue?"
  );
  if (confirmed) {
    await instance.applyRedactions();
  }
};

```

The created redactions are pending, and they appear as dark overlays on the page, but the underlying content isn’t removed until `applyRedactions()` runs. This gives the user a chance to review what’s been marked for redaction before it becomes permanent.

## Example: Combine selection and pattern redactions

The selection-driven and pattern-driven flows compose naturally, and you can create both kinds of redactions in the same workflow before applying them:

```js

async function redactSensitiveData(instance) {
  if (instance.getTextSelection()) {
    instance.annotations.redaction.createFromCurrentTextSelection();
  }
  await instance.createRedactionsBySearch(/\d{3}-\d{2}-\d{4}/);
  await instance.createRedactionsBySearch("CONFIDENTIAL");
}

```

After the function runs, the document has redactions for the user’s manually selected text, plus every Social Security Number pattern and every literal CONFIDENTIAL mention. Call `applyRedactions()` once to commit them all together.

## Choosing between the selection and pattern paths

Both APIs create the same kind of redaction annotation. Use the one that matches the workflow.

| Workflow                                                           | Use                                                        |
| ------------------------------------------------------------------ | ---------------------------------------------------------- |
| User points at the content to redact                               | `createFromCurrentTextSelection()` from a selection event. |
| Document has known patterns to redact (SSNs, account numbers, IDs) | `createRedactionsBySearch(regex)` once on document load.   |
| Need to redact every literal occurrence of a string                | `createRedactionsBySearch(string)`.                        |

## Related

- [Color presets](https://www.nutrient.io/guides/web/headless/color-presets.md) — The cross-namespace pattern for reading preset colors.

- [Redaction guide](https://www.nutrient.io/guides/web/redaction.md) — The broader redaction feature reference.
---

## Related pages

- [Headless callout annotations](/guides/web/headless/callout.md)
- [Headless color presets](/guides/web/headless/color-presets.md)
- [Headless image annotations](/guides/web/headless/image.md)
- [Annotation clipboard](/guides/web/headless/clipboard.md)
- [Headless](/guides/web/headless.md)
- [Headless ink annotations](/guides/web/headless/ink.md)
- [Headless stamp annotations](/guides/web/headless/stamp.md)
- [Headless link annotations](/guides/web/headless/link.md)
- [Headless note annotations](/guides/web/headless/note.md)
- [Programmatic notes panel](/guides/web/headless/notes-panel.md)
- [Headless text annotations](/guides/web/headless/text-annotations.md)
- [Headless shape annotations](/guides/web/headless/shape.md)
- [Headless text markup](/guides/web/headless/text-markup.md)

