// Sample code for sending text to a printer, and also for printing barcodes following the code128 standard. // Send text-array to printer (any array of strings and barcodes) // aText is for example : {{'T','Any text'},{'B','BCC123'}} == textline 'Any text', barcode 'BCC123' procedure DoprintTxtArr( aText ) local aPlijst, aForms := {}, aSize := {} local lFout := .F. local nI, nPrinter, nForm, nFormID local oPrinter, oDlg, oPrintlist, oParent, oOwner, oMess, oFoutje local bLasthandler local cName // Make list of all available printers. oPrinter := XbpPrinter():new() aPlijst := oPrinter:list() if aPlijst == NIL .or. empty( aPlijst ) msgbox( "No printers available." } else // Let user select one oDlg := XbpPrintDialog():new() oDlg:create() bLasthandler := errorblock( { |e| break(e) } ) begin sequence oPrinter := oDlg:display() // Maak een printer-object van de gekozen printer recover using oFoutje lFout := .T. msgbox( "No printer chosen, or the chosen printer cannot be used." } endsequence errorblock( bLasthandler ) oDlg:destroy() endif if ! lFout if oPrinter <> NIL // Specify a format to be used (printer dependent, in my case it is a labelprinter) aForms := oPrinter:forms() nForm := ascan( aForms, {|x| x[2] == 'USER'} ) nFormID := aForms[nForm,1] oPrinter:setFormSize( nFormID ) aSize := oPrinter:paperSize() // Do the actual printing LabelAfdruk( aText, oPrinter ) endif endif return // Send a text array to the printer procedure LabelAfdruk( aPrint, oPrinter ) local nY, nYtop, nYint, nLmarge, nI local oPrinterPS, oFont // Presentation space for printer. Sizes in 1/10 mm. Total X, Total Y, X-margin left, Y-margin bottom, X-margin right, Y-margin top oPrinterPS := XbpPresSpace():New() oPrinterPS:Create( oPrinter, {1040,742,40,40,1000,702}, GRA_PU_LOMETRIC ) // Zorg dat het "device" ook rekening houdt met deze papiermaten oPrinterPS:setPageSize( {1040,742}, GRA_PU_LOMETRIC ) // Create font object oFont := XbpFont():new( oPrinterPS ) oFont:create( FONT_COURIER_SMALL + FONT_STYLE_BOLD ) // Begin print document oPrinterPS:device():startDoc() // Set font for printer GraSetFont( oPrinterPS, oFont ) // Set left margin nLmarge := 40 // Set Y value for top of paper nYtop := 702 // Set Y-shift per line nYint := nYtop / 16 nY := nYtop - nYint for nI := 1 to len( aPrint ) if aPrint[nI,1] == 'T' nY -= nYint // Set next Y-position GraStringAt( oPrinterPS, {nLmarge,nY}, aPrint[nI,2] ) // Print line elseif aPrint[nI,1] == 'B' nY -= nYint // Set next Y-position nY -= nYint // Barcode takes two lines SchrijfBarcode( oPrinterPS, nLmarge, nY, nYint, aPrint[nI,2] ) endif next oPrinterPS:device():endDoc() // Destroy printer presentation space DestroyDevice( oPrinterPS ) return // Voor making a barcode in oPrinterPS document followint the code128 (A or B) standard procedure SchrijfBarcode( oPrinterPS, nXstart, nYlow, nYint, cCode ) local cCheck, cBarcode, cCodetabel, cTest local nI, nX, nY, nYtop, nXbreed, nXbox // Bepaal volgens welke tabel de barcode opgebouwd moet worden cCodetabel := 'START A' // Stel dan de barcode samen in enen en nullen cBarcode := Code128( cCodetabel, 'PATROON', cCodetabel ) // Begin with the start-code for nI := 1 to len( cCode ) cTest := Code128( cCode[nI], 'PATROON', cCodetabel ) // Add bit pattern for each character in the code, allowing inspection of pattern cBarcode += cTest next cCheck := BarcodeCheckDigit( cCode, cCodetabel ) // Find the checksum character for the barcode cBarcode += Code128( cCheck, 'PATROON', cCodetabel ) // Add the checksum bit-pattern cBarcode += '1100011101011' // Finally add the stop-code bit pattern // Find the lower end of the barcode nY := nYlow + (nYint / 2) // Find the top of the barcode stripes nYtop := nYlow + (nYint * 2) - (nYint / 2) // Set the width of each barcode unit nXbreed := 2.5 // Determine start-X richt of the quiet zone nX := nXstart + (11 * nXbreed) nI := 1 // Write the barcode stripes do while nI <= len( cBarcode ) if cBarcode[nI] == '0' // Move start-x one stripe for each '0' in the bit pattern, making a whitespace stripe nX += nXbreed ++ nI elseif cBarcode[nI] == '1' // Determine the width of black stripe. Maximum is 4 units, representing dan bit-pattern '1111' nXbox := nXbreed ++ nI if nI <= len( cBarcode ) .and. cBarcode[nI] == '1' nXbox += nXbreed ++ nI if nI <= len( cBarcode ) .and. cBarcode[nI] == '1' nXbox += nXbreed ++ nI if nI <= len( cBarcode ) .and. cBarcode[nI] == '1' nXbox += nXbreed ++ nI endif endif endif // Then, finally, draw the stripe as a filled box GraBox( oPrinterPS, {nX,nY}, {nX + nXbox,nYtop}, GRA_OUTLINEFILL ) nX += nXbox endif enddo return // Determining checksum character voor a code function BarcodeCheckDigit( cCode, cCodetabel ) local cCheckSumValue, nCheckSumDigit, nCheckTotal, nStartwaarde cCode := alltrim( cCode ) // Determine start-code, this is either START A or START B, depending on which code126 variant you want to use nStartwaarde := Code128( cCodetabel, 'WAARDE', cCodetabel ) nCheckTotal := nStartwaarde // Not really necessary // Calculate a running total for this code for nI := 1 to len( cCode ) nCheckTotal += nI * Code128( cCode[nI], 'WAARDE', cCodetabel ) next // Find the modulo of the total nCheckSumDigit := mod( nCheckTotal, nStartwaarde ) // Find the checksum character cCheckSumValue := Code128( nCheckSumDigit, 'KARAKTER', cCodetabel ) return( cCheckSumValue ) // For finding the corresponding number, character or bit pattern for any number or character // following the Code128A of Code128B standard function Code128( xZoeker, cVraag, cCodetabel ) local nI local xResult local aCode STATIC aCodeB := {{0 ,'SP' ,'11011001100'},; {1 ,'!' ,'11001101100'},; {2 ,'"' ,'11001100110'},; {3 ,'#' ,'10010011000'},; {4 ,'$' ,'10010001100'},; {5 ,'%' ,'10001001100'},; {6 ,'&' ,'10011001000'},; {7 ,"'" ,'10011000100'},; {8 ,'(' ,'10001100100'},; {9 ,')' ,'11001001000'},; {10 ,'*' ,'11001000100'},; {11 ,'+' ,'11000100100'},; {12 ,',' ,'10110011100'},; {13 ,'-' ,'10011011100'},; {14 ,'.' ,'10011001110'},; {15 ,'/' ,'10111001100'},; {16 ,'0' ,'10011101100'},; {17 ,'1' ,'10011100110'},; {18 ,'2' ,'11001110010'},; {19 ,'3' ,'11001011100'},; {20 ,'4' ,'11001001110'},; {21 ,'5' ,'11011100100'},; {22 ,'6' ,'11001110100'},; {23 ,'7' ,'11101101110'},; {24 ,'8' ,'11101001100'},; {25 ,'9' ,'11100101100'},; {26 ,':' ,'11100100110'},; {27 ,';' ,'11101100100'},; {28 ,'<' ,'11100110100'},; {29 ,'=' ,'11100110010'},; {30 ,'>' ,'11011011000'},; {31 ,'?' ,'11011000110'},; {32 ,'@' ,'11000110110'},; {33 ,'A' ,'10100011000'},; {34 ,'B' ,'10001011000'},; {35 ,'C' ,'10001000110'},; {36 ,'D' ,'10110001000'},; {37 ,'E' ,'10001101000'},; {38 ,'F' ,'10001100010'},; {39 ,'G' ,'11010001000'},; {40 ,'H' ,'11000101000'},; {41 ,'I' ,'11000100010'},; {42 ,'J' ,'10110111000'},; {43 ,'K' ,'10110001110'},; {44 ,'L' ,'10001101110'},; {45 ,'M' ,'10111011000'},; {46 ,'N' ,'10111000110'},; {47 ,'O' ,'10001110110'},; {48 ,'P' ,'11101110110'},; {49 ,'Q' ,'11010001110'},; {50 ,'R' ,'11000101110'},; {51 ,'S' ,'11011101000'},; {52 ,'T' ,'11011100010'},; {53 ,'U' ,'11011101110'},; {54 ,'V' ,'11101011000'},; {55 ,'W' ,'11101000110'},; {56 ,'X' ,'11100010110'},; {57 ,'Y' ,'11101101000'},; {58 ,'Z' ,'11101100010'},; {59 ,'[' ,'11100011010'},; {60 ,'\' ,'11101111010'},; {61 ,']' ,'11001000010'},; {62 ,' ' ,'11110001010'},; {63 ,'_' ,'10100110000'},; {64 ,'NUL' ,'10100001100'},; {65 ,'a' ,'10010110000'},; {66 ,'b' ,'10010000110'},; {67 ,'c' ,'10000101100'},; {68 ,'d' ,'10000100110'},; {69 ,'e' ,'10110010000'},; {70 ,'f' ,'10110000100'},; {71 ,'g' ,'10011010000'},; {72 ,'h' ,'10011000010'},; {73 ,'I' ,'10000110100'},; {74 ,'j' ,'10000110010'},; {75 ,'k' ,'11000010010'},; {76 ,'l' ,'11001010000'},; {77 ,'m' ,'11110111010'},; {78 ,'n' ,'11000010100'},; {79 ,'o' ,'10001111010'},; {80 ,'p' ,'10100111100'},; {81 ,'q' ,'10010111100'},; {82 ,'r' ,'10010011110'},; {83 ,'s' ,'10111100100'},; {84 ,'t' ,'10011110100'},; {85 ,'u' ,'10011110010'},; {86 ,'v' ,'11110100100'},; {87 ,'w' ,'11110010100'},; {88 ,'x' ,'11110010010'},; {89 ,'y' ,'11011011110'},; {90 ,'z' ,'11011110110'},; {91 ,'{' ,'11110110110'},; {92 ,'|' ,'10101111000'},; {93 ,'}' ,'10100011110'},; {94 ,'~' ,'10001011110'},; {95 ,'US' ,'10111101000'},; {96 ,'FNC3' ,'10111100010'},; {97 ,'FNC2' ,'11110101000'},; {98 ,'SHIFT' ,'11110100010'},; {99 ,'Code C' ,'10111011110'},; {100,'Code B' ,'10111101110'},; {101,'FNC4' ,'11101011110'},; {102,'FNC1' ,'11110101110'},; {103,'START A','11010000100'},; {104,'START B','11010010000'},; {105,'START C','11010011100'}} STATIC aCodeA := {{0 ,'SP' ,'11011001100'},; {1 ,'!' ,'11001101100'},; {2 ,'"' ,'11001100110'},; {3 ,'#' ,'10010011000'},; {4 ,'$' ,'10010001100'},; {5 ,'%' ,'10001001100'},; {6 ,'&' ,'10011001000'},; {7 ,"'" ,'10011000100'},; {8 ,'(' ,'10001100100'},; {9 ,')' ,'11001001000'},; {10 ,'*' ,'11001000100'},; {11 ,'+' ,'11000100100'},; {12 ,',' ,'10110011100'},; {13 ,'-' ,'10011011100'},; {14 ,'.' ,'10011001110'},; {15 ,'/' ,'10111001100'},; {16 ,'0' ,'10011101100'},; {17 ,'1' ,'10011100110'},; {18 ,'2' ,'11001110010'},; {19 ,'3' ,'11001011100'},; {20 ,'4' ,'11001001110'},; {21 ,'5' ,'11011100100'},; {22 ,'6' ,'11001110100'},; {23 ,'7' ,'11101101110'},; {24 ,'8' ,'11101001100'},; {25 ,'9' ,'11100101100'},; {26 ,':' ,'11100100110'},; {27 ,';' ,'11101100100'},; {28 ,'<' ,'11100110100'},; {29 ,'=' ,'11100110010'},; {30 ,'>' ,'11011011000'},; {31 ,'?' ,'11011000110'},; {32 ,'@' ,'11000110110'},; {33 ,'A' ,'10100011000'},; {34 ,'B' ,'10001011000'},; {35 ,'C' ,'10001000110'},; {36 ,'D' ,'10110001000'},; {37 ,'E' ,'10001101000'},; {38 ,'F' ,'10001100010'},; {39 ,'G' ,'11010001000'},; {40 ,'H' ,'11000101000'},; {41 ,'I' ,'11000100010'},; {42 ,'J' ,'10110111000'},; {43 ,'K' ,'10110001110'},; {44 ,'L' ,'10001101110'},; {45 ,'M' ,'10111011000'},; {46 ,'N' ,'10111000110'},; {47 ,'O' ,'10001110110'},; {48 ,'P' ,'11101110110'},; {49 ,'Q' ,'11010001110'},; {50 ,'R' ,'11000101110'},; {51 ,'S' ,'11011101000'},; {52 ,'T' ,'11011100010'},; {53 ,'U' ,'11011101110'},; {54 ,'V' ,'11101011000'},; {55 ,'W' ,'11101000110'},; {56 ,'X' ,'11100010110'},; {57 ,'Y' ,'11101101000'},; {58 ,'Z' ,'11101100010'},; {59 ,'[' ,'11100011010'},; {60 ,'\' ,'11101111010'},; {61 ,']' ,'11001000010'},; {62 ,' ' ,'11110001010'},; {63 ,'_' ,'10100110000'},; {64 ,'NUL' ,'10100001100'},; {65 ,'SOH' ,'10010110000'},; {66 ,'STX' ,'10010000110'},; {67 ,'ETX' ,'10000101100'},; {68 ,'EOT' ,'10000100110'},; {69 ,'ENQ' ,'10110010000'},; {70 ,'ACK' ,'10110000100'},; {71 ,'BEL' ,'10011010000'},; {72 ,'BS' ,'10011000010'},; {73 ,'HT' ,'10000110100'},; {74 ,'LF' ,'10000110010'},; {75 ,'VT' ,'11000010010'},; {76 ,'FF' ,'11001010000'},; {77 ,'CR' ,'11110111010'},; {78 ,'SO' ,'11000010100'},; {79 ,'SI' ,'10001111010'},; {80 ,'DLE' ,'10100111100'},; {81 ,'DC1' ,'10010111100'},; {82 ,'DC2' ,'10010011110'},; {83 ,'DC3' ,'10111100100'},; {84 ,'DC4' ,'10011110100'},; {85 ,'NAK' ,'10011110010'},; {86 ,'SYN' ,'11110100100'},; {87 ,'ETB' ,'11110010100'},; {88 ,'CAN' ,'11110010010'},; {89 ,'EM' ,'11011011110'},; {90 ,'SUB' ,'11011110110'},; {91 ,'ESC' ,'11110110110'},; {92 ,'FS' ,'10101111000'},; {93 ,'GS' ,'10100011110'},; {94 ,'RS' ,'10001011110'},; {95 ,'US' ,'10111101000'},; {96 ,'FNC3' ,'10111100010'},; {97 ,'FNC2' ,'11110101000'},; {98 ,'SHIFT' ,'11110100010'},; {99 ,'Code C ','10111011110'},; {100,'Code B ','10111101110'},; {101,'FNC4' ,'11101011110'},; {102,'FNC1' ,'11110101110'},; {103,'START A','11010000100'},; {104,'START B','11010010000'},; {105,'START C','11010011100'}} // Stel de te gebruiken tabel in if cCodetabel == 'START A' aCode := aCodeA elseif cCodetabel == 'START B' aCode := aCodeB endif if cVraag == 'KARAKTER' nI := ascan( aCode, {|x| x[1] == xZoeker} ) if nI == 0 // Fout, niet gevonden xResult := '' else xResult := aCode[nI,2] endif else // Zoek met het karakter de juiste regel nI := ascan( aCode, {|x| x[2] == xZoeker } ) if nI == 0 // Fout, niet gevonden if cVraag == 'WAARDE' xResult := 0 elseif cVraag == 'PATROON' xResult := ' ' endif else if cVraag == 'WAARDE' xResult := aCode[nI,1] elseif cVraag == 'PATROON' xResult := aCode[nI,3] endif endif endif return( xResult )