Saturday, January 30, 2010

Custom Page Size


PROCEDURE AddPaperId
LPARAMETERS cPaperName,nPaperHeight,nPaperWidth
nPaperHeight = nPaperHeight &&ROUND(nPaperHeight * 10000,0)
nPaperWidth = nPaperWidth &&ROUND(nPaperWidth * 10000,0)

Local hPrinter,lReturn
Local cPrinterName && , cPaperName
Local pPaperName, sPaperSize
Local nResult, nBufLen &&, nPaperWidth, nPaperHeight

Declare Long GetLastError In Kernel32
Declare Long ClosePrinter In WinSpool.Drv Long hPrinter
Declare Long OpenPrinter In WinSpool.Drv ;
String cPrinterName, Long @O_hPrinter, Long pDefault

Declare Long GetForm In WinSpool.drv As GetPrinterForm ;
Long hPrinter, String pFormName, ;
Long nLevelInfo, String @O_pFormInfo, ;
Long nBufSize, Long @O_nBufNeeded

Declare Long AddForm In WinSpool.drv As AddPrinterForm ;
Long hPrinter, Long nLevelInfo, String @pFormInfo

Declare Long LocalAlloc In Kernel32 Long uFlags, Long dwBytes
Declare Long LocalFree In Kernel32 Long Hmem

cPrinterName = Set( 'Printer', 2 ) && Get default Windows printer
hPrinter = 0

lReturn = .f.

If (OpenPrinter( cPrinterName, @hPrinter, 0 ) != 0)
* cPaperName = 'MyCustom-Half A4'
nBufLen = 32 && FORM_INFO_1_Size
cInfo = Replicate( Chr(0), 32 )
nResult = GetPrinterForm( hPrinter, cPaperName, 1, ;
@cInfo, nBufLen, @nBufLen )

If (nResult == 0) && Get printer form failed
nResult = GetLastError()

If (nResult == 1902) && ERROR_INVALID_FORM_NAME
** Custom Printer Form not exist, add the new one
* nPaperWidth = 210000 / 2 && Paper size is in 1/1000 millimeters
* nPaperHeight = 297000 / 2
sPaperSize = BinToC( nPaperWidth, '4rs' ) + BinToC( nPaperHeight, '4rs' )
pPaperName = LocalAlloc( 64, 32 )

If (pPaperName != 0)
Sys( 2600, pPaperName, Len( cPaperName ), cPaperName )
cInfo = BinToC( 0, '4rs' ) + BinToC( pPaperName, '4rs' ) + ;
sPaperSize + BinToC( 0, '4rs' ) + BinToC( 0, '4rs' ) + sPaperSize

If (AddPrinterForm( hPrinter, 1, cInfo ) != 0)
lReturn = .t.
*? 'Custom paper form (' + cPaperName + ') has been added! '
ELSE
lReturn = .f.
*? 'Error:', GetLastError()
Endif

LocalFree( pPaperName )
Endif

Else
If (nResult == 122) && Insufficient buffer
* ? 'Error: Custom Paper Form already exist!'
Else
* ? 'Error: ', nResult
Endif
Endif
Else
* ? 'Error: ', nResult
Endif
ClosePrinter( hPrinter )
ENDIF
RETURN

3 comments:

  1. Running on Windows 7 64 bit, the following error appears:

    "Cannot find entry point GETFORM in the DLL"

    That happends on line:

    nResult = GetPrinterForm( hPrinter, cPaperName, 1, ;
    @cInfo, nBufLen, @nBufLen )

    Any suggestions ?

    ReplyDelete
  2. How to edit existing custom page size

    ReplyDelete
    Replies
    1. You can Delete Old Paper Size then you add name paper size with same name.

      Delete