单元格:
1.单元格的创建
PdfPCell pdfPCell = new PdfPCell()
在创建单元格的时候,我们一般都会给单元格的构造函数传递参数,这个参数多数是一个含有文本的段落,同时会在这个段落上设置了行间距(在字数太多,单元格的宽度较小情况下会进行换行)例如:
Paragraph elements = new Paragraph(text);
elements.setLeading( fixedLeading,multipliedLeading ) // fixedLeading 固定间距值,multipliedLeading 多倍间距值
PdfPCell pdfPCell = new PdfPCell(elements);// 这个时候就不能直接把段落放构造方法中了,因为new PdfPCell(elements)底层使用的是this.column.setLeading(0.0F, 1.0F); 不会延用elements的setLeading,因此在单元格中添加段落建议使用:PdfPCell pdfPCell = new PdfPCell();
pdfPCell.addElement(elements );
2.单元格的常见方法
PdfPCell pdfPCell = new PdfPCell();
对齐方式:
// 水平垂直对齐
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
// 水平居中对齐
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
// 水平居中左对齐
pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
// 水平居中右对齐
pdfPCell.setHorizontalAlignment(Element.ALIGN_RIGHT);边框(单元格默认是有边框的):
// 无边框
pdfPCell.setBorder(NO_BORDER);设置单元格的高度:
//设置计算高度
pdfPCell.setCalculatedHeight();
// 设置固定高度
pdfPCell.setFixedHeight();
// 设置最小高度
pdfPCell.setMinimumHeight();合并列单元格:
// 表示该单元格跟右边列的两个单元格合并(一共三个单元格)
pdfPCell.setColspan(3);合并行单元格:
// 表示该单元格跟下面的一个单元格合并(一个两个单元格)
pdfPCell.setRowspan(2);
设置元素在单元格内的间距 :
// 左间距
pdfPCell.setPaddingLeft();
// 右间距
pdfPCell.setPaddingRight();
//上边距
pdfPCell.setPaddingTop();
//下边距
pdfPCell.setPaddingBottom();设置文字在单元格中换行时的行间距 :
pdfPCell.setLeading(fixedLeading,multipliedLeading);
最终行间距 :totalLeading= fixedLeading+fontSize(字体大小)* multipliedLeading;单元格的背景颜色:
pdfPCell.setBackgroundColor(new BaseColor(111,112,50)); // 数字代表的是RGB三个颜色的数值单元格添加元素:
pdfPCell.addElement();
表格:
1.表格的创建:
PdfPTabletable = new PdfPTable(3);
如果创建了N列,如果table.add添加的单元格数量不是N的倍数,那么就会报异常
2.表格的常见方法:
宽度百分比:
// 这个方法可以用来画出进度条
table.setWidthPercentage(100);
// 前间距
table.setSpacingBefore(10f);
// 后间距
table.setSpacingAfter(10f);
// 设置每列的宽度,因为是定义了三列的表格,所以这里的设置3个数据,代表了每列的宽度
float[] columnWidths = { 1f, 2f, 3f };
table.setWidths(columnWidths);//对齐方式
table .setHorizontalAlignment(Element.ALIGN_LEFT);// 左对齐//添加单元格
table.add(new PdfPCell())
图片:
1.获取该页四个点的坐标(最左上角,最左下角,最右上角,最右下角):
document.getPageSize().getTop();// y轴最上边的y坐标
document.getPageSize().getBottom(); // y轴最下边的y坐标
document.getPageSize().getLeft(); // x轴最左边的x坐标
document.getPageSize().getRight(); // x轴最右边的x坐标
document.getPageSize().getWidth(); // 获取页面宽度
document.getPageSize().getHeight(); // 获取页面高度
2.图片的获取以及添加到PDF上:
//获取照片
Imageimage = Image.getInstance("本地图片地址");
//也可以获取网上的图片,例如:
URLurl = newURL("http://static.cnblogs.com/images/adminlogo.gif")
Imageimage = Image.getInstance(url);
//设置图片位置的x轴和y轴
image.setAbsolutePosition(0, 0); // 相对于上面获取到的坐标,位于左下角//设置图片的宽度和高度
image.scaleAbsolute(100, 100);
// 添加图片
document.add(image);