---
title: "Encrypt PDF/UA-1 with AES-256 in C#"
url: https://recursiveai.org/onepdf/cookbook/save-aes-256-encrypted-pdf-ua-1/
description: "Encrypt a tagged PDF/UA-1 with AES-256 in C#. Keep accessibility extraction enabled and save as PDF 1.7 with OnePDF."
version: "1.26.1"
---

# Encrypt PDF/UA-1 with AES-256 in C#

**OnePDF** is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/save-aes-256-encrypted-pdf-ua-1/save-aes-256-encrypted-pdf-ua-1.zip)

The example’s `encrypted.pdf` passed veraPDF’s PDF/UA-1 profile: run `verapdf -f ua1 --format text encrypted.pdf` after saving it. The code needs a tagged PDF named `accessible.pdf`: use the one in the zip or substitute your own tagged PDF.

```shell
dotnet add package RecursiveAI.OnePdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePdf;

var edit = new PdfOpenOptions
{
    AllowEditing = true
};
using var doc =
    PdfDocument.Open("accessible.pdf", edit);
var encryption = new PdfEncryptionOptions
{
    Cipher = PdfCipher.Aes256,
    UserPassword = "",
    OwnerPassword = "demo-owner",
    Permissions =
        PdfPermissions.ExtractForAccessibility
};
doc.Save("encrypted.pdf", new PdfSaveOptions
{
    PdfUA = new() { Part = PdfUAPart.Part1 },
    Encryption = PdfEncryptionAction.Set,
    NewEncryption = encryption
});
Console.WriteLine("Wrote encrypted.pdf");
```

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onepdf/cookbook/save-aes-256-encrypted-pdf-ua-1/save-aes-256-encrypted-pdf-ua-1.zip) and `dotnet run`.

```text
Wrote encrypted.pdf
```

The empty user password lets readers open the file without a prompt; the owner password controls full access. Use your own owner password outside the demo. If OnePDF finds violations it can’t repair, the PDF/UA save throws `PdfUAException`. As the [PDF/UA guide](https://recursiveai.org/onepdf/docs/pdfua/) puts it, “a clean check is not proof of conformance.”

## Variations

**Use AES-128 when a recipient requires it.** Replace the `Cipher` assignment and leave the accessibility permission in place:

```csharp
Cipher = PdfCipher.Aes128,
```

## Related recipes

- [Create an accessible PDF in C#](https://recursiveai.org/onepdf/cookbook/create-pdf-ua/)
- [Encrypt a PDF with a password](https://recursiveai.org/onepdf/cookbook/encrypt-pdf-with-password/)

## The short answer

Open a tagged PDF with editing enabled. Set `PdfUA.Part` to `Part1`, `Encryption` to `Set`, and `NewEncryption.Cipher` to `Aes256`. Include `PdfPermissions.ExtractForAccessibility` so the encryption settings don’t block extraction for assistive technology. The sample’s pass does not guarantee that another PDF will pass.

## Can a PDF/UA-1 file be encrypted?

Yes. PDF/UA-1 allows encryption provided the accessibility extraction permission stays enabled. That permission is separate from ordinary text copying; the example requests accessibility extraction without granting all permissions. It is also separate from the password needed to open a file. Here that password is empty, so the file is encrypted but isn’t a way to keep the contents confidential from someone who obtains a copy. PDF/A is an archival standard with different rules: a PDF/A file can’t be encrypted.

## Why does an AES-256 PDF/UA-1 file use PDF 1.7?

PDF/UA-1 requires a PDF 1.x header. OnePDF 1.26.1 writes PDF 1.7 with the Adobe AES-256 extension declaration in the catalog. That declaration lets a PDF 1.7 reader that supports the extension identify revision 6 encryption. OnePDF versions 1.0.0 through 1.25.2 could write a PDF 2.0 header for this combination; save files made with that combination again. Saving as PDF/UA-2 instead keeps PDF 2.0, but that doesn’t meet a requirement to deliver part 1.

## How do you check if an encrypted PDF is accessible?

Start with a tagged source whose headings, reading order, language, and any alternative text are correct. The save can make some structural repairs, but it won’t decide what an image means or invent missing tags. Check the final file, not just the unencrypted input: an automated PDF/UA check covers machine-testable rules, while a human review is still needed for the meaning and order of the content. If you set an opening password, the person reviewing the PDF also needs that password.
