|
55 | 55 | - [Usage Examples](#usage-examples) |
56 | 56 | - [Create Document](#create-document) |
57 | 57 | - [Modify Document](#modify-document) |
58 | | - - [Create Form](#create-form) - _**new!**_ |
59 | | - - [Fill Form](#fill-form) - _**new!**_ |
| 58 | + - [Create Form](#create-form) |
| 59 | + - [Fill Form](#fill-form) |
| 60 | + - [Flatten Form](#flatten-form) - _**new!**_ |
60 | 61 | - [Copy Pages](#copy-pages) |
61 | 62 | - [Embed PNG and JPEG Images](#embed-png-and-jpeg-images) |
62 | 63 | - [Embed PDF Pages](#embed-pdf-pages) |
|
84 | 85 |
|
85 | 86 | - Create new PDFs |
86 | 87 | - Modify existing PDFs |
87 | | -- Create forms - _**new!**_ |
88 | | -- Fill forms - _**new!**_ |
| 88 | +- Create forms |
| 89 | +- Fill forms |
| 90 | +- Flatten forms - _**new!**_ |
89 | 91 | - Add Pages |
90 | 92 | - Insert Pages |
91 | 93 | - Remove Pages |
@@ -413,6 +415,54 @@ const pdfBytes = await pdfDoc.save() |
413 | 415 | // • Rendered in an <iframe> |
414 | 416 | ``` |
415 | 417 |
|
| 418 | +### Flatten Form |
| 419 | + |
| 420 | +_This example produces [this PDF](assets/pdfs/examples/flatten_form.pdf)_ (when [this PDF](assets/pdfs/form_to_flatten.pdf) is used for the `formPdfBytes` variable). |
| 421 | + |
| 422 | +<!-- [Try the JSFiddle demo](https://jsfiddle.net/Hopding/0mwfqkv6/3/) --> |
| 423 | + |
| 424 | +<!-- prettier-ignore --> |
| 425 | +```js |
| 426 | +import { PDFDocument } from 'pdf-lib' |
| 427 | + |
| 428 | +// This should be a Uint8Array or ArrayBuffer |
| 429 | +// This data can be obtained in a number of different ways |
| 430 | +// If your running in a Node environment, you could use fs.readFile() |
| 431 | +// In the browser, you could make a fetch() call and use res.arrayBuffer() |
| 432 | +const formPdfBytes = ... |
| 433 | + |
| 434 | +// Load a PDF with form fields |
| 435 | +const pdfDoc = await PDFDocument.load(formPdfBytes) |
| 436 | + |
| 437 | +// Get the form containing all the fields |
| 438 | +const form = pdfDoc.getForm() |
| 439 | + |
| 440 | +// Fill the form's fields |
| 441 | +form.getTextField('Text1').setText('Some Text'); |
| 442 | + |
| 443 | +form.getRadioGroup('Group2').select('Choice1'); |
| 444 | +form.getRadioGroup('Group3').select('Choice3'); |
| 445 | +form.getRadioGroup('Group4').select('Choice1'); |
| 446 | + |
| 447 | +form.getCheckBox('Check Box3').check(); |
| 448 | +form.getCheckBox('Check Box4').uncheck(); |
| 449 | + |
| 450 | +form.getDropdown('Dropdown7').select('Infinity'); |
| 451 | + |
| 452 | +form.getOptionList('List Box6').select('Honda'); |
| 453 | + |
| 454 | +// Flatten the form's fields |
| 455 | +form.flatten(); |
| 456 | + |
| 457 | +// Serialize the PDFDocument to bytes (a Uint8Array) |
| 458 | +const pdfBytes = await pdfDoc.save() |
| 459 | + |
| 460 | +// For example, `pdfBytes` can be: |
| 461 | +// • Written to a file in Node |
| 462 | +// • Downloaded from the browser |
| 463 | +// • Rendered in an <iframe> |
| 464 | +``` |
| 465 | + |
416 | 466 | ### Copy Pages |
417 | 467 |
|
418 | 468 | _This example produces [this PDF](assets/pdfs/examples/copy_pages.pdf)_ (when [this PDF](assets/pdfs/with_update_sections.pdf) is used for the `firstDonorPdfBytes` variable and [this PDF](assets/pdfs/with_large_page_count.pdf) is used for the `secondDonorPdfBytes` variable). |
|
0 commit comments