Interface: DocumentOperation

PSPDFKit.DocumentOperation

This object determines the operation to be performed on the document.

Available operations:

  • { type: "addPage", afterPageIndex: number, ...AddPageConfiguration }

Adds a blank page after the specified page index using the provided configuration.

type AddPageConfiguration = {
backgroundColor: PSPDFKit.Color,
pageWidth: number,
pageHeight: number,
rotateBy: 0 | 90 | 180 | 270,
insets?: PSPDFKit.Geometry.Rect
}
  • { type: "addPage", beforePageIndex: number, ...AddPageConfiguration }

Adds a blank page before the specified page index using the provided configuration.

  • { type: "keepPages", pageIndexes: Array<number> }

Removes all the pages from the document except for the pages specified in the pageIndexes array.

  • { type: "duplicatePages", pageIndexes: Array<number> }

Duplicates the pages specified in the pageIndexes array. Each duplicated page will be inserted after the page being duplicated.

  • { type: "movePages", pageIndexes: Array<number>, afterPageIndex: number }

Moves the pages specified in the pageIndexes array after the page specified.

  • { type: "movePages", pageIndexes: Array<number>, beforePageIndex: number }

Moves the pages specified in the pageIndexes array before the page specified.

  • { type: "rotatePages", pageIndexes: Array<number>, rotateBy: 0 | 90 | 180 | 270 }

Rotates the the pages specified in the pageIndexes array by the degrees indicated in rotateBy.

  • { type: "removePages", pageIndexes: Array<number> }

Removes the pages specified in the pageIndexes array.

  • { type: "importDocument", afterPageIndex: number, treatImportedDocumentAsOnePage: boolean, document: Blob | File }

Imports the provided document after the specified page index. treatImportedDocumentAsOnePage determines whether it will be treated as a single page for other document operations (e.g. a rotation) provided during the same call or not. After these operations are applied, the imported pages will behave like regular pages in the document.

Flattening and importing a document where treatImportedDocumentAsOnePage in the same operations batch is not supported and will raise an error.

Importing the same document more than once in the same operations block is not allowed with the UI in order to prevent possible user mistakes, but can be done programmatically.

  • { type: "importDocument", beforePageIndex: number, treatImportedDocumentAsOnePage: boolean, document: Blob | File }

Imports the provided document before the specified page index.

  • { type: "importDocument", beforePageIndex: number, importedPageIndexes?: ImportPageIndex, treatImportedDocumentAsOnePage: boolean, document: Blob | File }
type Range = [min, max]; // 'min' and 'max' are inclusive.
type ImportPageIndex = Array<number | Range>;

Imports the specified page indexes from the provided document before the specified page index.

  • { type: "applyInstantJson", instantJson: Object }

Applies the given Instant JSON object specified in the instantJson property. To learn about Instant JSON please refer to this guide article.

  • { type: "applyXfdf", xfdf: string, ignorePageRotation?: boolean }

Applies the given XFDF string specified in the xfdf property. To learn about XFDF please refer to this guide article.

  • { type: "flattenAnnotations", pageIndexes?: Array<number>, annotationIds?: Array<string>, noteAnnotationOpacity?: number, noteAnnotationBackgroundColor?: PSPDFKit.Color }

Flattens the annotations of the specified pages, or of all the pages if none is specified.

Flattening and importing a document where treatImportedDocumentAsOnePage in the same operations batch is not supported and will raise an error.

  • { type: "setPageLabel", pageIndexes?: Array<number>, pageLabel?: string }

Sets the page label of a given page index.

  • { type: "performOcr", pageIndexes?: Array<number> | "all", language: string }

Server only

If the OCR component is present in the license, performs OCR on the pages given with the language requested. See https://pspdfkit.com/guides/server/current/ocr/language-support/ for supported languages.

Server only.

instance.applyOperations([{
type: "performOcr",
pageIndexes: "all",
rotateBy: 90
}]);
  • { type: "applyRedactions" }

If the Redaction component is present in the license, applies any redaction annotations, redacting the page content and removing the annotations.

This operation doesn't have any option and it doesn't matter when it is executed - the redactions will always be applied when exporting the document at the end.

  • { type: "updateMetadata", metadata: { title?: string, author?: string } }

Updates Document metadata on the destination document.

Standalone only.

  • { type: "cropPages", pageIndexes?: Array<number>, cropBox: Rect }

Crops the pages of PDF document. If the pageIndexes property is undefined, the cropping operation is applied to all the pages.

instance.applyOperations([{
type: "cropPages",
pageIndexes: [1, 2],
rotateBy: new PSPDFKit.Geometry.Rect({
top: 100,
left: 100,
width: 100,
height: 100
})
}]);
    • { type: "addPageMargins", pageIndexes?: Array<number>, margins: Inset }

*** Standalone only ***

Adds margins to the pages of a PDF document. If the pageIndexes property is undefined, the new margins are applied to all the pages. Negative numbers will shrink the page.

Content and annotations will be repositioned back to the original location on the page, and other boxes (crop, bleed, trim, art) will be adjusted to encompass the same area.

instance.applyOperations([{
type: "addPageMargins",
pageIndexes: [1, 2],
rotateBy: new PSPDFKit.Geometry.Inset({
top: 100,
left: 100,
right: 100,
bottom: 100
})
}]);

Standalone only.

Example

// Rotate page 0 90 degrees clockwise
instance.applyOperations({
  type: "rotatePages",
  pageIndexes: [0],
  rotateBy: 90
});



See also