2009. április 5., vasárnap

How to set the printer paper size


Problem/Question/Abstract:

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?

Answer:

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.


var
  Device, Driver, Port: array[0..80] of Char;
  DevMode: THandle;
  pDevmode: PDeviceMode;
begin
  {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
  begin
    {lock it to get pointer to DEVMODE record}
    pDevMode := GlobalLock(Devmode);
    if pDevmode <> nil then
    try
      with pDevmode^ do
      begin
        {modify paper size}
        dmPapersize := DMPAPER_B5;
        {tell printer driver that dmPapersize field contains data it needs to inspect}
        dmFields := dmFields or DM_PAPERSIZE;
      end;
    finally
      {unlock DEVMODE handle}
      GlobalUnlock(Devmode);
    end;
  end;
end;


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);
type
  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;
var
  Device, Driver, Port: array[0..255] of Char;
  hDevMode: THandle;
  i, numBinNames, numBins, temp: Integer;
  pBinNames: PBinnameArray;
  pBins: PBinArray;
begin
  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
  begin
    raise Exception.Create('DeviceCapabilities reports different number of bins and ' + 'bin names!');
  end;
  if numBinNames > 0 then
  begin
    pBins := nil;
    GetMem(pBinNames, numBinNames * Sizeof(TBinname));
    GetMem(pBins, numBins * Sizeof(Word));
    try
      WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, Pchar(pBinNames), nil);
      WinSpool.DeviceCapabilities(Device, Port, DC_BINS, Pchar(pBins), nil);
      sl.clear;
      for i := 1 to numBinNames do
      begin
        temp := pBins^[i];
        sl.addObject(pBinNames^[i], TObject(temp));
      end;
    finally
      FreeMem(pBinNames);
      if pBins <> nil then
        FreeMem(pBins);
    end;
  end;
end;


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