FlattenOptionalContent
Small Business, layers
Requires the Small Business edition — in Delphi, C++Builder and Lazarus code create the library as TPDFlibSmallBusiness (unit PDFlibSmallBusiness) or TPDFlibEnterprise; the Small Business and Enterprise DLL and ActiveX builds provide it unchanged; the Single edition keeps the method, which returns 0 and sets LastErrorCode to 901 (PDFLIB_ERROR_SMALLBUSINESS_REQUIRED)
Requires Small Business or Enterprise; create TPDFlibSmallBusiness or TPDFlibEnterprise; a plain TPDFlib returns 0 with error 901
Description
Freezes supported optional content to a selected view, print or export configuration while retaining vector content
Syntax
Delphi
Function TPDFlib.FlattenOptionalContent(ConfigurationIndex: Integer; Const Usage: WideString): Integer; Virtual;ActiveX
Function PDFlib::FlattenOptionalContent(ConfigurationIndex As Long, Usage As String) As LongDLL
int DLFlattenOptionalContent(int InstanceID, int ConfigurationIndex, const wchar_t* Usage);
int DLFlattenOptionalContentA(int InstanceID, int ConfigurationIndex, const char* Usage);
ConfigurationIndex=0 selects the default /D configuration; positive indexes select /Configs[index-1]; Usage is View, Print or Export, with an empty string treated as View
Returns 1 on success; returns 0 with error 902 for unsupported or malformed input while retaining the source document and its existing child handles; the configured logger receives the rejection reason
The implementation resolves ON/OFF base states and overrides, supported usage applications, all four OCMD policies and nested visibility expressions, then rewrites pages, Forms, images, annotations, normal appearances and supported soft-mask groups
Parameters
| ConfigurationIndex | Zero-based index of the optional-content configuration whose visibility state is baked in; 0 selects the document's default configuration |
|---|---|
| Usage | The usage view to flatten: an empty string, View, Print or Export; any other value is rejected |
Return values
Returns 1 when the document was flattened and reloaded; returns 0 when no document is selected, Usage is not one of the accepted values, the current API scope cannot change the document, the operation was aborted, or the flattening pass fails. A document without optional content is a successful no-op for ConfigurationIndex 0
Private inherited contexts
Each active Form, tiling pattern and Type3 font variant receives private resource bindings for its effective inherited font, text rendering mode and selected patterns; repeated equivalent variants are cached, while different resource or state contexts receive separate rewrites
Type3 glyph metrics and font widths remain intact, glyph programs are rewritten with the font's private resources, and PDF 2.0 glyph-local resources retain their priority; pattern cells retain their inherited rendering state without recursively reselecting the pattern being painted
Inline images and hidden state
Supported raw, ASCIIHex, ASCII85, RunLength and Flate inline images use proved byte boundaries, including supported filter chains and final-Flate TIFF/PNG predictor layouts; pixel bytes that resemble EI or content operators are preserved as image data
Hidden painting is suppressed while graphics-state operations, path clipping and text advancement remain effective; hidden text uses rendering mode 3, or mode 7 when regular-font clipping must remain active, and its original rendering mode is restored afterwards
Commit behavior
Preparation, resource allocation and serialization occur in a private document; the result is reopened before adoption, preserving the selected document handle, page, password protection and active conformance authoring policies
After a successful call, reacquire font, image, form, search, structure cursor and other child resource handles because the document has been replaced; a rejected call retains the source document and those existing handles
Scope limits
Unsupported inputs include Unchanged base states, non-View intents, advanced automatic usage categories, unknown operators, unprovable inline-image codecs or layouts, hidden widgets, hidden appearance or soft-mask Forms, shared page annotations, tagged child streams that need structure ownership reconciliation, SetOCGState actions, reference XObjects and alternate images
Traversal is bounded to 10,000 pages, optional-content groups and private Form/font variants, 64 levels of child traversal, 256 graphics and marked-content levels, one million instructions per parsed stream and 100,000 private resource bindings and resource/visibility reference steps; decoded source content, prepared output and resource/state-key work each share a 64 MiB bound
Inline-image proof additionally retains its 16 MiB payload/intermediate cap, four-filter maximum and shared 64 MiB decoded-reservation and filter-work counters; unsupported cases and exhausted bounds reject the operation
This is visual flattening: hidden text bytes can remain as invisible text to preserve advancement, and original or unreachable resource objects can remain in the file; it does not securely delete sensitive content
Example
if PDF.FlattenOptionalContent(0, 'Print') = 1 then
PDF.SaveToFile('delivery.pdf');
DLL and Dylib expose DLFlattenOptionalContent and its A variant; ActiveX exposes the method through IPDFlibrary7
Edition comparison · ApplyRedactions · DeleteOptionalContentGroupEx