2009. április 5., vasárnap

How to set the printer paper size


I want to set the format of my page to a particular format for the printer that is not A4 (the default) but B5. How can I do this?


You have to descend into the depth of the API for that and modify the printers DEVMODE record. Note that you should first try to select a paper type and let the driver figure out the bin for that. See win32.hlp entry for DEVMODE for a list of valid paper format ID's.

  Device, Driver, Port: array[0..80] of Char;
  DevMode: THandle;
  pDevmode: PDeviceMode;
  {Get printer device name etc.}
  Printer.GetPrinter(Device, Driver, Port, DevMode);
  {force reload of DEVMODE}
  Printer.SetPrinter(Device, Driver, Port, 0);
  {get DEVMODE handle}
  Printer.GetPrinter(Device, Driver, Port, DevMode);
  if Devmode <> 0 then
    {lock it to get pointer to DEVMODE record}
    pDevMode := GlobalLock(Devmode);
    if pDevmode <> nil then
      with pDevmode^ do
        {modify paper size}
        dmPapersize := DMPAPER_B5;
        {tell printer driver that dmPapersize field contains data it needs to inspect}
        dmFields := dmFields or DM_PAPERSIZE;
      {unlock DEVMODE handle}

You need to do this before a BeginDoc has been performed.

If this does not work for your purpose you have to modify the dmDefaultSource field of the DEVMODE (same principle as above). Windows.Pas defines a bunch of DMBIM_* constants but there is no guarantee that your printer will support them all (or even use the constant for the bin you think it should use). Better ask it for the bins it supports.

uses winspool;

procedure GetBinnames(sl: TStrings);
  TBinName = array[0..23] of Char;
  TBinNameArray = array[1..High(Integer) div Sizeof(TBinName)] of TBinName;
  PBinnameArray = ^TBinNameArray;
  TBinArray = array[1..High(Integer) div Sizeof(Word)] of Word;
  PBinArray = ^TBinArray;
  Device, Driver, Port: array[0..255] of Char;
  hDevMode: THandle;
  i, numBinNames, numBins, temp: Integer;
  pBinNames: PBinnameArray;
  pBins: PBinArray;
  Printer.PrinterIndex := -1;
  Printer.GetPrinter(Device, Driver, Port, hDevmode);
  numBinNames := WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, nil, nil);
  numBins := WinSpool.DeviceCapabilities(Device, Port, DC_BINS, nil, nil);
  if numBins <> numBinNames then
    raise Exception.Create('DeviceCapabilities reports different number of bins and ' + 'bin names!');
  if numBinNames > 0 then
    pBins := nil;
    GetMem(pBinNames, numBinNames * Sizeof(TBinname));
    GetMem(pBins, numBins * Sizeof(Word));
      WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, Pchar(pBinNames), nil);
      WinSpool.DeviceCapabilities(Device, Port, DC_BINS, Pchar(pBins), nil);
      for i := 1 to numBinNames do
        temp := pBins^[i];
        sl.addObject(pBinNames^[i], TObject(temp));
      if pBins <> nil then

The integers in the DC_BINS array, which end up in the passed TStrings descendents Object property, are what you need to use for dmDefaultSource.

Nincsenek megjegyzések:

Megjegyzés küldése