public PdfPTable CreatePdfPTable() {
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];
PdfPTable table = new PdfPTable(columns);
table.TableEvent = this;
table.HorizontalAlignment = alignment;
int pos;
foreach (SimpleCell row in content) {
pos = 0;
foreach (SimpleCell cell in row.Content) {
if (float.IsNaN(cell.Spacing_left)) {
cell.Spacing_left = cellspacing / 2f;
}
if (float.IsNaN(cell.Spacing_right)) {
cell.Spacing_right = cellspacing / 2f;
}
if (float.IsNaN(cell.Spacing_top)) {
cell.Spacing_top = cellspacing / 2f;
}
if (float.IsNaN(cell.Spacing_bottom)) {
cell.Spacing_bottom = cellspacing / 2f;
}
cell.Padding = cellpadding;
table.AddCell(cell.CreatePdfPCell(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.TotalWidth = sumWidths;
table.SetWidths(widths);
}
else {
for (int i = 0; i < columns; i++) {
if (widthpercentages[i] == 0) {
sumWidths = 0;
break;
}
sumWidths += widthpercentages[i];
}
if (sumWidths > 0) {
table.SetWidths(widthpercentages);
}
}
if (width > 0) {
table.TotalWidth = width;
}
if (widthpercentage > 0) {
table.WidthPercentage = widthpercentage;
}
return table;
}