Re: Barcodes in PDF without saving to file.
Posted: Sun Aug 13, 2017 3:01 pm
Hello Nikos, I'm on a fortnightly vacation, happily without a PC. When I get back to work and clean up the mess, I'll prepare an example.
Edward
Edward
Exclusive forum for HMG, a Free / Open Source xBase WIN32/64 Bits / GUI Development System
http://hmgforum.com/
And I also my friend! I hope you're having fun!!
Of course, I understand Angel,
Here is an sample (made in a rush) .
Code: Select all
#include "hmg.ch"
Function Main()
SELECT HPDFDOC "sample.pdf" PAPERLENGTH 300 PAPERWIDTH 300 LOG
SET HPDFDOC ENCODING TO "WinAnsiEncoding"
START HPDFDOC
START HPDFPAGE
@ 10, 10 HPDFPRINT "EAN13 ShowDigits, Checksum"
HPDFDrawBarcode( 10, 100, "477012345678", "EAN13", 1 ,25, .T., .T.)
@ 20, 10 HPDFPRINT "EAN8 Checksum"
HPDFDrawBarcode( 20, 100, "1234567", "EAN8", 1, 20, .F., .T. )
@ 30, 10 HPDFPRINT "UPCA"
HPDFDrawBarcode( 30, 100, "01234567891", "UPCA", 1, 20 )
@ 40, 10 HPDFPRINT "UPCE"
HPDFDrawBarcode( 40, 100, "123456", "UPCE", 1)
@ 50, 10 HPDFPRINT "CODE39 ShowDigits"
HPDFDrawBarcode( 50, 100, "ABC123", "CODE39", 1, Nil , .T.)
@ 60, 10 HPDFPRINT "CODE39 ShowDigits, CheckSum"
HPDFDrawBarcode( 60, 100, "ABC123", "CODE39", 1, 20 , .T., .T.)
@ 70, 10 HPDFPRINT "CODE39 ShowDigits, Checksum, Wide2.5"
HPDFDrawBarcode( 70, 100, "ABC123", "CODE39", 0.5, 20, .T., .T., .T.)
@ 80, 10 HPDFPRINT "CODE39 ShowDigits, Checksum, Wide3"
HPDFDrawBarcode( 80, 100, "ABC123", "CODE39", 1, Nil, .T., .T. ,.F., .T.)
@ 90, 10 HPDFPRINT "ITF Wide3"
HPDFDrawBarcode( 90, 100, "1234", "ITF", 1, 20, .F., .F., .F., .T.)
@100, 10 HPDFPRINT "ITF ShowDigits, Checksum"
HPDFDrawBarcode(100, 100, "12345678901", "ITF", 1, Nil, .T., .T.)
@110, 10 HPDFPRINT "MSI"
HPDFDrawBarcode(110, 100, "1234", "MSI", 1)
@120, 10 HPDFPRINT "MSI ShowDigits, Checksum, Wide3"
HPDFDrawBarcode(120, 100, "1234", "MSI", 1, Nil, .T., .T., .F., .T.)
@130, 10 HPDFPRINT "MSI ShowDigits, Checksum"
HPDFDrawBarcode(130, 100, "1234567", "MSI", 1, Nil, .T., .T.)
@140, 10 HPDFPRINT "CODABAR Wide3"
HPDFDrawBarcode(140, 100, "40156", "CODABAR", Nil, Nil, Nil, Nil, Nil, .T.)
@150, 10 HPDFPRINT "CODABAR ShowDigits, Wide3"
HPDFDrawBarcode(150, 100, "-1234", "CODABAR", 1, 20, .T., .F., .F., .T.)
@160, 10 HPDFPRINT "CODE93"
HPDFDrawBarcode(160, 100, "ABC-123", "CODE93", 1)
@170, 10 HPDFPRINT "CODE93"
HPDFDrawBarcode(170, 100, "TEST93", "CODE93")
@180, 10 HPDFPRINT "CODE11 ShowDigits, Wide3"
HPDFDrawBarcode(180, 100, "12", "CODE11", 1, Nil, .T., .F., .F., .T.)
@190, 10 HPDFPRINT "CODE11 ShowDigits, Checksum, Wide3"
HPDFDrawBarcode(190, 100, "1234567890", "CODE11", 1, Nil, .T., .T., .F., .T.)
@200, 10 HPDFPRINT "CODE128"
HPDFDrawBarcode(200, 100, "Code 128", "CODE128", 1, 20, .F.)
@210, 10 HPDFPRINT "CODE128 ShowDigits"
HPDFDrawBarcode(210, 100, "1234567890", "CODE128", 1, 20 ,.T.)
@220, 10 HPDFPRINT "CODE128"
HPDFDrawBarcode(220, 100, "Wikipedia", "CODE128")
END HPDFPAGE
START HPDFPAGE
@10, 10 HPDFPRINT "PDF417"
HPDFDrawBarcode(10, 100, "Hello, World of Harbour!!! It's 2D barcode PDF417 :)", "PDF417", 1)
@40, 10 HPDFPRINT "DATAMATRIX"
HPDFDrawBarcode(40, 100, "Hello, World of Harbour!!! It's 2D barcode DataMatrix :)", "DATAMATRIX", 2)
@70, 10 HPDFPRINT "QRCODE"
HPDFDrawBarcode(70, 100, "http://harbour-project.org/", "QRCODE", 2)
END HPDFPAGE
END HPDFDOC
Execute File 'sample.pdf'
Return Nil
*******************************
PROCEDURE HPDFDrawBarcode( nRow, nCol, cCode, cType, nLineWidth, nLineHeight, lShowDigits, lCheckSum, lWide2_5, lWide3 )
Local hZebra, cTxt, nFlags, nSizeWidth, nTextWidth
Local hPdf := _HMG_SYSDATA[ 150 ][ 1 ]
Local hPage := _HMG_SYSDATA[ 150 ][ 7 ]
Local nWidth := _HMG_SYSDATA[ 150 ][ 4 ]
Local nHeight := _HMG_SYSDATA[ 150 ][ 5 ]
Local nxPos := _HMG_HPDF_MM2Pixel( nCol )
Local nyPos := nHeight - _HMG_HPDF_MM2Pixel( nRow )
Local cFont := "Helvetica"
Local nFontSize := 9
Local nTextHeight := 0
DEFAULT nLineWidth := 1
DEFAULT nLineHeight := 18
DEFAULT lCheckSum := .F.
DEFAULT lWide2_5 := .F.
DEFAULT lWide3 := .F.
DEFAULT lShowDigits := .F.
nFlags := 0
IF lChecksum
nFlags := nFlags + HB_ZEBRA_FLAG_CHECKSUM
ENDIF
IF lWide2_5
nFlags := nFlags + HB_ZEBRA_FLAG_WIDE2_5
ENDIF
IF lWide3
nFlags := nFlags + HB_ZEBRA_FLAG_WIDE3
ENDIF
IF nFlags == 0
nFlags := Nil
ENDIF
SWITCH cType
CASE "EAN13" ; hZebra := hb_zebra_create_ean13( cCode, nFlags ) ; EXIT
CASE "EAN8" ; hZebra := hb_zebra_create_ean8( cCode, nFlags ) ; EXIT
CASE "UPCA" ; hZebra := hb_zebra_create_upca( cCode, nFlags ) ; EXIT
CASE "UPCE" ; hZebra := hb_zebra_create_upce( cCode, nFlags ) ; EXIT
CASE "CODE39" ; hZebra := hb_zebra_create_code39( cCode, nFlags ) ; EXIT
CASE "ITF" ; hZebra := hb_zebra_create_itf( cCode, nFlags ) ; EXIT
CASE "MSI" ; hZebra := hb_zebra_create_msi( cCode, nFlags ) ; EXIT
CASE "CODABAR" ; hZebra := hb_zebra_create_codabar( cCode, nFlags ) ; EXIT
CASE "CODE93" ; hZebra := hb_zebra_create_code93( cCode, nFlags ) ; EXIT
CASE "CODE11" ; hZebra := hb_zebra_create_code11( cCode, nFlags ) ; EXIT
CASE "CODE128" ; hZebra := hb_zebra_create_code128( cCode, nFlags ) ; EXIT
CASE "PDF417" ; hZebra := hb_zebra_create_pdf417( cCode, nFlags ); nLineHeight := nLineWidth * 3 ; lShowDigits := .f. ; EXIT
CASE "DATAMATRIX" ; hZebra := hb_zebra_create_datamatrix( cCode, nFlags ); nLineHeight := nLineWidth ; lShowDigits := .f. ; EXIT
CASE "QRCODE" ; hZebra := hb_zebra_create_qrcode( cCode, nFlags ); nLineHeight := nLineWidth ; lShowDigits := .f. ; EXIT
ENDSWITCH
IF hZebra != NIL
IF hb_zebra_geterror( hZebra ) == 0
IF lShowDigits
cTxt := ALLTRIM(hb_zebra_getcode( hZebra ))
nSizeWidth := HMG_Zebra_GetWidth (hZebra, nLineWidth, nLineHeight, NIL)
nSizeHeight := HMG_Zebra_GetHeight (hZebra, nLineWidth, nLineHeight, NIL)
HPDF_Page_SetFontAndSize( hPage, HPDF_GetFont( hPdf, cFont, NIL ), nFontSize )
nTextWidth := HPDF_Page_TextWidth( hPage, cTxt )
nTextHeight:= nFontSize - 1
HPDF_Page_BeginText( hPage )
HPDF_Page_TextOut( hPage, (nxPos + ( nSizeWidth / 2 )) - ( nTextWidth / 2 ), nyPos - nSizeHeight, cTxt )
HPDF_Page_EndText( hPage )
ENDIF
hb_zebra_draw_hpdf( hZebra, hPage, nxPos, nyPos, nLineWidth, -(nLineHeight-nTextHeight))
ELSE
MsgInfo ("Type "+ cType + CRLF +"Code "+ cCode+ CRLF+ "Error "+LTrim(hb_valtostr(hb_zebra_geterror(hZebra))))
ENDIF
hb_zebra_destroy( hZebra )
ELSE
MsgStop("Invalid barcode type !", cType)
ENDIF
RETURN
***************************************
STATIC FUNCTION hb_zebra_draw_hpdf( hZebra, hPage, ... )
IF hb_zebra_geterror( hZebra ) != 0
RETURN HB_ZEBRA_ERROR_INVALIDZEBRA
ENDIF
hb_zebra_draw( hZebra, {| x, y, w, h | HPDF_Page_Rectangle( hPage, x, y, w, h ) }, ... )
HPDF_Page_Fill( hPage )
RETURN 0
******************************************************
*-----------------------------------------------------------------------------------------------*
FUNCTION HMG_Zebra_GetWidth (hZebra, nLineWidth, nLineHeight, iFlags)
*-----------------------------------------------------------------------------------------------*
LOCAL x1:= 0, y1 := 0, nBarWidth := 0, nBarHeight := 0
// always --> nBarHeight = nLineHeight
IF hb_zebra_GetError( hZebra ) != 0
RETURN HB_ZEBRA_ERROR_INVALIDZEBRA
ENDIF
// hb_zebra_draw ( hZebra, bCodeBlock, dX, dY, dWidth, dHeight, iFlags )
hb_zebra_draw ( hZebra, {| x, y, w, h | nBarWidth:=x+w-x1, nBarHeight:=y+h-y1 }, x1, y1, nLineWidth, nLineHeight, iFlags )
RETURN nBarWidth
*-----------------------------------------------------------------------------------------------*
FUNCTION HMG_Zebra_GetHeight (hZebra, nLineWidth, nLineHeight, iFlags)
*-----------------------------------------------------------------------------------------------*
LOCAL x1:= 0, y1 := 0, nBarWidth := 0, nBarHeight := 0
// always --> nBarHeight = nLineHeight
IF hb_zebra_GetError( hZebra ) != 0
RETURN HB_ZEBRA_ERROR_INVALIDZEBRA
ENDIF
// hb_zebra_draw ( hZebra, bCodeBlock, dX, dY, dWidth, dHeight, iFlags )
hb_zebra_draw ( hZebra, {| x, y, w, h | nBarWidth:=x+w-x1, nBarHeight:=y+h-y1 }, x1, y1, nLineWidth, nLineHeight, iFlags )
RETURN nBarHeight