Skip to content

Inspect and flatten PDF form fields

Provides two related utilities: detecting whether a PDF has AcroForm fields, and flattening those fields (making them non-editable) when present.

Field Value
Operation ID OP-SFC-013
Trigger PdfAPI\Services\PdfService.cs:38 and :45, used when the caller opts out of the remote client
Inputs byte[] PDF
Outputs bool (HasFormFields) or byte[] (Flatten)
Primary components PdfHelper.HasFormFields, PdfHelper.Flatten
Entry point PdfApi.SyncFusionCore\PdfHelper.cs

Steps

  1. HasFormFields: load the bytes as a PdfLoadedDocument inside a using and return doc.Form != null (BR-SFC-034).
  2. Flatten: load the bytes as a PdfLoadedDocument; when doc.Form != null, set Form.Flatten = true, save to a MemoryStream and return the new bytes (BR-SFC-035).
  3. If the document has no form: return the original input bytes unchanged (BR-SFC-036).

Process flow

The following diagram visualizes this operation's control flow.

flowchart TD
    Start(["HasFormFields or Flatten<br>called with PDF bytes"]) --> Load["Load bytes as<br>PdfLoadedDocument"]
    Load --> Which{"Which call?"}
    Which -->|HasFormFields| CheckForm{"doc.Form != null?"}
    CheckForm -->|Yes| ReturnTrue([Return true])
    CheckForm -->|No| ReturnFalse([Return false])
    Which -->|Flatten| CheckForm2{"doc.Form != null?"}
    CheckForm2 -->|Yes| SetFlatten["Set Form.Flatten = true,<br>save to MemoryStream"]
    SetFlatten --> ReturnFlattened([Return flattened bytes])
    CheckForm2 -->|No| ReturnOriginal(["Return original bytes<br>unchanged"])

Failure and edge-case behavior

  • A PDF with no Form present is a normal, non-error outcome for both methods: HasFormFields returns false and Flatten returns the input unchanged.

Sources

  • PdfApi.SyncFusionCore\PdfHelper.cs
  • PdfAPI\Services\PdfService.cs