Available Operations

The following operations can be used in the POST /process API to modify documents:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
type Rotation = 0 | 90 | 180 | 270;

type AddPageConfiguration = {
  backgroundColor: string, // #RRGGBB or rgb(number, number, number).
  pageWidth: number,
  pageHeight: number,
  rotateBy: Rotation,
  insets?: [number, number, number, number]
};

type Annotation = ...; // See watermark documentation for more information.

type Range = [min, max]; // 'min' and 'max' are inclusive.
type ImportPageIndex = Array<number | Range>;

type DocumentOperation =
  | {| type: "addPage", afterPageIndex: number, ...AddPageConfiguration |}
  | {| type: "addPage", beforePageIndex: number, ...AddPageConfiguration |}
  | {| type: "duplicatePages", pageIndexes: Array |}
  | {| type: "movePages", pageIndexes: Array, afterPageIndex: number |}
  | {| type: "movePages", pageIndexes: Array, beforePageIndex: number |}
  | {| type: "rotatePages", pageIndexes: Array, rotateBy: Rotation |}
  | {| type: "keepPages", pageIndexes: Array |}
  | {| type: "removePages", pageIndexes: Array |}
  | {| type: "setPageLabel", pageIndexes: Array, pageLabel: string |}
  | {|
      type: "importDocument",
      afterPageIndex: number,
      importedPageIndexes?: ImportPageIndex,
      treatImportedDocumentAsOnePage: boolean,
      document: string
    |}
  | {|
      type: "importDocument",
      beforePageIndex: number,
      importedPageIndexes?: ImportPageIndex,
      treatImportedDocumentAsOnePage: boolean,
      document: string
    |}
  | {|
      type: "applyXfdf",
      dataFilePath: string
    |}
  | {|
      type: "applyInstantJson",
      dataFilePath: string
    |}
  | {|
      type: "performOcr",
      pageIndexes: Array,
      language: string
    |}
  | {|
      type: "flattenAnnotations"
    |}
  | {|
      type: "watermark",
      pageIndexes: Array,
      annotation: Annotation
    |};

addPage

The addPage operation allows you to add a single empty page to the document.

duplicatePages

The duplicatePages operation will duplicate all pages at the given page indices. The duplicated page will be placed directly after the original page.

movePages

The movePages operation moves the page at the specified page index to a place before or after the specified page index.

rotatePages

The rotatePages operation will rotate the specified pages the desired amount. If the page is already rotated, this will add the specified rotation, so if a page is already rotated 90 degrees and you apply a 90-degreee rotation, it’ll result in the page being rotated 180 degrees.

keepPages

The keepPages operation will remove all pages except the ones specified to be kept. So if you specify [0], only the first page of the document will be kept, and all others will be removed.

removePages

The removePages operation will remove all specified pages.

setPageLabel

The setPageLabel operation will set the label for all specified pages. This label is, for example, shown in PSPDFKit for Android and PSPDFKit for iOS when scrolling pages.

importDocument

The importDocument operation allows you to add an existing PDF into your document. It’ll be added either before or after the specified page index, depending on if afterPageIndex or beforePageIndex is used. Using the treatImportedDocumentAsOnePage option, you can make sure that as far as all follow-up operations are concerned, the imported document is only treated as a single page that makes specifying indices easier.

importedPageIndexes may be used to import specific pages or a range of pages. If this parameter is left blank, the entire document will be imported.

applyXfdf

The applyXfdf operation allows you to apply an existing XFDF file to the document. This will import all annotations found in the XFDF file and add them to the document.

applyInstantJson

The applyInstantJson operation allows you to apply an existing Instant JSON file to the document. This will import all annotations and fill the form fields with the values found in the Instant JSON.

performOcr

The performOcr operation allows you to run OCR on your document.

For a list of all languages supported by the performOcr operation, see here.

flattenAnnotations

The flattenAnnotations operation will flatten all annotations and form fields in the document, meaning they can no longer be modified.

watermark

The watermark operation allows you to add a specified annotation to all specified pages.

annotation is an Instant JSON annotation, as described here.

Example Operations Object:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  "operations": [
    {
      "type": "watermark",
      "pageIndexes": "all",
      "annotation": {
        "horizontalAlign": "left",
        "bbox": [
          510.794701986755,
          145.13907284768214,
          101.03311258278146,
          20.344370860927157
        ],
        "font": "Helvetica",
        "rotation": 0,
        "pageIndex": 0,
        "updatedAt": "2019-07-09T06:55:33.426Z",
        "verticalAlign": "top",
        "type": "pspdfkit/text",
        "opacity": 0.5,
        "text": "Text annotation",
        "fontColor": "#000000",
        "fontSize": 72,
        "isFitting": true,
        "createdAt": "2019-07-09T06:55:24.320Z",
        "v": 1,
        "name": "1a287131-0473-402e-8094-097cb49083e2"
      }
    }
  ]
}

This adds a simple free text annotation on all pages.