How to Work with the Core PDF API in TypeScript
This guide shows how to open, create, edit, split, and save PDF documents
with Aspose.PDF FOSS for TypeScript. The Document and Page classes are
the entry points for every operation in the library — opening and creating
files, editing pages in place, splitting and combining documents, and saving
the result. It requires only Node.js 22 or later.
Step-by-Step Guide
Step 1: Install the Package
asposefoss/pdf is not yet published — build from source until it ships. See the project README for build instructions.
Verify the installation by importing the Document class in a new
TypeScript file — this line should resolve without error once the package
is installed:
import { Document } from '@asposefoss/pdf';Step 2: Import Required Classes
Import Document to load or create a PDF, and PageFormat if you plan to
start a document from scratch:
import { Document, PageFormat } from '@asposefoss/pdf';Step 3: Open an Existing PDF and Edit Pages
Document.OpenFile() opens a PDF from a file path; Document.Open()
accepts an in-memory Uint8Array. Pages can be inspected and edited in
place, and Document.WriteTo() writes the result back out.
import { Document } from '@asposefoss/pdf';
// Open from disk (or Document.Open(uint8array) for in-memory data)
const doc = Document.OpenFile('input.pdf');
// Inspect and edit pages
const pages = doc.Pages;
console.log(pages.length);
pages[0].Rotate = 90;
doc.RemovePage(2); // 1-based page number
doc.Reorder([3, 1, 2]);
// Save
doc.WriteTo('output.pdf'); // or: const bytes = doc.Save();
Step 4: Create a New PDF from Scratch
Document.New() starts a blank document at a given PageFormat.
Page.AddText() places positioned text, and Document.AddPage() appends
further pages.
import { Document, PageFormat } from '@asposefoss/pdf';
const doc = Document.New(PageFormat.A4); // one blank A4 page
doc.Pages[0].AddText('Hello', 72, 720, { fontSize: 14 });
doc.AddPage(PageFormat.A4.landscape()); // append more as you go
doc.WriteTo('scratch.pdf');Step 5: Split, Extract, and Combine Pages
Document.Split() returns one Document per page. Document.ExtractPages()
takes a 1-based, repeats-allowed page list and returns a new Document with
just those pages. Document.Append() and Document.InsertPage() copy pages
across documents, and Document.Merge() combines several documents into one.
const parts = doc.Split(); // one Document per page
const chapter = doc.ExtractPages([3, 4, 5]); // subset (1-based, repeats allowed) as a new Document
docA.Append(docB); // copy all of docB's pages onto docA
docA.InsertPage(1, docB.Pages[0]); // copy a single page across documents
Common Issues and Fixes
Document.OpenFile() throws InvalidPasswordError. The PDF is
password-protected and no password (or the wrong one) was supplied. Pass
{ password: '...' } to Document.OpenFile() / Document.Open().
RemovePage() or ExtractPages() removes or extracts the wrong page.
These methods take 1-based page numbers, while doc.Pages is a plain
0-based array — the two indexing schemes are easy to mix up.
WriteTo() output is larger than expected. The default save does not
use xref/object-stream compression. Pass { compressed: true } to
Document.WriteTo() to enable it.
Edits made to doc.Pages[i] do not appear in the saved file. Page edits
must happen before the call to Document.WriteTo() / Document.Save() —
there is no separate “commit” step, but edits made after saving will not be
reflected in a file already written to disk.
Frequently Asked Questions
How do I load a PDF from an in-memory buffer instead of a file path?
Pass a Uint8Array to Document.Open() instead of a path to
Document.OpenFile().
How do I rotate a page?
Set page.Rotate to 90, 180, or 270, then save the document with
Document.WriteTo().
Can I copy a single page from one document into another?
Yes — Document.InsertPage(at, sourcePage) copies one page from another
document at the given 1-based position, without copying the whole document.
Does Document.ExtractPages() allow the same page to appear twice?
Yes. The page-number list passed to Document.ExtractPages() allows
repeats, so the same source page can appear more than once in the result.