Skip to content

Commit 0b83638

Browse files
committed
Add form flattening to README
1 parent b8b5e92 commit 0b83638

File tree

4 files changed

+98
-20
lines changed

4 files changed

+98
-20
lines changed

README.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@
5555
- [Usage Examples](#usage-examples)
5656
- [Create Document](#create-document)
5757
- [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!**_
6061
- [Copy Pages](#copy-pages)
6162
- [Embed PNG and JPEG Images](#embed-png-and-jpeg-images)
6263
- [Embed PDF Pages](#embed-pdf-pages)
@@ -84,8 +85,9 @@
8485

8586
- Create new PDFs
8687
- Modify existing PDFs
87-
- Create forms - _**new!**_
88-
- Fill forms - _**new!**_
88+
- Create forms
89+
- Fill forms
90+
- Flatten forms - _**new!**_
8991
- Add Pages
9092
- Insert Pages
9193
- Remove Pages
@@ -413,6 +415,54 @@ const pdfBytes = await pdfDoc.save()
413415
// • Rendered in an <iframe>
414416
```
415417

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+
416466
### Copy Pages
417467

418468
_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).
35.5 KB
Binary file not shown.

scratchpad/index.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,43 @@ import { openPdf, Reader } from './open';
33
import { PDFDocument } from 'src/index';
44

55
(async () => {
6-
// Create a new PDFDocument
7-
const pdfDoc = await PDFDocument.create();
6+
// This should be a Uint8Array or ArrayBuffer
7+
// This data can be obtained in a number of different ways
8+
// If your running in a Node environment, you could use fs.readFile()
9+
// In the browser, you could make a fetch() call and use res.arrayBuffer()
10+
const formPdfBytes = fs.readFileSync('assets/pdfs/form_to_flatten.pdf');
811

9-
// Set the title and include the new option.
10-
pdfDoc.setTitle('Scratchpad Test Doc', { showInWindowTitleBar: true });
12+
// Load a PDF with form fields
13+
const pdfDoc = await PDFDocument.load(formPdfBytes);
1114

12-
// Add a blank page to the document
13-
const page = pdfDoc.addPage([550, 750]);
15+
// Get the form containing all the fields
16+
const form = pdfDoc.getForm();
1417

15-
// Manual test...
16-
page.drawText(
17-
`The window's title should match what we set in the metadata.`,
18-
{ x: 15, y: 400, size: 15 },
19-
);
18+
// Fill the form's fields
19+
form.getTextField('Text1').setText('Some Text');
2020

21-
// Save the PDF
21+
form.getRadioGroup('Group2').select('Choice1');
22+
form.getRadioGroup('Group3').select('Choice3');
23+
form.getRadioGroup('Group4').select('Choice1');
24+
25+
form.getCheckBox('Check Box3').check();
26+
form.getCheckBox('Check Box4').uncheck();
27+
28+
form.getDropdown('Dropdown7').select('Infinity');
29+
30+
form.getOptionList('List Box6').select('Honda');
31+
32+
// Flatten the form's fields
33+
form.flatten();
34+
35+
// Serialize the PDFDocument to bytes (a Uint8Array)
2236
const pdfBytes = await pdfDoc.save();
2337

38+
// For example, `pdfBytes` can be:
39+
// • Written to a file in Node
40+
// • Downloaded from the browser
41+
// • Rendered in an <iframe>
42+
2443
fs.writeFileSync('out.pdf', pdfBytes);
25-
openPdf('out.pdf', Reader.Acrobat);
44+
openPdf('out.pdf', Reader.Preview);
2645
})();

src/api/form/PDFForm.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,19 @@ export default class PDFForm {
513513
}
514514

515515
/**
516-
* Flatten all form fields.
516+
* Flatten all fields in this [[PDFForm]].
517517
*
518-
* Flattening a form field will take the current appearance and make that part
519-
* of the pages content stream. All form fields and annotations associated are removed.
518+
* Flattening a form field will take the current appearance for each of that
519+
* field's widgets and make them part of their page's content stream. All form
520+
* fields and annotations associated are then removed. Note that once a form
521+
* has been flattened its fields can no longer be accessed or edited.
522+
*
523+
* This operation is often used after filling form fields to ensure a
524+
* consistent appearance across different PDF readers and/or printers.
525+
* Another common use case is to copy a template document with form fields
526+
* into another document. In this scenario you would load the template
527+
* document, fill its fields, flatten it, and then copy its pages into the
528+
* recipient document - the filled fields will be copied over.
520529
*
521530
* For example:
522531
* ```js

0 commit comments

Comments
 (0)