|
How do I reduce the height of a nested table? I have
an outer table that contains rows of PdfPtables, and the height of each row is
more than if I simply added a PdfPCell. Nested tables were added so
that the page breaks occur on a row. A sample test program below draws 2 tables, one with PdfPCells
as rows and one with nested tables as rows. How can I get the height of
both tables to be the same? Ralph Bohnet ---------------------------- Sample test program
-------------------------- import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPCellEvent; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class MyPdfPTableTest { static final Font normal9pt = new
Font(Font.TIMES_ROMAN, 9.5f, Font.NORMAL); public static void main(String[] args)
throws Exception{ int [] int[] {1}; String[] text =
new String[] {
"This is the first line",
"This is the second line",
"This is the third line",
"This is the fourth line",
"This is the Fifth line",
"This is the Sixth line",
"This is the seventh line" }; Document doc =
new Document(); PdfWriter writer
= PdfWriter.getInstance(doc, new
FileOutputStream("nested_tables.pdf")); doc.open(); doc.add(new
Paragraph("Table with cells only:")); PdfPTable >
addContents(oneTable, text); doc.add(oneTable); doc.add(new
Paragraph("Table with nested tables as rows:")); PdfPTable
nestedTables = createTransparentPdfPTable(ONE_COLUMN);
addContentsAsNestedTables(nestedTables, text);
doc.add(nestedTables); doc.close(); } private static void addContents(PdfPTable
aTable, String[] details ) throws DocumentException { for (int i =0; i
< details.length; i++) {
addCell(aTable, details[i], 1, normal9pt, Element.ALIGN_LEFT,
Element.ALIGN_MIDDLE, null); } } private static void
addContentsAsNestedTables(PdfPTable outerTable, String[] details ) throws
DocumentException { for (int i =0; i
< details.length; i++) {
PdfPTable nestedTable = createTransparentPdfPTable(new int[] {1});
addCell(nestedTable, details[i], 1, normal9pt, Element.ALIGN_LEFT,
Element.ALIGN_MIDDLE, null);
outerTable.addCell(nestedTable); } } protected static PdfPTable
createTransparentPdfPTable(int[] columnProportions) throws DocumentException { PdfPTable
newTable = new PdfPTable(columnProportions.length);
newTable.setWidths(columnProportions);
newTable.setWidthPercentage(100f); PdfPCell
defaultCell = newTable.getDefaultCell();
defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
defaultCell.setBorderWidth(0f); return newTable; } protected static PdfPCell
addCell(PdfPTable table, String content, int colspan, Font font, int
horizontalAlignment,
int verticalAlignment,PdfPCellEvent specialAction) throws
DocumentException { PdfPCell cell =
new PdfPCell(new Paragraph(content, font)); cell.setColspan(colspan);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(verticalAlignment);
cell.setBorderWidth(0f);
cell.setPadding(0f); if (specialAction
!= null) {
cell.setCellEvent(specialAction); }
table.addCell(cell); return cell; } } |
