SaveDOCXToStream

Document conversion, extraction, DOCX, streams

Description

Converts a PDF page range into an editable WordprocessingML document and writes the DOCX package at the current stream position

Syntax

Function TPDFlib.SaveDOCXToStream(Const PageRange: WideString;
  Options: Integer; Target: TStream): Integer;

Parameters

PageRangeA one-based page range, or an empty string for every page; explicit page order is preserved
OptionsA bit mask: PDF_DOCX_INCLUDE_IMAGES = 1 embeds page images as floating PNG objects, PDF_DOCX_DETECT_HEADINGS = 2 maps large short lines to Heading 1 through Heading 3, PDF_DOCX_PRESERVE_STYLES = 4 retains font names, sizes, colors, bold, italic, superscript and subscript, and PDF_DOCX_PRESERVE_PAGE_BREAKS = 8 inserts editable page breaks; PDF_DOCX_DEFAULT enables all four
TargetThe destination stream, which does not become owned by the library

Return values

Returns the number of converted pages, or zero when validation, cancellation, extraction, packaging, or writing fails

Remarks

Detected URL text becomes clickable hyperlinks; targets that differ in path or query case remain distinct, while identical targets reuse a relationship

Analysis uses SetStructuredTextOptions independently of the format-specific Options; inspect GetLastStructuredTextDiagnostics for reading sources and fallback warnings

Real tag-tree order takes precedence when content bindings are complete; unresolved or ambiguous content retains extracted text in mixed or geometric fallback, and unknown custom roles are not guessed from their names

Reading-order paragraphs, bullet and numbered lists, and tables become native editable Word content rather than page snapshots

Empty tagged cells, horizontal spans, and vertical spans use native Word table cells, w:gridSpan, and w:vMerge

Joined table fragments remain in one w:tbl across pages and identified repeated continuation headers are omitted; the page-break option inserts an editable break in the continued table

Geometric columns, margin furniture, continuation matching, and opt-in inferred merges remain heuristic; inspect the latest diagnostics and structured JSON provenance when fidelity matters

Selected page models and included images are retained for document-wide analysis; XML and package assembly use additional buffers, so memory grows with selected content and images

The output is a deterministic DOCX ZIP package with CRC-checked raw Deflate entries and requires no Office installation

Extraction preserves the selected page and open writer tag stack; current unsaved writer tags are included without closing tags or finalizing the document

LastErrorCode is 114 for invalid arguments and 518 for extraction, packaging, or stream failures

Text and navigation fidelity

Explicit line endings and tabs in extracted replacement text become native Word line break and tab elements

Ordered lists retain their source starting number and restart after an interruption instead of continuing every sequence from one shared counter

Internal PDF page links become Word bookmarks and local hyperlinks named page_N, rather than external relationships

Preserved fonts populate Latin, East Asian, and complex-script assignments; valid language tags are inherited from the semantic ancestor chain and attached to the corresponding text runs

See also

SaveDOCXToFile, ExtractStructuredDocument, ExportDocumentHTML