> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/redaction/redaction/searchredact.md).

# Search and Redact PDF on Server/Desktop

Learn how to search for content and redact it from a PDF document using Apryse SDK API. Remove sensitive data securely with Apryse's PDFNet.Redactor. Control redaction style and more. The Apryse Serve

{% hint style="info" %}
**Requirements**

*These packages are required to use these features in production. Trial keys have unlimited access to all features*

<a href="https://apryse.com/capabilities#Redaction" class="button primary">Package: Redaction</a><a href="https://showcase.apryse.com/redaction" class="button primary">Live demo</a>
{% endhint %}

This sample code searches a PDF document for all instances of a search pattern and redacts these instances.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
const { PDFNet } = require('@pdftron/pdfnet-node');
const ApryseLicense = require('../LicenseKey/LicenseKey');

const main = async () => {
  const pattern = 'features'; // expression to search for
  const redactText = 'Top Secret'; // text to show in place of redacted content. Can be empty string.

  const doc = await PDFNet.PDFDoc.createFromURL(filename);
  const txtSearch = await PDFNet.TextSearch.create();
  const mode = PDFNet.TextSearch.Mode.e_whole_word + PDFNet.TextSearch.Mode.e_highlight; // Use whole word search

  txtSearch.setMode(mode);
  txtSearch.setPattern(pattern);

  // call Begin() method to initialize the text search.
  txtSearch.begin(doc, pattern, mode);
  const redactions = []; // array to hold redaction objects
  let result;
  // loop to find all instances of the pattern
  while ((result = await txtSearch.run()).code === PDFNet.TextSearch.ResultCode.e_found) {
    // add a redaction object based on the location of the found instance
    const highlights = result.highlights;
    await highlights.begin(doc);
    while (await highlights.hasNext()) {
      const pageNumber = await highlights.getCurrentPageNumber();
      const quadArr = await highlights.getCurrentQuads();
      for (let i = 0; i < quadArr.length; ++i) {
        const currQuad = quadArr[i];
        const x1 = Math.min(Math.min(Math.min(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
        const x2 = Math.max(Math.max(Math.max(currQuad.p1x, currQuad.p2x), currQuad.p3x), currQuad.p4x);
        const y1 = Math.min(Math.min(Math.min(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
        const y2 = Math.max(Math.max(Math.max(currQuad.p1y, currQuad.p2y), currQuad.p3y), currQuad.p4y);
        redactions.push(await PDFNet.Redactor.redactionCreate(pageNumber, (await PDFNet.Rect.init(x1, y1, x2, y2)), false, redactText));
      }
      highlights.next();
    }
  }
  const appearance = {};
  appearance.redaction_overlay = true;
  appearance.border = false;
  appearance.positive_overlay_color = await PDFNet.ColorPt.init(1, 0.2, 0.2, 0); // red
  appearance.show_redacted_content_regions = true;
  PDFNet.Redactor.redact(doc, redactions, appearance, false, false);
  doc.save('textsearch_redacted.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
}

PDFNet.runWithCleanup(main, ApryseLicense.key) // provide your license key here
  .catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
  })
  .then(function () {
    PDFNet.shutdown();
  });

```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.*;
import com.pdftron.pdf.Redactor.Redaction;
import com.pdftron.sdf.SDFDoc;
import java.util.ArrayList;
import java.util.List;

class SearchAndRedact {
    public static void main(String[] args) {
        PDFNet.initialize(PDFTronLicense.Key());
        String pattern = "features"; // expression to search for
        String redact_text = "Top Secret"; // text to show in place of redacted content. Can be empty string.

        try (PDFDoc doc = new PDFDoc(FileNames.input)) {
            TextSearch txt_search = new TextSearch();
            int mode = TextSearch.e_whole_word | TextSearch.e_highlight; // Use whole word search
            txt_search.setMode(mode);
            txt_search.setPattern(pattern);
            // call Begin() method to initialize the text search.
            txt_search.begin(doc, pattern, mode, -1, -1);

            // List to hold redaction objects
            List<Redactor.Redaction> redactions = new ArrayList<Redactor.Redaction>();
            TextSearchResult result;
            while ((result = txt_search.run()).getCode() == TextSearchResult.e_found) {
                // add a redaction object based on the location of the found instance
                Highlights highlights = result.getHighlights();
                highlights.begin(doc);
                while (highlights.hasNext()) {
                    int page_number = highlights.getCurrentPageNumber();
                    double[] quads = highlights.getCurrentQuads();
                    int quad_count = quads.length / 8;
                    for (int i = 0; i < quad_count; ++i) {
                        //assume each quad is an axis-aligned rectangle
                        int offset = 8 * i;
                        double x1 = Math.min(Math.min(Math.min(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]);
                        double x2 = Math.max(Math.max(Math.max(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]);
                        double y1 = Math.min(Math.min(Math.min(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]);
                        double y2 = Math.max(Math.max(Math.max(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]);
                        Rect rect = new Rect(x1, y1, x2, y2);
                        redactions.add(new Redactor.Redaction(page_number, rect, false, redact_text));
                    }
                    highlights.next();
                }
            }
            Redactor.Appearance appearance = new Redactor.Appearance();
            appearance.redactionOverlay = true;
            appearance.border = false;
            appearance.positiveOverlayColor = new ColorPt(1, 0.2, 0.2, 0); // red
            appearance.showRedactedContentRegions = true;
            Redaction[] redactions_array = new Redaction[redactions.size()];
            redactions.toArray(redactions_array);
            Redactor.redact(doc, redactions_array, appearance, false, false);
            doc.save(FileNames.output, SDFDoc.SaveMode.LINEARIZED, null);
        } catch (PDFNetException e) {
            System.out.println(e);
        }
        PDFNet.terminate();
    }
}

```

{% endcode %}
{% endtab %}

{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
using System;
using System.Collections;

using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;

namespace SearchAndRedactCS
{
   class Class1
   {
      private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
      static Class1() { }

      static void Main(string[] args)
      {
         PDFNet.Initialize(PDFTronLicense.Key);
         string pattern = "features"; // expression to search for
         string redact_text = "Top Secret"; // text to show in place of redacted content. Can be empty string.
         using (PDFDoc doc = new PDFDoc(FileNames.input))
         {
            try
            {

               TextSearch txt_search = new TextSearch();
               int mode = (int)(TextSearch.SearchMode.e_whole_word | TextSearch.SearchMode.e_highlight); // Use whole word search
               // call Begin() method to initialize the text search.
               txt_search.Begin(doc, pattern, mode, -1, -1);
               // List to hold redaction objects
               ArrayList redactions = new ArrayList();
               int page_num = 0;
               string result_str = "", ambient_string = "";
               Highlights highlights = new Highlights();
               while (txt_search.Run(ref page_num, ref result_str, ref ambient_string, highlights) == TextSearch.ResultCode.e_found)
               {
                  // add a redaction object based on the location of the found instance
                  highlights.Begin(doc);
                  while (highlights.HasNext())
                  {
                     int page_number = highlights.GetCurrentPageNumber();
                     double[] quads = highlights.GetCurrentQuads();
                     int quad_count = quads.Length / 8;
                     for (int i = 0; i < quad_count; ++i)
                     {
                        //assume each quad is an axis-aligned rectangle
                        int offset = 8 * i;
                        double x1 = Math.Min(Math.Min(Math.Min(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]);
                        double x2 = Math.Max(Math.Max(Math.Max(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]);
                        double y1 = Math.Min(Math.Min(Math.Min(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]);
                        double y2 = Math.Max(Math.Max(Math.Max(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]);
                        redactions.Add(new Redactor.Redaction(page_number, new Rect(x1, y1, x2, y2), false, redact_text));
                     }
                     highlights.Next();
                  }
               }
               Redactor.Appearance appearance = new Redactor.Appearance();
               appearance.RedactionOverlay = true;
               appearance.Border = false;
               appearance.PositiveOverlayColor = new ColorPt(1, 0.2, 0.2, 0); // red
               appearance.ShowRedactedContentRegions = true;
               Redactor.Redact(doc, redactions, appearance, false, false);
               doc.Save(FileNames.output, SDFDoc.SaveOptions.e_linearized);
            }
            catch (PDFNetException e)
            {
               Console.WriteLine(e.Message);
            }
            PDFNet.Terminate();
         }
      }
   }
}

```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDF redaction](/core/get-started/samples/pdfredacttest.md) Full code sample which shows how to use Apryse's `PDFNet.Redactor` to remove potentially sensitive content within PDF documents.

## About redactor

Apryse Redactor makes sure that if a portion of an image, text, or vector graphics is contained in a redaction region, that portion of the image or path data is destroyed and is not hidden with clipping or image masks. Apryse SDK API can also be used to review and remove metadata and other content that can exist in a PDF document, including XML Forms Architecture (XFA) content and Extensible Metadata Platform (XMP) content.

The redaction process in Apryse SDK consists of two steps:

**1. Content identification** A user applies redact annotations that specify the pieces or regions of content that should be removed. This example uses `PDFNet.TextSearch` to identify the content for redaction programmatically, but it can also be identified in other ways such as using `PDFNet.TextExtractor`, or interactively (e.g. using WebViewer). Up until the next step is performed, the user can see, move and redefine these annotations.

**2. Content removal** Using `PDFNet.Redactor.redact()` the user instructs Apryse SDK to apply the redact regions, after which the content in the area specified by the redact annotations is removed. The redaction function includes a number of options to control the style of the redaction overlay (including color, text, font, border, transparency, etc.)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/core/redaction/redaction/searchredact.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
