Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/api/form/PDFForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { assertIs, Cache, assertOrUndefined } from 'src/utils';

export interface FlattenOptions {
updateFieldAppearances: boolean;
fields?: PDFField[];
}

/**
Expand Down Expand Up @@ -514,7 +515,7 @@ export default class PDFForm {
}

/**
* Flatten all fields in this [[PDFForm]].
* Flatten fields in this [[PDFForm]].
*
* Flattening a form field will take the current appearance for each of that
* field's widgets and make them part of their page's content stream. All form
Expand All @@ -528,18 +529,26 @@ export default class PDFForm {
* document, fill its fields, flatten it, and then copy its pages into the
* recipient document - the filled fields will be copied over.
*
* For example:
* For example (flatten all fields):
* ```js
* const form = pdfDoc.getForm();
* form.flatten();
* ```
*
* To flatten specific fields, you can use this syntax:
* ```js
* const form = pdfDoc.getForm();
* form.flatten({ fields: [
* form.getField('Example')
* ] });
* ```
*/
flatten(options: FlattenOptions = { updateFieldAppearances: true }) {
flatten(options: FlattenOptions = { updateFieldAppearances: true }): void {
if (options.updateFieldAppearances) {
this.updateFieldAppearances();
}

const fields = this.getFields();
const fields = options.fields ?? this.getFields();

for (let i = 0, lenFields = fields.length; i < lenFields; i++) {
const field = fields[i];
Expand Down