Forms and Fields
Forms and Fields
Section titled “Forms and Fields”pdf-lib can create AcroForms — the interactive form layer built into the PDF standard. Fields are filled by the reader application (Acrobat, a browser’s PDF viewer, etc.) and the filled values can be extracted programmatically.
Accessing the form
Section titled “Accessing the form”Every PDFDocument has exactly one form object, retrieved with doc.getForm().
All field creation and lookup goes through this object:
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib')
async function main() { const doc = await PDFDocument.create() const font = await doc.embedFont(StandardFonts.Helvetica) const form = doc.getForm()
const page = doc.addPage([595, 842])
// … add fields below …
return doc}Text fields
Section titled “Text fields”form.createTextField(name) returns a PDFTextField. Call field.addToPage()
to position it on a page:
const nameField = form.createTextField('applicant.name')nameField.setText('Jane Smith') // optional default valuenameField.addToPage(page, { x: 150, y: 700, width: 200, height: 24, font, borderColor: rgb(0.4, 0.4, 0.4), borderWidth: 1,})Field names use dot notation to group related fields (applicant.name,
applicant.email). Names must be unique within the document.
Checkboxes
Section titled “Checkboxes”const agreeBox = form.createCheckBox('terms.agree')agreeBox.check() // checked by defaultagreeBox.addToPage(page, { x: 150, y: 660, width: 16, height: 16,})
page.drawText('I agree to the terms and conditions', { x: 175, y: 663, size: 12, font, color: rgb(0.2, 0.2, 0.2),})Radio groups
Section titled “Radio groups”A radio group holds multiple options; only one can be selected at a time. Create the group once, then add each option as a widget on a page:
const planGroup = form.createRadioGroup('subscription.plan')
const options = [ { label: 'Basic', value: 'basic', y: 600 }, { label: 'Pro', value: 'pro', y: 576 }, { label: 'Enterprise', value: 'enterprise', y: 552 },]
for (const opt of options) { planGroup.addOptionToPage(opt.value, page, { x: 150, y: opt.y, width: 14, height: 14, }) page.drawText(opt.label, { x: 172, y: opt.y + 2, size: 12, font, color: rgb(0.2, 0.2, 0.2), })}
planGroup.select('pro') // default selectionDropdowns
Section titled “Dropdowns”PDFDropdown presents a list of options in a pop-up menu:
const countryField = form.createDropdown('address.country')countryField.addOptions(['Australia', 'Canada', 'United Kingdom', 'United States'])countryField.select('Australia')countryField.addToPage(page, { x: 150, y: 510, width: 180, height: 24, font, borderColor: rgb(0.4, 0.4, 0.4), borderWidth: 1,})A complete example
Section titled “A complete example”Putting it all together — a simple one-page contact form:
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib')
async function main() { const doc = await PDFDocument.create() const font = await doc.embedFont(StandardFonts.Helvetica) const fontB = await doc.embedFont(StandardFonts.HelveticaBold) const form = doc.getForm() const page = doc.addPage([595, 842])
const label = (text, y) => page.drawText(text, { x: 50, y, size: 11, font: fontB, color: rgb(0.3, 0.3, 0.3) })
const field = (name, y, w = 200) => { const tf = form.createTextField(name) tf.addToPage(page, { x: 150, y, width: w, height: 22, font, borderColor: rgb(0.6, 0.6, 0.6), borderWidth: 1, }) }
// Title page.drawText('Contact Form', { x: 50, y: 780, size: 22, font: fontB, color: rgb(0.1, 0.1, 0.4), }) page.drawLine({ start: { x: 50, y: 768 }, end: { x: 545, y: 768 }, thickness: 1, color: rgb(0.2, 0.4, 0.8), })
// Fields label('Full name', 730); field('contact.name', 728, 250) label('Email', 700); field('contact.email', 698, 250) label('Phone', 670); field('contact.phone', 668, 180)
label('Subject', 630) const subjectDrop = form.createDropdown('contact.subject') subjectDrop.addOptions(['General enquiry', 'Support', 'Billing', 'Other']) subjectDrop.select('General enquiry') subjectDrop.addToPage(page, { x: 150, y: 628, width: 220, height: 22, font, borderColor: rgb(0.6, 0.6, 0.6), borderWidth: 1, })
label('Message', 590) const msgField = form.createTextField('contact.message') msgField.enableMultiline() msgField.addToPage(page, { x: 150, y: 510, width: 350, height: 80, font, borderColor: rgb(0.6, 0.6, 0.6), borderWidth: 1, })
return doc}Reading field values
Section titled “Reading field values”To pre-populate a form or inspect values already in a PDF, use
form.getTextField(name).getText(), form.getCheckBox(name).isChecked(), etc.:
const existing = await PDFDocument.load(existingPdfBytes)const form = existing.getForm()const name = form.getTextField('applicant.name').getText()Flattening
Section titled “Flattening”Flattening bakes the current field values into the page content and removes interactivity — useful for archiving a filled form as a static document:
form.flatten()return docNext steps
Section titled “Next steps”- Open Edit imports… and select the Forms preset to import all form field types at once.
- Combine forms with the drawing API (lines, rectangles, text labels) to produce fully styled, print-ready documents.