Java IText:表格
我们可以使用IText中的com.itextpdf.text.PdfPTable类将表格添加到PDF文档中。表是IText中一些较复杂的对象,因此此文本比本教程中的其余文本大一些。以下是涵盖的主题列表:
- 建立表格
- 工作台宽度
- 表格前后的间距
- 列宽
- 列跨度
- 单元格文本模式和复合模式
- 文本模式下的默认单元格设置
- 单元对齐
- 单元压痕
- 单元格引导
- 单元填充
- 单元格边框和颜色
- 细胞旋转
- 表格和图片
- 嵌套表
建立表格
当实例化一个" PdfTable"时,我们必须告诉该表应该有多少列。我们将列数作为参数传递给PdfPTable构造函数。
要将单元格添加到表中,我们可以调用addCell()方法,传递PdfPCell实例或者其他IText对象(如" Paragraph"等)。但是请记住,行为的不同取决于我们添加的对象。有关更多信息,请参见"单元格文本模式"和"复合模式"部分。
这是一个简单的代码示例:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class TableExample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorld-Table.pdf"));
document.open();
PdfPTable table = new PdfPTable(3); // 3 columns.
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
document.add(table);
document.close();
} catch(Exception e){
}
}
}
工作台宽度
我们可以使用setWidthPercentage()方法来设置表格宽度。这会将表格的宽度设置为页面宽度的百分比。默认宽度为80%。这是一个代码示例:
table.setWidthPercentage(100);
表格前后的间距
我们可以像这样设置表格前后的间距:
table.setSpacingBefore(10f); table.setSpacingAfter(10f);
列宽
我们可以使用setWidths()方法设置列宽,如下所示:
float[] columnWidths = {2f, 1f, 1f};
table.setWidths(columnWidths);
float数组中的列宽是相对宽度。在上面的示例中,第一列的宽度是后面各列的宽度的两倍。我们可以在width数组中使用任何数字,它们将被解释为相对值。例如,如果我们需要对列大小进行更细粒度的控制,则可以写成100、50、50。
列跨度
如果我们需要一个单元格来跨越多列,则可以使用setColspan()方法来实现,如下所示:
cell.setColspan(2);
单元格文本模式和复合模式
可以以文本模式或者复合模式添加单元格。
在文本模式下,添加元素的设置("短语","段落"等)将被忽略。仅应用单元格的设置。
在复合模式下,将考虑添加元素的设置。因此,应遵守诸如行距(行距),边距等设置。
添加到PdfCell的构造函数中的内容被视为文本模式内容。通过PdfCell.addElement()方法添加的内容被认为是复合模式内容。这里有些例子:
PdfCell textModeCell = new PdfCell(new Paragraph("Text Mode"));
PdfCell compositeModeCell = new PdfCell();
compositeModeCell.addElement(new Paragraph("Composite Mode"));
table.addCell(new Paragraph("Text Mode"));
文本模式下的默认单元格设置
我们可以使用table.addCell()方法来设置添加的新单元格的默认单元格设置,如下所示:
PdfCell defaultCell = table.getDefaultCell();
defaultCell.setBorder(PdfCell.NO_BORDER);
//set more default settings.
//add cells with default settings:
table.addCell(new Paragraph("default text mode cell");
table.addCell(new Phrase("default text mode cell");
单元对齐
我们可以使用setHorizontalAlignment()和setVerticalAlignment()设置像元对齐方式,如下所示:
cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
在复合模式下,我们还可以通过在Paragraph对象上设置段落对齐方式来分别设置每个Paragraph对齐方式。
单元压痕
我们可以设置单元格内容的缩进。
setIndent()方法设置单元格中第一段的缩进。
方法setFollowingIndent()设置单元格中以下段落的缩进。
setRightIndent()方法设置单元格内容的右缩进。
单元格引导
我们可以设置单元格中元素的前导(行距)。
如果单元格处于复合模式,则只需在添加的元素上设置前导,例如在将"段落"的开头添加到单元格之前,先对其进行设置。
在文本模式下,我们可以设置用于整个单元格内容的前导值。为此,请使用setLeading()方法。此方法有两个参数。固定的行距和根据字体高度计算的行距。这是两个示例:
cell.setLeading(15f, 0f); cell.setLeading(0f, 1.5f);
第一个方法调用将前导设置为15点+ 0 x字体高度。
第二个方法调用将前导设置为0点+ 1.5 x字体高度。
单元填充
我们可以使用以下方法设置单元格的填充(单元格边缘和内容之间的距离):
cell.setPadding(5); cell.setPaddingLeft(8); cell.setPaddingRight(8); cell.setPaddingTop(8); cell.setPaddingBottom(8);
单元格边框和颜色
我们可以使用以下方法设置单元格边框和颜色:
cell.setBackgroundColor(BaseColor.YELLOW); //sets BG color to yellow. cell.setBorder(Rectangle.NO_BORDER); // removes border cell.setBorderWidth (3f); // sets border width to 3 units cell.setBorderWidthLeft (1f); cell.setBorderWidthRight (1f); cell.setBorderWidthTop (1f); cell.setBorderWidthBottom(1f); cell.setBorderColor (BaseColor.BLUE); // sets blue border cell.setBorderColorLeft (BaseColor.GREEN); cell.setBorderColorRight (BaseColor.GREEN); cell.setBorderColorTop (BaseColor.GREEN); cell.setBorderColorBottom(BaseColor.GREEN);
为了避免单元格边界和内容重叠,如果单元格边界较粗,请调用setUserBorderPadding(true),如下所示:
cell.setUserBorderPadding(true);
细胞旋转
我们可以使用setRotation()方法设置单元格内容的旋转度,如下所示:
cell.setRotation(90);
表格和图片
我们可以将图像添加到表格单元格中,或者使该单元格适合图像的大小,或者使图像适合单元格的大小。我们可以通过将图像传递到PdfPCell构造函数,以及一个布尔值来做到这一点,该布尔值说明图像是适合单元格(true),还是单元格适合图像(false)。方法如下:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.net.URL;
public class Table3Example {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("Table3.pdf"));
document.open();
PdfPTable table = new PdfPTable(2); // 3 columns.
Image image = Image.getInstance("Hyman-Hyman.jpg");
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(image, false);
table.addCell(cell1);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(image, true);
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
table.addCell(cell3);
table.addCell(cell4);
document.add(table);
document.close();
} catch(Exception e){
}
}
}
嵌套表
我们可以在" PdfCell"内部添加" PdfTable"作为内容,从而将表嵌套在表中。这是一个例子:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class Table2Example {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("Table2.pdf"));
document.open();
PdfPTable table = new PdfPTable(3); // 3 columns.
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPTable nestedTable = new PdfPTable(2);
nestedTable.addCell(new Paragraph("Nested Cell 1"));
nestedTable.addCell(new Paragraph("Nested Cell 2"));
cell3.addElement(nestedTable);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
document.add(table);
document.close();
} catch(Exception e){
e.printStackTrace();
}
}
}

