Edit Page Labels in a PDF

With setPageLabel, you can change the label of any page:

instance.applyOperations([
  {
    type: "setPageLabel",
    pageIndexes: [0],
    pageLabel: "New page label"
  }
]);

pageLabel is optional, and if it’s not provided, the current page label will be cleared. The operation will fail if more than one page number is provided in the pageIndexes array.

Multiple Operations

Operations can be chained in a single call to instance.applyOperations() or instance.exportPDFWithOperations(). When performing multiple operations, always keep in mind that the page indices in each operation must reference the corresponding page indices that result from performing the previous operation.

To better understand the implications of this behavior, below is a failing example. Assume the current document has six pages, which are indexed from 0 to 5:

instance.applyOperations([
  // First, remove the last two pages from the document.
  {
    type: "removePages",
    pageIndexes: [4, 5] // Remove pages 4 and 5.
  },
  // The resulting document would have four pages, indexed from `0` to `3`.
  // The next operation will throw an error, as there's no longer a page with index 5.
  {
    type: "rotatePages",
    pageIndexes: [5], // Rotate page 5.
    rotateBy: 90 // Rotate page 90 degrees clockwise.
  }
]);

On the other hand, this next example would succeed in spite of operating on a page that doesn’t exist in the original document (assume you have a six-page document again):

instance.applyOperations([
  // First, add a page at the beginning of the document.
  {
    type: "addPage",
    beforePageIndex: 0, // Add new page after page 1.
    backgroundColor: new PSPDFKit.Color({ r: 255, g: 255, b: 255 }), // Set white as the new page background color.
    pageWidth: 750,
    pageHeight: 1000,
    rotateBy: 0 // No rotation.
    // Insets are optional.
  },
  // The resulting document would have 7 pages now, indexed from `0` to `6`.
  // The next operation will succeed, as page 6 now exists.
  {
    type: "rotatePages",
    pageIndexes: [6], // Rotate page 6.
    rotateBy: 90 // Rotate page 90 degrees clockwise.
  }
]);

Exporting a PDF

After this operation is complete, you can call instance#exportPDF to get an ArrayBuffer containing the data for the final PDF.

If you need to apply this operation and export the resulting document in one step, you can provide the same argument passed to instance#applyOperations to instance#exportPDFWithOperations instead, and it’ll resolve to an ArrayBuffer containing the final PDF.