New: PDF/A-3b conversion with a compliance report included.See Docs

Make a PDF Unsearchable in C# Using PDF.co

Convert an existing PDF into an image-only PDF by rasterizing its pages. The output retains the document’s appearance without its selectable text layer.

This is not a substitute for encryption or redaction: OCR software can still recognize text in the images.

Step 1: Create a Console Application

You’ll need .NET 8 or later, a PDF.co API key, and an accessible URL for a PDF.

Create a project:

dotnet new console -n MakePdfUnsearchable
cd MakePdfUnsearchable

For this example, use a small PDF containing selectable text. If your PDF is stored locally, upload it to PDF.co’s file storage and copy its file link.

Step 2: Add the Complete Code

Replace Program.cs with the following code. No additional NuGet packages are required.

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;

const string ApiKey = "YOUR_PDFCO_API_KEY";
const string SourceUrl = "YOUR_SOURCE_PDF_URL";
const string Password = "";

string destinationFile = Path.Combine(
    Environment.CurrentDirectory,
    "unsearchable.pdf"
);

try
{
    using var client = new HttpClient
    {
        Timeout = TimeSpan.FromMinutes(2)
    };

    using var request = new HttpRequestMessage(
        HttpMethod.Post,
        "https://api.pdf.co/v1/pdf/makeunsearchable"
    )
    {
        Content = JsonContent.Create(new
        {
            url = SourceUrl,
            name = Path.GetFileName(destinationFile),
            password = Password,
            pages = "",
            @async = false
        })
    };

    request.Headers.Add("x-api-key", ApiKey);

    using var response = await client.SendAsync(request);
    string responseText = await response.Content.ReadAsStringAsync();

    if (!response.IsSuccessStatusCode)
    {
        throw new HttpRequestException(
            $"API request failed (HTTP {(int)response.StatusCode}): " +
            responseText
        );
    }

    using var document = JsonDocument.Parse(responseText);
    JsonElement result = document.RootElement;

    if (result.TryGetProperty("error", out JsonElement error) &&
        error.GetBoolean())
    {
        string message = result.TryGetProperty(
            "message", out JsonElement details
        )
            ? details.GetString() ?? "Conversion failed."
            : "Conversion failed.";

        throw new InvalidOperationException(message);
    }

    string resultUrl = result.GetProperty("url").GetString()
        ?? throw new InvalidOperationException("Missing output URL.");

    using var download = await client.GetAsync(resultUrl);
    download.EnsureSuccessStatusCode();

    byte[] pdfBytes = await download.Content.ReadAsByteArrayAsync();
    await File.WriteAllBytesAsync(destinationFile, pdfBytes);

    Console.WriteLine($"Saved PDF to: {destinationFile}");
}
catch (Exception exception)
{
    Console.Error.WriteLine(exception.Message);
    Environment.ExitCode = 1;
}

Replace ApiKey and SourceUrl with your values. If the PDF requires a password, enter it in Password; otherwise, leave it empty.

The request uses:

  • url: The source PDF’s accessible file link.
  • name: The generated PDF’s filename.
  • password: The source PDF’s password, if required.
  • pages: An empty string processes all pages.
  • async: false waits for the conversion response.

See the Make Text Unsearchable API reference for parameter details.

Step 3: Run the Application

From the project folder, run:

dotnet run

The application converts the PDF and downloads the result as unsearchable.pdf in your working directory.

Step 4: Check the Output

Open the downloaded PDF and confirm that its pages look correct. In a reader without automatic OCR, the original text should no longer be selectable or searchable.

The source PDF remains unchanged.

Related Tutorials

See Related Tutorials