/** * Adds a <CODE>Chunk</CODE>, an <CODE>Anchor</CODE> or another <CODE>Phrase</CODE> to this <CODE> * Phrase</CODE>. * * @param index index at which the specified element is to be inserted * @param o an object of type <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE> * @throws ClassCastException when you try to add something that isn't a <CODE>Chunk</CODE>, * <CODE>Anchor</CODE> or <CODE>Phrase</CODE> */ public void add(int index, Object o) { if (o == null) return; try { Element element = (Element) o; if (element.type() == Element.CHUNK) { Chunk chunk = (Chunk) element; if (!font.isStandardFont()) { chunk.setFont(font.difference(chunk.getFont())); } if (hyphenation != null) { chunk.setHyphenation(hyphenation); } super.add(index, chunk); } else if (element.type() == Element.PHRASE || element.type() == Element.ANCHOR || element.type() == Element.ANNOTATION || element.type() == Element.TABLE || // line added by David Freels element.type() == Element.YMARK || element.type() == Element.MARKED) { super.add(index, element); } else { throw new ClassCastException(String.valueOf(element.type())); } } catch (ClassCastException cce) { throw new ClassCastException("Insertion of illegal Element: " + cce.getMessage()); } }
/** * 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."); }