Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.612
  • Views Posts: 542.464
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

VB.NET - GENERARE E STAMPARE CODICE A BARRA "BarCode" CON LA LIBRERIA iTextSharp (Codice)

VB.NET - GENERARE E STAMPARE CODICE A BARRA "BarCode" CON iTextSharp (Codice)


  


Oggi vi voglio mettere a disposizione un piccolo progetto per generare codici a Barra utilizzando la libreria iTextSharp che la potete scaricare al seguente indirizzo Web Ufficiale Download iTextSharp link alternativo Download Libreria iTextSharp . Vi ricordo che la funzione restituisce una immagine è utile per fare delle operazioni, tipo aggiungerci delle scritte o quant'altro.

Infondo alla pagina troverete un piccolo progetto che sfrutta la libreria Open Source di iTextSharp. 

La funzione deve essere richiamata come segue : 

        Dim ValueCodeBar As String
        ValueCodeBar = "*1234567890*"

        Dim ImgBarCode128 As System.Drawing.Image
        ImgBarCode128 = GeneraBarcode128(ValueCodeBar)
       
        'Nel caso che si vuole salvare in una immagine il BarCode128
        'Dim FileName As String  = "c:\BarCode128.jpg"
        'ImgBarCode128 = GeneraBarcode128(ValueCodeBar, FileName)

 

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET:

   Private Function GeneraBarcode128(ByVal ValoreCodeBare As String, Optional ByVal FileName As String = Nothing) As System.Drawing.Image
        Try
            Dim Salva As System.Drawing.Image
            Dim Estenzione As String = Nothing

            Dim barcode As New iTextSharp.text.pdf.Barcode128()
            barcode.BarHeight = Height
            barcode.ChecksumText = True
            barcode.GenerateChecksum = True
            barcode.Code = ValoreCodeBare
            Salva = barcode.CreateDrawingImage(Color.Black, Color.White)

            If FileName <> Nothing Then
                Select Case FileName.Substring(FileName.Length - 4, 4).ToString

                    Case ".jpg"
                        Exit Select
                    Case ".bmp"
                        Exit Select
                    Case ".png"
                        Exit Select
                    Case Else
                        Estenzione = ".jpg"
                End Select
                FileName = FileName & Estenzione
                Salva.Save(FileName)
            End If

            Return Salva
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

Qui di seguito vi aggiungo la funzione che serve per aggiungere il codice o valore sotto il BarCode generato :

       Dim ValueCodeBar As String
        ValueCodeBar = "*1234567890*"

        Dim ImgBarCode128 As System.Drawing.Image
        ImgBarCode128 = GeneraBarcode128(ValueCodeBar)

        'Aggiungo la scritta *1234567890* all'immagine sotto al Codice a Barra
        ImgBarCode128 = CatturaBarCodeImg(ValueCodeBar, ImgBarCode128 , "c:\BarCode128.jpg")
       

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET:

    Private Function CatturaBarCodeImg(ByVal ValueCodeBar As String, ByVal BarCodeImg As System.Drawing.Image, Optional ByVal FileName As String = Nothing)
        Try

            Dim Bitmap As New Bitmap(BarCodeImg.Width + 30, BarCodeImg.Height + 30)
            Dim Graphic As Graphics
            Graphic = Graphics.FromImage(Bitmap)
            Graphic.DrawImage(BarCodeImg, New Point(15, 0))
            Graphic.DrawString(ValueCodeBar, New Font(FontFamily.GenericMonospace, 10), Brushes.Black, 0, BarCodeImg.Height + 5)

            BarCodeImg = Bitmap
            Dim Estenzione As String = Nothing
            If FileName <> Nothing Then
                Select Case FileName.Substring(FileName.Length - 4, 4).ToString

                    Case ".jpg"
                        Exit Select
                    Case ".bmp"
                        Exit Select
                    Case ".png"
                        Exit Select
                    Case Else
                        Estenzione = ".jpg"
                End Select

                FileName = FileName & Estenzione
                BarCodeImg.Save(FileName)

            End If


            Return BarCodeImg
        Catch ex As Exception
            Return Nothing
        End Try

    End Function

Potete scaricare il file d'esempio :

Password :  "ImaginSystem" 


By ImaginSystems & Queen Gin   
Categoria: VB.NET
lunedì, 22 ott 2012 Ore. 16.40

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003