Example #1
0
  /**
   * Adds an item to the page for printing. If the document was not successfully opened, or if the
   * item has an invalid barcode, no action will be taken and a negative Result will be returned.
   *
   * @param item
   */
  public Result addItem(Item item) {
    if (!_open) {
      return new Result(false, "Cannot add a product to an unopened file.");
    }
    if (!item.getBarcode().isValid()) {
      return new Result(false, "Cannot print an invalid barcode.");
    }

    // Generate the barcode image:
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCodeType(com.itextpdf.text.pdf.Barcode.UPCA);
    codeEAN.setCode(item.getBarcode().toString());
    Image bcimage = codeEAN.createImageWithBarcode(_rawContent, null, null);

    // Add some text to a chunk, and the image to a chunk.
    Font font = new Font(Font.FontFamily.HELVETICA, 8);
    Chunk c1 =
        new Chunk(
            item.getProductDescription()
                + "\n"
                + item.getShortEntryDateString()
                + " -> "
                + item.getShortExpirationDateString()
                + "\n\n");
    c1.setFont(font);
    Phrase p = new Phrase(c1);
    Chunk c2 = new Chunk(bcimage, 0, 0);
    p.add(c2);

    PdfPCell cell = new PdfPCell(p);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setPadding(10);
    cell.setBorder(0);
    _table.addCell(cell);

    _itemsAdded++;

    return new Result(true, "Barcode added successfully.");
  }