PrintDocumentEx

Rendering and printing

Description

Prints a page range of the selected document to the named printer with an explicit number of copies and a collation setting, baking both into the job's DEVMODE through a temporary custom-printer profile before delegating to PrintDocument. Returns 1 when the job is sent, or 0 when the printer name is not valid or the profile cannot be created.

Syntax

Delphi

Function TPDFlib.PrintDocumentEx(Const PrinterName: WideString; StartPage, EndPage, Options, Copies, Collate: Integer): Integer;

Parameters

PrinterName is either a printer name as enumerated by GetPrinterNames or a profile name returned by NewCustomPrinter; anything else makes the call fail without printing. StartPage and EndPage are the 1-based inclusive page range and are normalised rather than rejected: a start below 1 becomes 1, a start past the last page becomes the last page, and an end below the start is raised to the start. Options takes the value produced by PrintOptions, whose low bits select print behaviour and whose high bits select an entry from the print-title list. Copies is the copy count, where 0 leaves the driver default for DEVMODE.dmCopies untouched and 1..32767 writes that value. Collate is the collation flag, 0 for off and 1 for on, written to DEVMODE.dmCollate.

Return values

1 when the requested pages were rendered and the job was handed to the printer queue. 0 when PrinterName is neither an installed printer nor a known custom profile so no temporary profile could be created, when the document could not be rendered, when no document is open, or when the job was cancelled before it finished.

Remarks

Copies and collation are not produced by repeating the page loop: the function creates a temporary custom-printer profile named PLPrinter followed by a generated suffix, stores both settings in that profile's DEVMODE, and delegates to PrintDocument, so the driver receives a single job carrying the requested values. Each call registers a further profile in the component and profiles are released only when the component is destroyed, so a process that prints many times accumulates them; PrintDocumentAdvanced performs the same job through one settings record and validates every field up front. Out-of-range settings are dropped silently instead of failing the call: a Copies value of 0 or above 32767 leaves dmCopies unset, and a Collate value outside 0..1 leaves dmCollate unset, with the document printed either way. Collation only has any visible effect when more than one copy is requested and the driver supports it.