Skip to content

Page organization ​

Page organization adjusts the page structure of a PDF document, including inserting, deleting, moving, copying, rotating, extracting, merging, and replacing pages, as well as adjusting page size and page boxes. Foxit PDF SDK for Web exposes these capabilities primarily through PDFDoc.

If you use UIExtension, some page organization features are also available through the thumbnail panel or related UI components. This topic focuses on page organization through the API.

API overview ​

TaskAPI
Get page informationgetPageCount(), getPageByIndex(), PDFDoc.getAllBoxesByPageIndex(); for a single page use PDFPage.getWidth() / getHeight()
Get single-page informationPDFPage.getIndex(), getWidth(), getHeight(), getPageBox(), getAllBoxes(), isCropped(), isVirtual()
Insert blank pagesinsertPage(), insertBlankPages()
Import pages from a PDF fileinsertPages()
Merge pages from an open documentmergePDFDoc(); for multiple sources, call repeatedly on the target document or use extractPages() + insertPages()
Delete pagesremovePage(), removePages()
Move pagesmovePageTo(), movePagesTo()
Copy pagesUse extractPages() to obtain PDF data, then insertPages() at the target position (same or another document)
Replace pagesAfter removePage() / removePages(), use insertPages() or mergePDFDoc() to write source pages
Extract pagesextractPages()
Export pages to another documentAfter extractPages(), call insertPages() on the target PDFDoc (or save to a file and merge when opened)
Rotate pagesPDFDoc.rotatePages(), PDFPage.setRotation(), PDFPage.getRotation(), PDFPage.getRotationAngle()
Page size and page boxesPDFDoc.setPagesBox(), PDFDoc.getAllBoxesByPageIndex(), PDFPage.setPageSize(), PDFPage.getPageBox(), PDFPage.getAllBoxes()

Get the current document ​

javascript
const pdfViewer = await pdfui.getPDFViewer();
const pdfDoc = pdfViewer.getCurrentPDFDoc();

if (!pdfDoc) {
    throw new Error('No document is currently open');
}

Page ranges are usually represented as two-dimensional arrays. [[0]] means page 1, [[2, 4]] means pages 3 through 5, and [[0], [2, 4]] means page 1 plus pages 3 through 5. Page indexes start at 0.

Get page information ​

javascript
const pageCount = pdfDoc.getPageCount();
const page = await pdfDoc.getPageByIndex(0);

// Logical size of a single page (commonly used where consistent with getWidth / getHeight)
const width = page.getWidth();
const height = page.getHeight();

// Page boxes for a given page (media box, crop box, etc.)
const boxes = await pdfDoc.getAllBoxesByPageIndex(0);
console.log(boxes.mediaBox, boxes.cropBox);

After you obtain a PDFPage, you can also read single-page information and page boxes directly:

javascript
const pageIndex = page.getIndex();
const w = page.getWidth();
const h = page.getHeight();
// Read a specific page box: getPageBox(boxType), where boxType is a Box_Type enum value; see API Reference
const allBoxes = await page.getAllBoxes();
const isCropped = page.isCropped();
const isVirtual = page.isVirtual();

Virtual pages are generally unsuitable for page organization, creating form widgets, redaction, and other operations that modify page content.

Insert blank pages ​

PDFDoc.insertPage(pageIndex, width, height) inserts a blank page at the specified position. Page dimensions are in points; 1 inch equals 72 points.

javascript
// Insert one A4-sized blank page before page 1.
await pdfDoc.insertPage(0, 595, 842);

To insert multiple blank pages in batch, use insertBlankPages(pageRange, width, height).

javascript
// Insert multiple blank pages.
await pdfDoc.insertBlankPages([[1, 3]], 595, 842);

Import pages from another PDF ​

PDFDoc.insertPages(options) imports pages from another PDF file.

javascript
await pdfDoc.insertPages({
    destIndex: 1,
    file,
    password: '',
    startIndex: 0,
    endIndex: 2
});

Common parameters:

ParameterDescription
destIndexInsert position in the current document.
fileSource PDF file; can be File, Blob, ArrayBuffer, and so on.
passwordOpen password for the source PDF; may be empty.
startIndexStart page index in the source PDF.
endIndexEnd page index in the source PDF.
flagsImport options; 0 for normal import, 1 for import with layers.
layerNameLayer name when importing with layers.

Merge pages from an open document ​

If the source document is already loaded as a PDFDoc, use mergePDFDoc(options) to merge its pages into the current document.

javascript
const sourceDoc = await pdfViewer.loadPDFDocByFile(sourceFile, {
    password: ''
});

await pdfDoc.mergePDFDoc({
    doc: sourceDoc,
    insertIndex: pdfDoc.getPageCount(),
    pages: [0, 1],
    layerName: ''
});

If pages is empty or unset, all pages from the source document are imported. When insertIndex is less than or equal to 0, pages are inserted at the beginning; when unset or greater than or equal to the current page count, they are inserted at the end.

To merge pages from multiple open documents in sequence, call mergePDFDoc() multiple times on the current document (each time with a different doc and pages), or obtain PDF binary via getFile() / extractPages() on the source and use insertPages() on the target.

Delete pages ​

javascript
// Delete page 1.
await pdfDoc.removePage(0);

// Delete multiple page ranges.
await pdfDoc.removePages([[1, 3]]);

Move pages ​

javascript
// Move page 1 to the position of page 4.
await pdfDoc.movePageTo(0, 3);

// Move multiple page ranges to the target position.
await pdfDoc.movePagesTo([[0, 2]], 5);

Copy and paste pages ​

The SDK does not yet provide dedicated clipboard-style copy/paste APIs. To copy selected pages to another position in the same document, call extractPages() to obtain a temporary PDF containing only those pages, then insertPages() on the current document.

javascript
// Copy page 1 before page 3 (destIndex = 2). The temporary PDF from extractPages contains only the extracted pages.
const extracted = await pdfDoc.extractPages([[0]]);
const blob = new Blob([extracted], { type: 'application/pdf' });

await pdfDoc.insertPages({
    destIndex: 2,
    file: blob,
    password: '',
    startIndex: 0,
    endIndex: 0
});

When extracting multiple pages, the temporary PDF page count is endIndex - startIndex + 1 for insertPages; you can call loadPDFDocByFile on extracted and read getPageCount(), or see extractPages / insertPages in the API Reference. For cross-document copy, call insertPages() on the target PDFDoc (with the source file or the Blob from the step above), or use mergePDFDoc() as described earlier.

Rotate pages ​

PDFDoc.rotatePages(pageRange, rotation) updates page rotation in the PDF document, unlike PDFViewer.rotateTo(), which only changes the view.

javascript
// Rotate pages 1 through 3 by 90 degrees.
await pdfDoc.rotatePages([[0, 2]], 1);

rotation uses the SDK page rotation enum. Common values are 0, 1, 2, and 3, corresponding to 0°, 90°, 180°, and 270°.

If you already have a PDFPage, you can set or read rotation for that page directly.

javascript
const page = await pdfDoc.getPageByIndex(0);

await page.setRotation(1);

const rotation = page.getRotation();
const rotationAngle = page.getRotationAngle();

Extract pages ​

extractPages(pageRange) extracts the specified pages into one new PDF document stream (type per API Reference, usually ArrayBuffer). It does not modify the current document.

javascript
const extracted = await pdfDoc.extractPages([[0, 2]]);
const extractedFile = new Blob([extracted], {
    type: 'application/pdf'
});

Export pages to another document ​

To export selected pages from the current document into another open PDFDoc, call extractPages() first, then insertPages() on the target document (inserting the page range from the temporary PDF at the desired position).

javascript
// Extract pages 1–2 (indexes 0, 1) from the current document and append to destDoc
const extracted = await pdfDoc.extractPages([[0, 1]]);
const blob = new Blob([extracted], { type: 'application/pdf' });

await destDoc.insertPages({
    destIndex: destDoc.getPageCount(),
    file: blob,
    password: '',
    startIndex: 0,
    endIndex: 1
});

Repeat the insert logic for each target destDoc when writing to multiple documents.

Replace pages ​

The SDK does not yet provide a dedicated in-place replace API. Combine delete then insert or insert then delete—for example, replace a consecutive range in the current document with pages from a source PDF.

javascript
// Replace page 1 of the current document with page 1 from the source file: remove then insert
await pdfDoc.removePage(0);
await pdfDoc.insertPages({
    destIndex: 0,
    file: sourceFile,
    password: '',
    startIndex: 0,
    endIndex: 0
});

If the source is an open PDFDoc, obtain a Blob/File via getFile() and call insertPages(), or use the extractPages + insertPages flow above.

Set page size and page boxes ​

setPagesBox(options) adjusts page size, content offset, and page boxes. It is commonly used to crop pages, change page dimensions, or remove white margins.

javascript
await pdfDoc.setPagesBox({
    indexes: [0, 1],
    width: 600,
    height: 792,
    offsetX: 40,
    offsetY: 40,
    boxes: {
        cropBox: {
            left: 0,
            bottom: 0,
            right: 600,
            top: 792
        }
    }
});

Read page box information for a given page:

javascript
const boxes = await pdfDoc.getAllBoxesByPageIndex(0);

console.log(boxes.mediaBox, boxes.cropBox);

To remove white margins only, use removeWhiteMargin.

javascript
await pdfDoc.setPagesBox({
    indexes: [0],
    removeWhiteMargin: true
});

Use PDFPage.getPageBox(boxType) and getAllBoxes() to read page boxes on a single page; use PDFDoc.setPagesBox() to modify crop box and other page boxes (pass indexes: [pageIndex] for one page). To adjust logical width and height only, use PDFPage.setPageSize(width, height).

javascript
const page = await pdfDoc.getPageByIndex(0);
const pageIndex = page.getIndex();

const allBoxes = await page.getAllBoxes();
console.log(allBoxes);

await pdfDoc.setPagesBox({
    indexes: [pageIndex],
    width: 600,
    height: 792,
    offsetX: 0,
    offsetY: 0,
    boxes: {
        cropBox: {
            left: 0,
            bottom: 0,
            right: 600,
            top: 792
        }
    }
});

await page.setPageSize(600, 792);

boxType values for getPageBox() are defined by Box_Type in the API Reference (for example Media, Crop, Trim, Art, Bleed).

Save changes ​

Page organization operations modify the current PDFDoc. To download or upload the modified document, call getFile() after the operations complete.

javascript
const file = await pdfDoc.getFile({
    fileName: 'organized.pdf'
});

Notes ​

  • Page indexes start at 0.
  • Delete, move, rotate, and similar operations change document structure; confirm user intent before running them.
  • PDFViewer.rotateTo() only changes viewer display; use PDFDoc.rotatePages() to persist page rotation.
  • PDFPage.setRotation() suits single-page rotation; PDFDoc.rotatePages() suits batch page ranges.
  • Prefer PDFDoc.setPagesBox() to adjust page boxes (pass indexes: [i] for a single page); use PDFPage.setPageSize() when only width and height change.
  • extractPages() returns PDF binary data and does not modify the current document directly.
  • When using mergePDFDoc(), keep the source PDFDoc valid until the merge completes; if the source is a user file, insertPages() can avoid holding multiple document instances long term.
  • For large documents, show progress in the UI or disable duplicate actions to avoid users triggering page organization repeatedly.