Flatten form fields¶
This operation removes interactive AcroForm fields from a PDF by writing their current values into page content via an iTextSharp PdfStamper with form flattening enabled.
| Field | Value |
|---|---|
| Operation ID | OP-07 |
| Trigger | Caller invokes Flatten(byte[] file) |
| Inputs | PDF bytes |
| Outputs | Flattened PDF bytes, or the original bytes unchanged |
| Primary components | PdfAPI.Core.PdfClientService.Flatten |
| Entry point | PdfAPI.Core\Services\PdfClientService.cs |
Steps¶
- Run Validate PDF bytes to guard the input. (BR-01, BR-02, BR-03)
- Open a
PdfReaderover the bytes. - If
reader.AcroFormis null, return the input bytes unchanged. (BR-11) - Otherwise write through a
PdfStamperwithFormFlattening = trueinto aMemoryStream, close the stamper, and return the stream bytes. (BR-11) - The
MemoryStreamin this step is not disposed and thePdfStamperis closed rather than disposed.*
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start(["Flatten(bytes)"]) --> Guard["OP-01: ValidatePdf"]
Guard --> Open["Open PdfReader"]
Open --> Form{reader.AcroForm null?}
Form -->|Yes| Unchanged([Return original bytes])
Form -->|No| Stamp["PdfStamper with<br>FormFlattening = true"]
Stamp --> Return([Return flattened bytes])
Failure and edge-case behavior¶
- A PDF without an AcroForm is returned byte-for-byte unchanged rather than being reprocessed (BR-11).
Note: Statements marked with
*were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.
Related documentation¶
Sources¶
PdfAPI.Core\Services\PdfClientService.cs