public static void main(String[] args) {
    // The path to the resource directory.
    String dataDir = Utils.getDataDir(SpecifySymbology.class) + "Barcode/BasicFeatures/";

    // Generate and save the image to file
    BarCodeBuilder builder = new BarCodeBuilder();
    // Set code text
    builder.setCodeText("test-123");
    // Set Symbology type
    builder.setSymbologyType(Symbology.Code39Standard);

    builder.save(dataDir + "Code39Standard.jpg");
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    BarCodeBuilder b = new BarCodeBuilder();
    b.setSymbologyType(Symbology.Code128);
    b.setCodeText("12345678");
    BufferedImage image = b.getBarCodeImage();

    response.setContentType("image/png");
    OutputStream outputStream = response.getOutputStream();
    ImageIO.write(image, "png", outputStream);
    outputStream.close();
  }
  public static void main(String[] args) {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(BarcodeImageBorders.class);

    String dst = dataDir + "barcode-image-borders.jpg";

    // Instantiate barcode object
    BarCodeBuilder bb = new BarCodeBuilder();

    // Set the Code text for the barcode
    bb.setCodeText("1234567");

    // Set the symbology type to code128
    bb.setSymbologyType(Symbology.Code128);

    // Set border style to solid
    bb.setBorderDashStyle(BorderDashStyle.Solid);

    // Set border margins by assigning an instance of MarginsF
    bb.setMargins(new MarginsF(2f, 2f, 2f, 2f));

    // Set border width
    bb.setBorderWidth(0.5f);

    // Enable border to be shown in the barcode
    bb.setBorderVisible(true);

    // Save the image
    bb.save(dst);

    System.out.println("Barcode saved successfully.");
  }
  /**
   * Called for every merge field encountered in the document. We can either return some data to the
   * mail merge engine or do something else with the document. In this case we choose to modify cell
   * formatting.
   */
  private static void fillProperties(Document doc, BarCodeBuilder barCode) throws Exception {
    String[] fieldNames =
        new String[] {
          "SymbologyType", "CodeText", "SupplementData", "RotationAngle", "ImageQuality"
        };
    Object[] fieldValues =
        new Object[] {
          getNameOfEnumValue(Symbology.class, barCode.getSymbologyType()),
          barCode.getCodeText(),
          barCode.getSupplementData(),
          barCode.getRotationAngleF(),
          getNameOfEnumValue(ImageQualityMode.class, barCode.getImageQuality())
        };

    // Execute mail merge
    doc.getMailMerge().execute(fieldNames, fieldValues);
  }
  /** Inserts the barcode into the document at the specified bookmark. */
  private static void insertBarcodeImage(Document doc, BarCodeBuilder barCode) throws Exception {
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Move to the appropriate table cell
    builder.moveToBookmark("BarcodeImage");
    // Insert the barcode image
    BufferedImage image = (BufferedImage) barCode.generateBarCodeImage();
    builder.insertImage(image);
  }
 /** Returns a random code text string. */
 private static String getRandomCodeText(BarCodeBuilder barCode) {
   // Some barcode standard specifications have certain restrictions of the
   // number of digits in the code
   switch ((int) barCode.getSymbologyType()) {
     case (int) Symbology.EAN13:
       return getRandomDigits(12);
     case (int) Symbology.EAN8:
       return getRandomDigits(7);
     case (int) Symbology.UPCA:
     case (int) Symbology.UPCE:
       return getRandomDigits(10);
     default:
       return getRandomDigits(12);
   }
 }
  /** Generates a barcode with random settings. */
  private static BarCodeBuilder createBarCode() {
    // Instantiate a LinearBarCode object
    BarCodeBuilder barCode = new BarCodeBuilder();

    // Call necessary methods each of which returns a random value for the
    // corresponding barcode property
    barCode.setSymbologyType(getRandomSymbology());
    barCode.setCodeText(getRandomCodeText(barCode));
    barCode.setSupplementData(getRandomSupplementData());
    barCode.setImageQuality((int) getRandomImageQualityMode());
    barCode.setRotationAngleF(getRandomRotationAngle());

    return barCode;
  }