Flatten PDF Forms
Flattening a PDF form is a process that converts interactive form fields into static content, making them part of the PDF document itself. This means that once a PDF form is flattened, the information filled in the form fields cannot be edited or modified.
Flattening is used to preserve a document’s content and prevent any further changes, ensuring the information remains as it was when the form was flattened. This is particularly useful in situations where the completed form needs to be shared, archived, or submitted for official purposes and it’s important that the data remains unchanged.
Since form fields in a PDF document are a type of annotation called a “widget annotation,” the process of flattening a form is similar to that of flattening annotations.
How to Flatten PDF Forms
-
Send a multipart POST request with instructions to Document Engine’s
/api/build
endpoint.
For more information, refer to the API reference to learn about the /api/build
endpoint and all the actions you can perform on PDFs with Document Engine.
For an overview of multipart requests, refer to the brief tour of multipart requests blog post.
To flatten a PDF document, apply the flatten
action to the PDF part in the /api/build
endpoint’s instructions. This request merges the document1
and document2
files before flattening the annotations in the final output PDF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
curl -X POST http://localhost:5000/api/build \ -H "Authorization: Token token=<API token>" \ -F document1=@/path/to/example-document1.pdf \ -F document2=@/path/to/example-document2.pdf \ -F instructions='{ "parts": [ { "file": "document1" }, { "file": "document2" } ], "actions": [ { "type": "flatten" } ] }' \ -o result.pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
POST /api/build HTTP/1.1 Content-Type: multipart/form-data; boundary=customboundary Authorization: Token token=<API token> --customboundary Content-Disposition: form-data; name="document1"; filename="example-document1.pdf" Content-Type: application/pdf <PDF data> --customboundary Content-Disposition: form-data; name="document2"; filename="example-document2.pdf" Content-Type: application/pdf <PDF data> --customboundary Content-Disposition: form-data; name="instructions" Content-Type: application/json { "parts": [ { "file": "document1" }, { "file": "document2" } ], "actions": [ { "type": "flatten" } ] } --customboundary--
Flattening Specific Pages of a Document
Flattening is applied to all parts when it’s specified in the outermost actions
field. To flatten only a section of a document, split the document into multiple parts and flatten each part separately.
A part can be restricted to certain pages of a document. This way, you can flatten annotations only on the specified pages. The request below will flatten the first two pages of a four-page document with indexes from zero to four.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
curl -X POST http://localhost:5000/api/build \ -H "Authorization: Token token=<API token>" \ -F document=@/path/to/example-document.pdf \ -F instructions='{ "parts": [ { "file": "document", "pages": { "start": 0, "end": 1 }, "actions": [ { "type": "flatten" } ] }, { "file": "document", "pages": { "start": 2, "end": 3 } } ] }' \ -o result.pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
POST /api/build HTTP/1.1 Content-Type: multipart/form-data; boundary=customboundary Authorization: Token token=<API token> --customboundary Content-Disposition: form-data; name="document"; filename="example-document.pdf" Content-Type: application/pdf <PDF data> --customboundary Content-Disposition: form-data; name="instructions" Content-Type: application/json { "parts": [ { "file": "document", "pages": { "start": 0, "end": 1 }, "actions": [ { "type": "flatten" } ] }, { "file": "document", "pages": { "start": 2, "end": 3 } } ] } --customboundary--