// methods to set the membervariables /** * Adds an element to this Cell. * <P> * Remark: you can't add ListItems, Rows, Cells, * JPEGs, GIFs or PNGs to a Cell. * * @param element The Element to add * @throws BadElementException if the method was called with a ListItem, Row or Cell */ /// <summary> /// Adds an element to this Cell. /// </summary> /// <remarks> /// You can't add ListItems, Rows, Cells, /// JPEGs, GIFs or PNGs to a Cell. /// </remarks> /// <param name="element">the Element to add</param> public void AddElement(IElement element) { if (IsTable()) { Table table = (Table) arrayList[0]; Cell tmp = new Cell(element); tmp.Border = NO_BORDER; tmp.Colspan = table.Columns; table.AddCell(tmp); return; } switch (element.Type) { case Element.LISTITEM: case Element.ROW: case Element.CELL: throw new BadElementException("You can't add listitems, rows or cells to a cell."); case Element.JPEG: case Element.IMGRAW: case Element.IMGTEMPLATE: arrayList.Add(element); break; case Element.LIST: if (float.IsNaN(this.Leading)) { leading = ((List) element).TotalLeading; } if (((List) element).IsEmpty()) return; arrayList.Add(element); return; case Element.ANCHOR: case Element.PARAGRAPH: case Element.PHRASE: if (float.IsNaN(leading)) { leading = ((Phrase) element).Leading; } if (((Phrase) element).IsEmpty()) return; arrayList.Add(element); return; case Element.CHUNK: if (((Chunk) element).IsEmpty()) return; arrayList.Add(element); return; case Element.TABLE: Table table = new Table(3); float[] widths = new float[3]; widths[1] = ((Table)element).Width; switch (((Table)element).Alignment) { case Element.ALIGN_LEFT: widths[0] = 0f; widths[2] = 100f - widths[1]; break; case Element.ALIGN_CENTER: widths[0] = (100f - widths[1]) / 2f; widths[2] = widths[0]; break; case Element.ALIGN_RIGHT: widths[0] = 100f - widths[1]; widths[2] = 0f; break; } table.Widths = widths; Cell tmp; if (arrayList.Count == 0) { table.AddCell(Cell.DummyCell); } else { tmp = new Cell(); tmp.Border = NO_BORDER; tmp.Colspan = 3; foreach (IElement ele in arrayList) { tmp.Add(ele); } table.AddCell(tmp); } tmp = new Cell(); tmp.Border = NO_BORDER; table.AddCell(tmp); table.InsertTable((Table)element); tmp = new Cell(); tmp.Border = NO_BORDER; table.AddCell(tmp); table.AddCell(Cell.DummyCell); Clear(); arrayList.Add(table); return; default: arrayList.Add(element); break; } }
/** * Creates a Table object based on this TableAttributes object. * @return a com.lowagie.text.Table object * @throws BadElementException */ public Table CreateTable() { if (content.Count == 0) throw new BadElementException("Trying to create a table without rows."); SimpleCell rowx = (SimpleCell)content[0]; int columns = 0; foreach (SimpleCell cell in rowx.Content) { columns += cell.Colspan; } float[] widths = new float[columns]; float[] widthpercentages = new float[columns]; Table table = new Table(columns); table.Alignment = alignment; table.Spacing = cellspacing; table.Padding = cellpadding; table.CloneNonPositionParameters(this); int pos; foreach (SimpleCell row in content) { pos = 0; foreach (SimpleCell cell in row.Content) { table.AddCell(cell.CreateCell(row)); if (cell.Colspan == 1) { if (cell.Width > 0) widths[pos] = cell.Width; if (cell.Widthpercentage > 0) widthpercentages[pos] = cell.Widthpercentage; } pos += cell.Colspan; } } float sumWidths = 0f; for (int i = 0; i < columns; i++) { if (widths[i] == 0) { sumWidths = 0; break; } sumWidths += widths[i]; } if (sumWidths > 0) { table.Width = sumWidths; table.Locked = true; table.Widths = widths; } else { for (int i = 0; i < columns; i++) { if (widthpercentages[i] == 0) { sumWidths = 0; break; } sumWidths += widthpercentages[i]; } if (sumWidths > 0) { table.Widths = widthpercentages; } } if (width > 0) { table.Width = width; table.Locked = true; } else if (widthpercentage > 0) { table.Width = widthpercentage; } return table; }