> 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/security/decrypt.md).

# Decrypt & read a PDF document on Server/Desktop

Guide to read and decrypt a PDF document with password protection. Discover how to integrate Apryse SDK into .NET Framework applications on Windows. Get a free trial with unlimited usage and support.

{% 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#Security" class="button primary">Package: Security</a><a href="https://showcase.apryse.com/password-protect" class="button primary">Live demo</a>
{% endhint %}

To read a PDF document with password protection.

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

```csharp
PDFDoc doc = new PDFDoc(filename);

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if (doc.InitStdSecurityHandler(password)) {
  // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```cpp
PDFDoc doc(filename);

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if (doc.InitStdSecurityHandler(password)) {
  // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```go
doc := NewPDFDoc(filename)

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if doc.InitStdSecurityHandler(password, len(password)){
  // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```java
PDFDoc doc = new PDFDoc(filename);

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if (doc.initStdSecurityHandler(password)) {
  // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```js
async function main() {
  const securedDoc = await PDFNet.PDFDoc.createFromURL(filename);

  // At this point MySecurityHandler callbacks will be invoked.
  // MySecurityHandler.GetAuthorizationData() should collect the password and
  // AuthorizeFailed() is called if user repeatedly enters a wrong password.
  if ((await securedDoc.initStdSecurityHandlerUString(password))) {
    // The password is correct! Document can now be used for reading and editing
  }
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

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

```kotlin
val doc = PDFDoc(filename)

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if (doc.initStdSecurityHandler(password)) {
  // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if([doc InitStdSecurityHandler: password]) {
    // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if doc.initStdSecurityHandler(password) {
    // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```php
$doc = new PDFDoc($filename);

// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if($doc->InitStdSecurityHandler($password, strlen($password))) {
  // The password is correct! Document can now be used for reading and editing
}
```

{% endcode %}
{% endtab %}

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

```python
doc = PDFDoc(filename)

# At this point MySecurityHandler callbacks will be invoked.
# MySecurityHandler.GetAuthorizationData() should collect the password and
# AuthorizeFailed() is called if user repeatedly enters a wrong password.
if doc.InitStdSecurityHandler(password, len(password)):
  # The password is correct! Document can now be used for reading and editing
```

{% endcode %}
{% endtab %}

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

```ruby
doc = PDFDoc.new(filename)

# At this point MySecurityHandler callbacks will be invoked.
# MySecurityHandler.GetAuthorizationData() should collect the password and
# AuthorizeFailed() is called if user repeatedly enters a wrong password.
if doc.InitStdSecurityHandler(password, password.length)
  # The password is correct! Document can now be used for reading and editing
end
```

{% endcode %}
{% endtab %}

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

```vb
Dim doc As PDFDoc = New PDFDoc(filename)

' At this point MySecurityHandler callbacks will be invoked.
' MySecurityHandler.GetAuthorizationData() should collect the password and
' AuthorizeFailed() is called if user repeatedly enters a wrong password.
If doc.InitStdSecurityHandler(password) Then
  ' The password is correct! Document can now be used for reading and editing
End If
```

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

[Encrypt and Decrypt PDF Files](/core/get-started/samples/enctest.md) Full sample code which illustrates some of our encryption support. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

## About reading a secured document

Apryse SDK fully supports the reading of secured and encrypted PDF documents. To test whether a document requires a password, check the return value of `PDFDoc.InitSecurityHandler()` as shown above. Because `InitSecurityHandler()` doesn't have any side effects on documents that are not encrypted you should always invoke this method, or `InitStdSecurityHandler()`, after constructing a document.

If a document doesn't require authentication data (such as a user password) in order to view its content, `InitSecurityHandler()` is enough to work with encrypted documents. If, on the other hand, the document requires a password, `InitStdSecurityHandler` allows you to provide one.

After the document's security handler is initialized, you can access it using the `doc.GetSecurityHandler()` method. You can edit permissions and authorization data on an existing handler, or set a completely new security handler using the `doc.SetSecurityHandler(handler)` method.

## Removing security from a document

To **remove** PDF security, call the `RemoveSecurity()` method:

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

```csharp
PDFDoc doc = new PDFDoc(filename);
doc.InitSecurityHandler();
doc.RemoveSecurity();
```

{% endcode %}
{% endtab %}

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

```cpp
PDFDoc doc(filename);
doc.InitSecurityHandler();
doc.RemoveSecurity();
```

{% endcode %}
{% endtab %}

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

```go
doc := NewPDFDoc(filename)
doc.InitSecurityHandler()
doc.RemoveSecurity()
```

{% endcode %}
{% endtab %}

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

```java
PDFDoc doc = new PDFDoc(filename);
doc.initSecurityHandler();
doc.removeSecurity();
```

{% endcode %}
{% endtab %}

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

```js
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);
  await doc.initSecurityHandler();
  doc.removeSecurity();
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

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

```kotlin
val doc = PDFDoc(filename)
doc.initSecurityHandler()
doc.removeSecurity()
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
[doc InitSecurityHandler];
[doc RemoveSecurity];
```

{% endcode %}
{% endtab %}

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

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
doc.initSecurityHandler()
doc.removeSecurity()
```

{% endcode %}
{% endtab %}

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

```php
$doc = new PDFDoc($filename);
$doc->InitSecurityHandler();
$doc->RemoveSecurity();
```

{% endcode %}
{% endtab %}

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

```python
doc = PDFDoc(filename)
doc.InitSecurityHandler()
doc.RemoveSecurity()
```

{% endcode %}
{% endtab %}

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

```ruby
doc = PDFDoc.new(filename)
doc.InitSecurityHandler()
doc.RemoveSecurity()
```

{% endcode %}
{% endtab %}

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

```vb
Dim doc As PDFDoc = New PDFDoc(filename)
doc.InitSecurityHandler()
doc.RemoveSecurity()
```

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

A document may have both a current and a new security handler associated with it because a PDF document is not fully loaded in memory and decrypted when it is loaded. To fully decrypt the document, even after applying a new security handler, the original security handler is still required.

## PDF security permissions

The security mechanism for the high-level document works in the same way as with an SDF document. To secure a document, use the `PDFDoc.SetSecurityHandler()` method. To open a secured document, call `PDFDoc.InitSecurityHandler()`. To open a document with a password, call `PDFDoc.InitStdSecurityHandler()`.

The following table lists security permissions available through the standard security handler:

| Permission          | Description                                                                                                                                                                                                                                          |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| e\_owner            | All permissions are granted.                                                                                                                                                                                                                         |
| e\_doc\_open        | A permission to open a document.                                                                                                                                                                                                                     |
| e\_doc\_modify      | Modify the contents of the document.                                                                                                                                                                                                                 |
| e\_print            | Print the document.                                                                                                                                                                                                                                  |
| e\_print\_high      | Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this permission is not set, printing is limited to a low level representation of the appearance, possibly of degraded quality. |
| e\_extract\_content | Copy or otherwise extract text and graphics from the document.                                                                                                                                                                                       |
| e\_mod\_annot       | Add or modify text annotations, fill in interactive form fields.                                                                                                                                                                                     |
| e\_fill\_forms      | Fill in existing interactive form fields (including signature fields).                                                                                                                                                                               |
| e\_access\_support  | Extract text and graphics (in support of accessibility to disabled users or for other purposes).                                                                                                                                                     |
| e\_assemble\_doc    | Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if e\_doc\_modify is not set.                                                                                                                 |


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/security/decrypt.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
