XFA Medium Prototypes and Supplied Stock
Unit: HPDFXFA
TXFAMediumResolutionOptions configures load-time inheritance for medium declarations and a separate application-supplied stock-size lookup; it does not add general prototype expansion for other XFA element classes
Configure an operation or runtime session
Start with TXFAMediumResolutionOptions.Default, then set the resource and stock callbacks and their limits as needed
- Set
TXFAWidgetRuntimeOptions.MediumResolutionbefore constructing an independent runtime - Set
TXFAHeadlessPDFOptions.MediumResolutionbefore native headless PDF creation - Set
THotPDF.XFAMediumResolutionOptionsbeforeApplyXFAAsAcroForm,FlattenLoadedXFAorCreateLoadedXFAWidgetRuntime; an already returned runtime retains its own fixed session configuration - The full
HPDFXFAFlattenoverload acceptsAMediumResolutionafterEmitPageand beforeout PhysicalPageCount; existing overloads remain available and delegate with default options
function HPDFXFAFlatten(const XDPBytes: TBytes; PageW, PageH: Double;
Editable: Boolean; const EmitField: TXFAEmitField;
const EmitDraw: TXFAEmitDraw; Warnings: TStringList;
const AScriptOptions: TXFAFormScriptOptions;
const AInstanceAction: TXFAFormInstanceAction;
const AMeasureText: TXFAMeasureTextFunc; const EmitPage: TXFAEmitPage;
const AMediumResolution: TXFAMediumResolutionOptions;
out PhysicalPageCount: Integer): TXFAFlattenResult; overload;
A lower-level TXFADocument caller uses PrepareMediumResolution explicitly before form merging or layout; the generic LoadFromBytes packet parser does not perform external lookups
One-time medium prototype preparation
Preparation resolves medium use and usehref references before initial runtime layout or flatten emission, publishes inherited attributes into the receiving live medium nodes and consumes their resolved references; the caller's input byte buffer is not modified
Targets must resolve uniquely to the same element class in template structure, without expanding repeated data instances. Nonempty usehref takes priority over use; explicitly supplied local attributes, including empty values, override inherited attributes
Use an XML ID or supported structural template SOM for an internal target, or ResolveResource plus BaseURI for external template bytes. URI-only usehref retains the standard default subform target; it is not a shortcut to the first medium in a resource
The resource callback receives the document URI after reference/base merging and fragment separation, together with its base context, and returns a TXFAMediumResource containing URI, BaseURI and actual Data bytes. URI handling merges relative paths, removes dot segments and normalizes scheme/host case and percent-escape spelling; request and canonical-response aliases reuse the first accepted immutable document snapshot. There is no implicit filesystem or network request
Preparation is separate from reflow and rollback. Later edits to a prototype declaration or its former reference do not retroactively change properties already inherited by another medium; edits to that prepared medium's own properties remain visible to later layout
Supplied stock dimensions and precedence
Two valid positive explicit edges, with long greater than short, take priority over stock. A zero/default edge pair requests stock lookup when a provider is configured; an absent medium retains the caller page dimensions
A partial or malformed explicit edge pair retains the existing warning and caller-page fallback without asking the stock provider. Without a provider, unresolved stock retains the caller-page fallback. Portrait, including the existing empty-orientation default, uses the short edge across the page; landscape swaps the edges once without rotating content again
An omitted or explicitly empty stock uses letter for size lookup in this adapter. An explicitly empty attribute remains empty in the stored DOM and blocks prototype inheritance; a nonempty stock name is passed unchanged, without trimming or case folding
ResolveStock receives the exact stock name and an opaque TXFAMediumResource configuration and returns a TXFAMediumStockSize with finite positive ShortEdge and LongEdge in PDF points before orientation, with LongEdge greater than ShortEdge
Set HasStockConfiguration and supply StockConfiguration to select the configuration snapshot; otherwise the callback receives an empty resource context. The library copies configuration bytes for the session and callback use
The callback returns xmlsFound, xmlsNotFound or xmlsError, with an optional diagnostic. Provider errors and invalid found dimensions are errors, not ordinary missing-stock fallbacks
The application can adapt an admitted embedded XDC packet or external configuration to this callback. No universal XDC grammar, printer-tray behavior or built-in paper catalog is inferred; XFA 3.3 printed page 1041 leaves XDC grammars application-specific
Fixed configuration and callback lifetime
The provider configuration is fixed for one runtime document session or one complete flatten operation. Accepted found and missing results are copied into a bounded cache keyed by the exact stock name; the provider must return stable results for that configuration
Changing stock A to B requests B on first use, and returning to A reuses the accepted result. Lookup does not overwrite medium short and long attributes, so later stock edits retain their ordinary selection semantics
Reflow and snapshot restoration reuse accepted entries without requesting them again. Read-only memoization entries may survive a failed transaction while XML, values, layout, focus and edit state still roll back; a provider is a synchronous lookup rather than a transactional host-effect API
A different provider configuration requires a new runtime/document session. Keep callbacks and their receivers valid for the operation or returned runtime lifetime; assigning component options does not transfer ownership of an arbitrary callback target
Save, reload and portability
Runtime SaveToBytes serializes the expanded medium attributes and consumed prototype references, rather than promising byte-preserving original declarations. Reloading that expanded state does not require the original external prototype resource provider
Internal snapshot restoration restores the already expanded document without preparing prototypes again, preserving the runtime's existing node/script identity and interaction recovery contract
A document still using stock needs an equivalent provider and configuration in a fresh session after reload; accepted stock responses are not silently embedded as explicit medium dimensions
Limits and compatibility
| Option | Default and scope |
|---|---|
| ResolveResource, ResolveStock | nil; application-supplied resource bytes and stock dimensions are opt-in callbacks |
| MaxResources | 64 retained external resource snapshots |
| MaxResourceBytes | 16 MiB for each separately accounted configuration, resource-response, expanded-attribute or diagnostic limit described below |
| MaxOperations | 200000 preparation operations |
| MaxReferenceDepth | 64 nested reference levels |
| MaxStockEntries | 1024 accepted found or missing stock entries |
| MaxStockKeyBytes | 65536 bytes for an individual stock key and the total accepted stock-key bytes; medium reference and URI text also use this per-string bound |
| CheckCancelled | nil; optional caller cancellation check during bounded preparation and lookup work |
MaxResourceBytes separately bounds the stock configuration Data plus URI and BaseURI lengths, the cumulative Data plus returned URI and BaseURI lengths of newly retained external snapshots, staged expanded-attribute strings counted as Length times SizeOf(Char), and each provider diagnostic. These are separate limits, not one aggregate peak-memory allowance
Delphi provider types are anonymous-method references; the FPC declarations use method callbacks. A cancellation callback cannot preempt arbitrary caller code inside a synchronous provider, so keep provider work bounded
Start from Default when configuring the options; nonpositive medium-resolution limit values use their corresponding defaults. The existing layout, font, page, script and native-output limits continue to apply independently
Prototype preparation currently applies to medium elements; it does not expand prototypes for other XFA element classes
See per-master physical media, Interactive XFA Widget Runtime and CreateLoadedXFAWidgetRuntime