Skip to main content

Redacting in PDF

Use the Automation API to find sensitive text in a PDF document and permanently redact all matching occurrences from an external interface — enter a search term, set options, preview redaction marks, and apply them.

info

The document opens as a PDF. Your code calls connector.callCommand() to execute Document Builder API methods that search text and create redaction annotations. The redaction is applied permanently when the user confirms.

REDACT IN PDF
Enter text and click Find to locate and mark it for redaction in the PDF.

How it works

  1. When the user enters text and clicks Find, the connector.callCommand() method is used to execute Document Builder API code inside the editor. The code calls SearchAndRedact on the document, which searches for all matches and creates redaction annotations for each one. The method returns an array of ApiRedactAnnotation objects:

    Asc.scope.redactParams = { text, matchCase, wholeWords };

    connector.callCommand(() => {
    const params = Asc.scope.redactParams;
    const doc = Api.GetDocument();
    const annots = doc.SearchAndRedact({
    text: params.text,
    matchCase: params.matchCase,
    wholeWords: params.wholeWords,
    });

    const ids = [];
    if (annots && annots.length > 0) {
    for (let i = 0; i < annots.length; i++) {
    ids.push(annots[i].GetInternalId());
    }
    }

    return { count: ids.length, ids };
    }, (result) => {
    // Display the number of redaction marks in the external panel
    });
  2. The external panel shows the total number of redaction marks and their status. When the user clicks Apply redaction, the ApplyRedact method is called to permanently remove the redacted content from the PDF:

    connector.callCommand(() => {
    const doc = Api.GetDocument();
    doc.ApplyRedact();
    });
  3. When the user clicks Clear marks instead, the redaction annotations are removed without applying them. The code iterates all pages, retrieves annotations with GetAllAnnots, and deletes the matching redaction annotations using Delete:

    Asc.scope.annotIds = redactAnnotIds;

    connector.callCommand(() => {
    const ids = Asc.scope.annotIds;
    const doc = Api.GetDocument();
    const pageCount = doc.GetPagesCount();
    for (let i = 0; i < pageCount; i++) {
    const page = doc.GetPage(i);
    const annots = page.GetAllAnnots();
    for (let j = annots.length - 1; j >= 0; j--) {
    for (let k = 0; k < ids.length; k++) {
    if (annots[j].GetInternalId() === ids[k]) {
    annots[j].Delete();
    break;
    }
    }
    }
    }
    });
note

Please note that the connector is available only for ONLYOFFICE Docs Developer.

The connector is a premium feature available at an extra cost. See ONLYOFFICE Docs Developer for pricing details, or contact our sales team at sales@onlyoffice.com to request a quote.