这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.stream.Stream;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
Expand All @@ -23,25 +24,23 @@ public static void main(String[] args) {

try {

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("iTextTable.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("iTextTable.pdf"));

document.open();
document.open();

PdfPTable table = new PdfPTable(3);
addTableHeader(table);
setAbsoluteColumnWidths(table);
//setAbsoluteColumnWidthsInTableWidth(table);
//setRelativeColumnWidths(table);
addRows(table);
addCustomRows(table);
PdfPTable table = new PdfPTable(3);
addTableHeader(table);

addRows(table);
addCustomRows(table);

document.add(table);
document.close();
document.add(table);
document.close();

} catch (Exception e) {
e.printStackTrace();
}
e.printStackTrace();
}
}

private static void addTableHeader(PdfPTable table) {
Expand All @@ -54,24 +53,6 @@ private static void addTableHeader(PdfPTable table) {
table.addCell(header);
});
}

private static void setAbsoluteColumnWidths(PdfPTable table) {
table.setTotalWidth(500); // Sets total table width to 500 points
table.setLockedWidth(true);
float[] columnWidths = {100f, 200f, 200f}; // Defines three columns with absolute widths
table.setWidths(columnWidths);
}

private static void setAbsoluteColumnWidthsInTableWidth(PdfPTable table) {
table.setTotalWidth(new float[] {72f, 144f, 216f}); // First column 1 inch, second 2 inches, third 3 inches
table.setLockedWidth(true);
}

private static void setRelativeColumnWidths(PdfPTable table) {
// Set column widths (relative)
table.setWidths(new float[] {1, 2, 1});
table.setWidthPercentage(80); // Table width as 80% of page width
}

private static void addRows(PdfPTable table) {
table.addCell("row 1, col 1");
Expand Down