Monday, February 1, 2010

Find Paper ID Programmatically

****************** Find Paper Size ID from Printer ****************

PROCEDURE FindPaperID
PARAMETERS lc_FindPaperName

#Define DC_PAPERS 2
#Define DC_PAPERS_Size 2
#Define DC_PAPERNAMES 16
#Define DC_PAPERNAMES_Size 64
Declare Long DeviceCapabilities In WinSpool.drv ;
String cPrinterName, String cPort, Short nCapFlags, ;
String @O_cBuffer, Long pDevMode

Local Array la_Printer[1]
Local ln_Row, ln_Result, ln_I, ln_Index
Local lc_PrinterName, lc_Buffer
Local lc_PaperSizeID, lc_PaperName,nPaperID
nPaperId=0

lc_PrinterName = Set( 'Printer', 2 ) && Get default windows printer
= Aprinters( la_Printer )
ln_Row = Ascan( la_Printer, lc_PrinterName, 1, 0, 0, 9 )
ln_Result = DeviceCapabilities( la_Printer[ ln_Row, 1 ], ;
la_Printer[ ln_Row, 2 ], DC_PAPERNAMES, 0, 0 )
If (ln_Result > 0)
ln_Index = -1
* lc_FindPaperName = Upper( 'MyCustom - Half A4' )
lc_Buffer = Replicate( Chr(0), ln_Result * DC_PAPERNAMES_Size )
ln_Result = DeviceCapabilities( la_Printer[ ln_Row, 1 ], ;
la_Printer[ ln_Row, 2 ], DC_PAPERNAMES, @lc_Buffer, 0 )
For ln_I = 0 To ln_Result-1
lc_PaperName = Upper( Substr( lc_Buffer, (ln_I * DC_PAPERNAMES_Size )+1, ;
DC_PAPERNAMES_Size ))
lc_papername = SUBSTR(lc_papername,1,LEN(lc_FindPaperName))

If (UPPER(lc_FindPaperName) $ upper(lc_PaperName))
ln_Index = ln_I
Exit
Endif
NEXT
* ? "Ok"
If (ln_Index != -1)
** Paper Name found
** Get PaperSize ID
ln_Result = DeviceCapabilities( la_Printer[ ln_Row, 1 ], ;
la_Printer[ ln_Row, 2 ], DC_PAPERS, 0, 0 )

If (ln_Result > 0)
lc_Buffer = Replicate( Chr(0), ln_Result * DC_PAPERS_Size )
ln_Result = DeviceCapabilities( la_Printer[ ln_Row, 1 ], ;
la_Printer[ ln_Row, 2 ], DC_PAPERS, @lc_Buffer, 0 )
lc_PaperSizeID = Substr( lc_Buffer, (ln_Index * DC_PAPERS_Size )+1, DC_PAPERS_Size )
* ? 'PaperSize ID for "' + lc_FindPaperName + '" is', CToBin( lc_PaperSizeID, '2rs' )
nPaperId = CToBin( lc_PaperSizeID, '2rs')
ENDIF
ENDIF
ENDIF

RETURN nPaperId

1 comment:

  1. What should I do to select the format found in the printer?

    ReplyDelete