-
-
Notifications
You must be signed in to change notification settings - Fork 156
API Reference
This reference covers the main classes and methods in Boxable.
The main class for creating tables.
BaseTable(float yStart,
float yStartNewPage,
float bottomMargin,
float width,
float margin,
PDDocument document,
PDPage page,
boolean drawLines,
boolean drawContent)Parameters:
-
yStart- Starting Y position on the current page -
yStartNewPage- Y position for new pages (usually same as yStart) -
bottomMargin- Bottom margin (when to create new page) -
width- Table width in points -
margin- Left margin in points -
document- PDDocument instance -
page- Current PDPage -
drawLines- Whether to draw cell borders -
drawContent- Whether to draw cell content
// Create a new row
Row<PDPage> createRow(float height)
// Add a header row (repeats on each page)
void addHeaderRow(Row<PDPage> row)
// Draw the table
float draw() // Returns final Y position
// Get current page (may change after draw)
PDPage getCurrentPage()
// Set outer border style
void setOuterBorderStyle(LineStyle style)Represents a table row.
// Create a text cell
Cell<PDPage> createCell(float widthPercent, String text)
// Create an image cell
ImageCell<PDPage> createImageCell(float widthPercent, Image image)
// Create a table cell (nested table via HTML)
Cell<PDPage> createTableCell(float widthPercent, String htmlTable,
PDDocument doc, PDPage page, float yStart,
float bottomMargin, float margin)
// Set row height
void setHeight(float height)
// Set fixed height (auto-shrink text)
void setFixedHeight(boolean fixed)
// Get all cells in row
List<Cell<PDPage>> getCells()Represents a table cell.
// Colors
void setFillColor(Color color)
void setTextColor(Color color)
// Text properties
void setFont(PDFont font)
void setFontSize(float size)
void setTextRotated(boolean rotated)
// Alignment
void setAlign(HorizontalAlignment align)
void setValign(VerticalAlignment align)
// Borders
void setBorderStyle(LineStyle style)
void setTopBorderStyle(LineStyle style)
void setBottomBorderStyle(LineStyle style)
void setLeftBorderStyle(LineStyle style)
void setRightBorderStyle(LineStyle style)
// Padding
void setTopPadding(float padding)
void setBottomPadding(float padding)
void setLeftPadding(float padding)
void setRightPadding(float padding)
// Content
String getText()
void setText(String text)Extends Cell for image content.
// Get the image
Image getImage()
// Inherited from Cell: all styling methodsHelper class for importing data.
DataTable(BaseTable table, PDPage page)
DataTable(BaseTable table, PDPage page, List<Float> colWidths)// Import from CSV
void addCsvToTable(String csvData, boolean hasHeader, char delimiter)
// Import from List
void addListToTable(List<List> data, boolean hasHeader)
// Style templates
Cell getHeaderCellTemplate()
Cell getDataCellTemplateEven()
Cell getDataCellTemplateOdd()
List<Cell> getDataCellTemplateEvenList()
List<Cell> getDataCellTemplateOddList()
Cell getFirstColumnCellTemplate()
Cell getLastColumnCellTemplate()Represents border styling.
LineStyle(Color color, float width)Example:
LineStyle redBorder = new LineStyle(Color.RED, 2f);
cell.setBorderStyle(redBorder);Wrapper for images in cells.
Image(BufferedImage image)Example:
BufferedImage buffImg = ImageIO.read(new File("image.png"));
Image image = new Image(buffImg);
ImageCell<PDPage> cell = row.createImageCell(50, image);HorizontalAlignment.LEFT
HorizontalAlignment.CENTER
HorizontalAlignment.RIGHTVerticalAlignment.TOP
VerticalAlignment.MIDDLE
VerticalAlignment.BOTTOMDataTable.HASHEADER // = true (data has header row)
DataTable.NOHEADER // = false (no header row)PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
float margin = 50;
float yStart = page.getMediaBox().getHeight() - margin;
float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
BaseTable table = new BaseTable(yStart, yStart, 70, tableWidth,
margin, doc, page, true, true);
Row<PDPage> row = table.createRow(20f);
Cell<PDPage> cell = row.createCell(100, "Hello World");
table.draw();
doc.save("output.pdf");
doc.close();Row<PDPage> header = table.createRow(20f);
Cell<PDPage> headerCell = header.createCell(100, "Header");
headerCell.setFillColor(new Color(52, 152, 219));
headerCell.setTextColor(Color.WHITE);
headerCell.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD));
headerCell.setAlign(HorizontalAlignment.CENTER);
table.addHeaderRow(header);for (int i = 0; i < data.length; i++) {
Row<PDPage> row = table.createRow(15f);
Cell<PDPage> cell = row.createCell(100, data[i]);
if (i % 2 == 0) {
cell.setFillColor(new Color(236, 240, 241));
} else {
cell.setFillColor(Color.WHITE);
}
}// Thick red border
cell.setBorderStyle(new LineStyle(Color.RED, 3f));
// No borders
cell.setBorderStyle(null);
// Bottom border only
cell.setTopBorderStyle(null);
cell.setBottomBorderStyle(new LineStyle(Color.BLACK, 2f));
cell.setLeftBorderStyle(null);
cell.setRightBorderStyle(null);
// Outer border only on table
table.setOuterBorderStyle(new LineStyle(Color.BLACK, 2f));// Alpha channel: 0 = fully transparent, 255 = fully opaque
Color transparentRed = new Color(255, 0, 0, 128); // 50% transparent
cell.setFillColor(transparentRed);String html = "<b>Bold</b> and <i>italic</i><br/>" +
"H<sub>2</sub>O and E=mc<sup>2</sup>";
Cell<PDPage> cell = row.createCell(100, html);Row<PDPage> row = table.createRow(20f);
Cell<PDPage> wideCell = row.createCell(70, "Wide cell");
Cell<PDPage> narrowCell1 = row.createCell(15, "Narrow 1");
Cell<PDPage> narrowCell2 = row.createCell(15, "Narrow 2");-
Always call
draw()before saving the document -
Store return value of
draw()if creating multiple tables -
Check
getCurrentPage()afterdraw()for page transitions - Use percentage widths that sum to 100
- Add header rows before data rows
-
Set
drawContentanddrawLinesaccording to your needs - Use templates in DataTable for consistent styling
- Close PDDocument after saving
Special thanks to :
=======
Copyright 2026
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.