# Einschränkungen zu einem PDF hinzufügen

Legen Sie die Berechtigungs-Flags eines PDFs fest und steuern Sie damit Kopieren, Drucken, Bearbeiten und das Ausfüllen von Formularen, hinter einem Besitzerpasswort.

Wenden Sie Berechtigungseinschränkungen auf ein PDF an, um Kopieren, Drucken,
Bearbeiten, das Ausfüllen von Formularen und mehr zu begrenzen, durchgesetzt
von einem `owner_password`. Einschränkungen sind Berechtigungs-Flags, im
Unterschied zum [Hinzufügen eines Passworts](/docs/api/add-password-to-pdf),
das nur das Passwort festlegt, das zum Öffnen der Datei nötig ist; Sie können
hier zusätzlich ein `user_password` setzen, um beides zu verlangen. Die API ist
*stateless*: Ihr Dokument wird in der Region verarbeitet und niemals
gespeichert.

## Endpoint

<Endpoint method="POST" path="/v1/add_restrictions" />

In allen Regionen verfügbar. Hinweise zu Routing und Datenresidenz finden Sie
unter [Regionen und Datenresidenz](/docs/api/regions-and-data-residency).

| Region                 | URL                                                   |
| ---------------------- | ----------------------------------------------------- |
| Global                 | `https://api.pdfblocks.com/v1/add_restrictions`       |
| USA                    | `https://us.api.pdfblocks.com/v1/add_restrictions`    |
| HIPAA USA              | `https://hipaa.api.pdfblocks.com/v1/add_restrictions` |
| Europäische Union      | `https://eu.api.pdfblocks.com/v1/add_restrictions`    |
| Vereinigtes Königreich | `https://uk.api.pdfblocks.com/v1/add_restrictions`    |
| Kanada                 | `https://ca.api.pdfblocks.com/v1/add_restrictions`    |
| Australien             | `https://au.api.pdfblocks.com/v1/add_restrictions`    |
| Japan                  | `https://jp.api.pdfblocks.com/v1/add_restrictions`    |
| Indien                 | `https://in.api.pdfblocks.com/v1/add_restrictions`    |
| Brasilien              | `https://br.api.pdfblocks.com/v1/add_restrictions`    |

## Authentifizierung

Authentifizieren Sie jede Anfrage mit Ihrem geheimen API-Schlüssel im Header
`X-API-Key`, über HTTPS. Schlüssel erstellen und verwalten Sie im
[Dashboard](https://dashboard.pdfblocks.com). Einzelheiten finden Sie unter
[Authentifizierung](/docs/api/authentication).

## Anfrage

Der Endpoint nimmt einen Anfragetext vom Typ `multipart/form-data` entgegen.

<ParamField name="file" type="file" required>
  Das PDF-Eingabedokument.
</ParamField>

<ParamField name="owner_password" type="string" required>
  Das Besitzerpasswort. 4 bis 32 druckbare ASCII-Zeichen. Es öffnet das Dokument
  und ändert dessen [Berechtigungs-Flags](#berechtigungs-flags).
</ParamField>

<ParamField name="user_password" type="string">
  Ein optionales Benutzerpasswort. 4 bis 32 druckbare ASCII-Zeichen. Ist es
  gesetzt, lässt sich das Dokument ohne dieses Passwort nicht öffnen. Lassen Sie
  es weg oder leer, damit jeder das Dokument öffnen kann.
</ParamField>

<ParamField name="encryption_algorithm" type="string" default="AES-128">
  Der Verschlüsselungsalgorithmus. Entweder `AES-128` oder `AES-256`.
</ParamField>

<ParamField name="allow_copy_content" type="boolean" default="true">
  Erlaubt das Kopieren von Text und Bildern in die Zwischenablage.
</ParamField>

<ParamField name="allow_change_content" type="boolean" default="true">
  Erlaubt das Ändern des Dokumentinhalts.
</ParamField>

<ParamField name="allow_print" type="boolean" default="true">
  Erlaubt das Drucken des Dokuments.
</ParamField>

<ParamField name="allow_print_high_resolution" type="boolean" default="true">
  Erlaubt das Drucken in hoher Auflösung.
</ParamField>

<ParamField name="allow_comment_and_fill_form" type="boolean" default="true">
  Erlaubt das Hinzufügen, Bearbeiten und Ändern von Anmerkungen sowie das
  Ausfüllen von Formularfeldern.
</ParamField>

<ParamField name="allow_fill_form" type="boolean" default="true">
  Erlaubt das Ausfüllen von Formularfeldern.
</ParamField>

<ParamField name="allow_assemble_document" type="boolean" default="true">
  Erlaubt das Zusammenstellen oder Bearbeiten des Dokuments (Seiten einfügen,
  löschen, drehen).
</ParamField>

<ParamField name="allow_accessibility" type="boolean" default="true">
  Erlaubt es Software für Barrierefreiheit, den Text und die Bilder des
  Dokuments zu lesen.
</ParamField>

### Berechtigungs-Flags

Jedes Flag regelt eine Aktion, die ein Leser ausführen kann. Übergeben Sie das
Flag als booleschen Wert; setzen Sie es auf `false`, um diese Berechtigung
abzuschalten.

| Flag                          | Standard | Auf `false` setzen, um…                                      |
| ----------------------------- | -------- | ------------------------------------------------------------ |
| `allow_copy_content`          | `true`   | das Kopieren von Text und Bildern in die Zwischenablage zu blockieren |
| `allow_change_content`        | `true`   | das Ändern des Dokumentinhalts zu blockieren                 |
| `allow_print`                 | `true`   | das Drucken des Dokuments zu blockieren                      |
| `allow_print_high_resolution` | `true`   | das Drucken in hoher Auflösung zu blockieren                 |
| `allow_comment_and_fill_form` | `true`   | das Hinzufügen, Bearbeiten und Ändern von Anmerkungen sowie das Ausfüllen von Formularfeldern zu blockieren |
| `allow_fill_form`             | `true`   | das Ausfüllen von Formularfeldern zu blockieren              |
| `allow_assemble_document`     | `true`   | das Zusammenstellen oder Bearbeiten des Dokuments (Seiten einfügen, löschen, drehen) zu blockieren |
| `allow_accessibility`         | `true`   | Software für Barrierefreiheit am Lesen des Dokuments zu hindern |

<Note>
  Jedes Flag steht standardmäßig auf `true`, es ist also nichts eingeschränkt,
  solange Sie es nicht verlangen. Senden Sie nur die Flags, die Sie abschalten
  wollen. Durchgesetzt werden die Einschränkungen vom `owner_password`: Ein
  Leser, der es angibt, kann die Berechtigungen ändern. Fügen Sie nur dann ein
  `user_password` hinzu, wenn Sie zusätzlich ein Passwort allein zum Öffnen der
  Datei verlangen wollen. Zum vollständigen Lebenszyklus siehe [Dokumente
  schützen](/docs/api/protecting-documents).
</Note>

## Beispiele

Sperren Sie ein Dokument so, dass es weder kopiert noch gedruckt werden kann, es
aber jeder öffnen kann:

<CodeGroup>

```bash title="cURL"
curl https://api.pdfblocks.com/v1/add_restrictions \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F owner_password='s3cr3t-owner' \
  -F allow_copy_content=false \
  -F allow_print=false \
  -F allow_print_high_resolution=false \
  -o restricted.pdf
```

```python title="Python"
# pip install requests
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/add_restrictions',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={
            'owner_password': 's3cr3t-owner',
            'allow_copy_content': 'false',
            'allow_print': 'false',
            'allow_print_high_resolution': 'false',
        },
    )

response.raise_for_status()
with open('restricted.pdf', 'wb') as output:
    output.write(response.content)
```

```javascript title="Node.js"
// Node.js 18+
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('owner_password', 's3cr3t-owner');
body.set('allow_copy_content', 'false');
body.set('allow_print', 'false');
body.set('allow_print_high_resolution', 'false');

const response = await fetch('https://api.pdfblocks.com/v1/add_restrictions', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('restricted.pdf', Buffer.from(await response.arrayBuffer()));
```

```php title="PHP"
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_restrictions');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'owner_password' => 's3cr3t-owner',
        'allow_copy_content' => 'false',
        'allow_print' => 'false',
        'allow_print_high_resolution' => 'false',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('restricted.pdf', $pdf);
}
```

```ruby title="Ruby"
# gem install http
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/add_restrictions', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    owner_password: 's3cr3t-owner',
    allow_copy_content: 'false',
    allow_print: 'false',
    allow_print_high_resolution: 'false',
  })

File.write('restricted.pdf', response.body) if response.status.success?
```

```go title="Go"
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("owner_password", "s3cr3t-owner")
	form.WriteField("allow_copy_content", "false")
	form.WriteField("allow_print", "false")
	form.WriteField("allow_print_high_resolution", "false")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_restrictions", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("restricted.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
```

```csharp title="C#"
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("s3cr3t-owner"), "owner_password" },
    { new StringContent("false"), "allow_copy_content" },
    { new StringContent("false"), "allow_print" },
    { new StringContent("false"), "allow_print_high_resolution" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "restricted.pdf", await response.Content.ReadAsByteArrayAsync());
```

</CodeGroup>

## Antwort

Bei Erfolg lautet die Antwort `200 OK` und enthält das eingeschränkte PDF als
Antworttext:

```http
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 48213
```

Die Ausgabe ist dasselbe Dokument, auf das die angeforderten Berechtigungen
angewendet wurden. Seine Seiten und sein Inhalt sind unverändert. Schreiben Sie
den Antworttext direkt in eine Datei, wie die Beispiele oben es tun; bei uns
wird nichts gespeichert.

## Fehler

Fehlgeschlagene Anfragen liefern einen Antworttext vom Typ
`application/problem+json`. Am häufigsten ist bei diesem Endpoint der Status
`400`, der zurückgegeben wird, wenn ein Parameter ungültig ist (zum Beispiel ein
`owner_password`, das nicht aus 4 bis 32 druckbaren ASCII-Zeichen besteht);
dabei nennt das Objekt `errors` jedes betroffene Feld:

```json
{
  "type": "https://www.pdfblocks.com/docs/api/v1/error/400",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "owner_password": ["The field owner_password must match the regular expression '^[\\x20-\\x7e]{4,32}$'."]
  }
}
```

Ein fehlender oder ungültiger `X-API-Key` gibt einen `401` zurück. Alle
Statuscodes und die vollständige Form der Antwort finden Sie unter
[Fehler](/docs/api/errors).

## Rezepte

Häufige Varianten. Klappen Sie eine auf, um sie in allen Sprachen zu sehen.

<AccordionGroup>

<Accordion title="Schreibgeschützt: Kopieren, Bearbeiten und Drucken blockieren">

<CodeGroup>

```bash title="cURL"
curl https://api.pdfblocks.com/v1/add_restrictions \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F owner_password='s3cr3t-owner' \
  -F allow_copy_content=false \
  -F allow_change_content=false \
  -F allow_print=false \
  -F allow_print_high_resolution=false \
  -o readonly.pdf
```

```python title="Python"
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/add_restrictions',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={
            'owner_password': 's3cr3t-owner',
            'allow_copy_content': 'false',
            'allow_change_content': 'false',
            'allow_print': 'false',
            'allow_print_high_resolution': 'false',
        },
    )

response.raise_for_status()
with open('readonly.pdf', 'wb') as output:
    output.write(response.content)
```

```javascript title="Node.js"
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('owner_password', 's3cr3t-owner');
body.set('allow_copy_content', 'false');
body.set('allow_change_content', 'false');
body.set('allow_print', 'false');
body.set('allow_print_high_resolution', 'false');

const response = await fetch('https://api.pdfblocks.com/v1/add_restrictions', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('readonly.pdf', Buffer.from(await response.arrayBuffer()));
```

```php title="PHP"
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_restrictions');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'owner_password' => 's3cr3t-owner',
        'allow_copy_content' => 'false',
        'allow_change_content' => 'false',
        'allow_print' => 'false',
        'allow_print_high_resolution' => 'false',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('readonly.pdf', $pdf);
}
```

```ruby title="Ruby"
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/add_restrictions', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    owner_password: 's3cr3t-owner',
    allow_copy_content: 'false',
    allow_change_content: 'false',
    allow_print: 'false',
    allow_print_high_resolution: 'false',
  })

File.write('readonly.pdf', response.body) if response.status.success?
```

```go title="Go"
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("owner_password", "s3cr3t-owner")
	form.WriteField("allow_copy_content", "false")
	form.WriteField("allow_change_content", "false")
	form.WriteField("allow_print", "false")
	form.WriteField("allow_print_high_resolution", "false")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_restrictions", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("readonly.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
```

```csharp title="C#"
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("s3cr3t-owner"), "owner_password" },
    { new StringContent("false"), "allow_copy_content" },
    { new StringContent("false"), "allow_change_content" },
    { new StringContent("false"), "allow_print" },
    { new StringContent("false"), "allow_print_high_resolution" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "readonly.pdf", await response.Content.ReadAsByteArrayAsync());
```

</CodeGroup>

</Accordion>

<Accordion title="Passwort zum Öffnen verlangen, mit AES-256">

<CodeGroup>

```bash title="cURL"
curl https://api.pdfblocks.com/v1/add_restrictions \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F owner_password='s3cr3t-owner' \
  -F user_password='open-me-2024' \
  -F encryption_algorithm=AES-256 \
  -o protected.pdf
```

```python title="Python"
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/add_restrictions',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={
            'owner_password': 's3cr3t-owner',
            'user_password': 'open-me-2024',
            'encryption_algorithm': 'AES-256',
        },
    )

response.raise_for_status()
with open('protected.pdf', 'wb') as output:
    output.write(response.content)
```

```javascript title="Node.js"
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('owner_password', 's3cr3t-owner');
body.set('user_password', 'open-me-2024');
body.set('encryption_algorithm', 'AES-256');

const response = await fetch('https://api.pdfblocks.com/v1/add_restrictions', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('protected.pdf', Buffer.from(await response.arrayBuffer()));
```

```php title="PHP"
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_restrictions');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'owner_password' => 's3cr3t-owner',
        'user_password' => 'open-me-2024',
        'encryption_algorithm' => 'AES-256',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('protected.pdf', $pdf);
}
```

```ruby title="Ruby"
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/add_restrictions', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    owner_password: 's3cr3t-owner',
    user_password: 'open-me-2024',
    encryption_algorithm: 'AES-256',
  })

File.write('protected.pdf', response.body) if response.status.success?
```

```go title="Go"
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("owner_password", "s3cr3t-owner")
	form.WriteField("user_password", "open-me-2024")
	form.WriteField("encryption_algorithm", "AES-256")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_restrictions", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("protected.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
```

```csharp title="C#"
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("s3cr3t-owner"), "owner_password" },
    { new StringContent("open-me-2024"), "user_password" },
    { new StringContent("AES-256"), "encryption_algorithm" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "protected.pdf", await response.Content.ReadAsByteArrayAsync());
```

</CodeGroup>

</Accordion>

</AccordionGroup>

## Verwandte Aktionen

<CardGroup cols={2}>

<Card title="Einschränkungen entfernen" href="/docs/api/remove-restrictions-from-pdf">
  Diese Berechtigungs-Flags zurücksetzen.
</Card>

<Card title="Passwort hinzufügen" href="/docs/api/add-password-to-pdf">
  Ein Passwort allein zum Öffnen der Datei verlangen.
</Card>

<Card title="Passwort entfernen" href="/docs/api/remove-password-from-pdf">
  Ein passwortgeschütztes PDF entschlüsseln.
</Card>

<Card title="Signaturen entfernen" href="/docs/api/remove-signatures-from-pdf">
  Signaturen vor der erneuten Verarbeitung entfernen.
</Card>

</CardGroup>
