4 Replies Latest reply on Apr 17, 2017 2:43 PM by beh_gras_beh

    EAN13 script

    beh_gras_beh Level 1

      Dear people,

       

      I saw an little javascript online to create an barcode with an font in an HTML file.

      I thought I can do this with an acrobat form and the ean13 fonts only I'm very new to acrobat and Javascript.

       

      This is what I have as validation script.

       

      var gug = ["LLLLLL","LLGLGG","LLGGLG","LLGGGL","LGLLGG","LGGLLG","LGGGLL","LGLGLG","LGLGGL","LGGLGL"]
      function updatebarcode(){
      var txt = this.getField("Text1").value
      // Left Block
      var first = txt[0]
      var enc = "_"+first+"*"
      var max = ((txt.length<7)? txt.length:7)
      for(var i = 1;i< max;i++){
      enc+=gug[first][i-1]+txt[i]
      }
      
      
      enc+="**"
      
      
      // Right Block
      var max = ((txt.length<12)? txt.length:12)
      for(var i = 7;i<  max;i++){
      enc+="R"+txt[i]
      }
      pr = 0
      
      
      //Checksum
      for(var i= Math.min(txt.length,12);i>=1;i--){
      pr+=parseInt(txt[i-1])*(i%2==1?1:3)
      }
      pr=(10-(pr%10))%10
      enc+="R"+pr
      enc+="*"
      
      
      
      
      this.getField("Text1").value=enc
      }
      

       

      An barcode like this:

      8711731130608

      Should become this:

      _8*L7G1L1G7G3L1**R1R3R0R6R0R8*

       

      Is this possible?

        • 1. Re: EAN13 script
          Karl Heinz Kremer Adobe Community Professional

          Can you please explain how you want to get from "8711731130608" to "_8*L7G1L1G7G3L1**R1R3R0R6R0R8*". If there are well defined rules, then you can certainly do that in JavaScript, but we need to be able to understand these rules first.

          • 2. Re: EAN13 script
            beh_gras_beh Level 1

            Well, thank you for the reply.

             

            It's an long read but it is an good explanation from the developer.

             

            Encoding is done with the following schema: (example 4054503008694; the last Digit 4 is the checksum)

             

             

            the first digit is just written with an underscore `_4` followed by the startcode `*`

            then you lookup the even/uneven pattern from this table, base on the first digit (4)

             

             

            | digit | pattern |

            | --- | --- |

            | 0 | LLLLLL |

            | 1 | LLGLGG |

            | 2 | LLGGLG |

            | 3 | LLGGGL |

            | 4 | LGLLGG |

            | 5 | LGGLLG |

            | 6 | LGGGLL |

            | 7 | LGLGLG |

            | 8 | LGLGGL |

            | 9 | LGGLGL |

             

             

            so in our case the pattern is `LGLLGG`

            next you write the digits 2 to 7 (`054503`) prefixed with the corresponding letter from the pattern

             

             

            `L0G5L4L5G0G3` followed by the mid-stop marker `**`.

             

             

            the remaining digits are just prefixed with `R`: `R0R0R8R6R9R4` and then the whole code ends with the stop code `*`

             

             

            so the encoded string now looks like this:

             

             

                _4*L0G5L4L5G0G3**R0R0R8R6R9R4*

             

             

            when you display this string with the ean13 font, it will produce a nicely scannable barcode.

             

            I bumped across this site: EAN 13 Barcode Genrator unsing an EAN13 Font

            And I thought that has to be an possibility to use this in an acrobat form but maybe I'm wrong.

             

            Greetings.

            • 3. Re: EAN13 script
              Karl Heinz Kremer Adobe Community Professional

              You are on the right track with your script, I only had to make a few adjustments to make it run correctly. Try this:

               

              var gug = ["LLLLLL", "LLGLGG", "LLGGLG", "LLGGGL", "LGLLGG", "LGGLLG", "LGGGLL", "LGLGLG", "LGLGGL", "LGGLGL"];
              
              function updatebarcode() {
                var txt = this.getField("BC_IN").valueAsString;
              
              
                // Left Block  
                var first = Number(txt[0]);
                var enc = "_" + first + "*";
                var max = ((txt.length < 7) ? txt.length : 7);
                for (var i = 1; i < max; i++) {
                enc += gug[first][i - 1] + txt[i];
                }
              
                enc += "**";
              
                // Right Block  
                max = ((txt.length < 13) ? txt.length : 13);
                for (var i = 7; i < max; i++) {
                enc += "R" + txt[i];
                }
              
                enc += "*";
              
                event.value = enc;
              }
              
              updatebarcode();
              

               

              The most severe problem you had was that you were reading and writing to the same text field. Unless you know exactly what that field contains (user input or calculated input), you cannot do that. In my version, the script is running as a custom calculation script for the field that should display the modified string (the one that will use your barcode font to show the barcode). It is reading from a field called "BC_IN". Also, you did not actually call your function, I added that to the script as well. And, I removed the portion of the code that calculated the checksum. Based on what you've described, the checksum is already part of the string received by this function.

              • 4. Re: EAN13 script
                beh_gras_beh Level 1

                Oh wow that worked perfectly!

                 

                Thanks!

                 

                This is giving me the right tools to experiment a little more.

                Good explanation and an good example, thanks!