我可以创建一个矩形,并在里面添加段落和图片。矩形的宽度也很好,但我只是想根据段落中的文字来设置矩形的高度。另外,我还想以一种特殊的方式在矩形内添加数据,这就是为什么我在里面创建了表格。那么如何使表格填满整个矩形。谁能帮我解决这个问题?
PdfContentByte cb = writer.getDirectContent();
Rectangle rect = new Rectangle(kBorderInset, document.getPageSize().getHeight()-kPageDisclaimerY,
document.getPageSize().getWidth()-2 * kBorderInset,700f);
cb.rectangle(rect);
cb.stroke();
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(1);
rect.setBorderColor(BaseColor.BLACK);
cb.rectangle(rect);
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(rect);
ct.addElement(createTable1(auditBundle, context));
ct.go();
创建表格代码
private static PdfPTable createTable1(AuditBundle auditBundle, Context context) throws
DocumentException {
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.getDefaultCell().setUseAscender(true);
table.getDefaultCell().setUseDescender(true);
table.getDefaultCell().setFixedHeight(112f);
table.setWidths(new int[]{1, 2, 1});
float fntSize, lineSpacing;
fntSize = 20f;
lineSpacing = 12f;
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase(lineSpacing,auditBundle.getAudit().auditName,
FontFactory.getFont(FontFactory.HELVETICA, fntSize)));
paragraph.setAlignment(Element.ALIGN_LEFT | Element.ALIGN_CENTER);
paragraph.setPaddingTop(30);
PdfPCell cell = new PdfPCell();
cell.addElement(paragraph);
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell("");
table.addCell(cell);
Drawable d = context.getDrawable(R.drawable.ic_action_device_access_camera); // the drawable (Captain Obvious, to the rescue!!!)
assert d != null;
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
PdfPCell cellImg = new PdfPCell();
try {
Image image = Image.getInstance(bitmapdata);
image.setAlignment(Element.ALIGN_CENTER);
cellImg.setVerticalAlignment(Element.ALIGN_MIDDLE);
cellImg.addElement(image);
cellImg.setBackgroundColor(BaseColor.WHITE);
} catch (IOException e) {
e.printStackTrace();
}
table.addCell(cellImg);
return table;
}
如果你的内容是在一个表格中,你想在表格周围画一个矩形,最直接的方法就是使用表格边框。 这样就不需要再画一个额外的矩形了。
如果需要单独绘制矩形,你可以简单地添加内容,并得到结果的垂直位置(writer.getVerticalPosition(false)
). 然后根据添加内容前后的垂直位置画出矩形。
不过,确定渲染一段内容所需的面积这个通用问题还是很有意思的。 所以我就以问题中定义的表格为背景来回答,虽然这可能不是最有用的方法。 它可以应用于其他类型的内容和用例。
首先,在PDF中,坐标从左到右增加,从 从下到上. 该 Rectangle
你正在使用的构造函数是 Rectangle(final float lowerleftx, final float lowerlefty, final float upperrightx, final float upperrighty)
. 所以... lowerlefty
应小于 upperrighty
.你也可以忽略这一点,定义好的 Rectangle
"颠三倒四 Rectangle.normalize()
.
要确定渲染内容所需的垂直空间,你可以运行 ColumnText
在模拟模式下,它将完成所有的渲染逻辑,而不需要实际将内容写入PDF文档。 它将完成所有的渲染逻辑,而不需要实际将内容写入PDF文档。 然后你可以使用模拟运行的信息来调整第二阶段的实际渲染。
PdfContentByte cb = writer.getDirectContent();
// the initial rectangle defines the max size of the content
Rectangle rect = new Rectangle(10, document.getPageSize().getBottom() + 50,
document.getPageSize().getWidth() - 2 * 10, document.getPageSize().getTop() - 50);
// flip the rectangle if top and bottom were switched
rect.normalize();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(rect);
ct.addElement(createTable1(auditBundle, context));
// do a simulation run
int result = ct.go(true);
// assume the content fits in the initial rectangle
if (result == ColumnText.NO_MORE_TEXT) {
// the bottom of the simulated content
float verticalpos = ct.getYLine();
// redefine the rectangle based on the simulation
rect = new Rectangle(10, verticalpos, document.getPageSize().getWidth() - 2 * 10,
document.getPageSize().getTop() - 50);
ct.setSimpleColumn(rect);
// the original content was consumed in the simulation, so add it again
ct.addElement(createTable1(auditBundle, context));
// render again
ct.go(false);
// draw the rectangle
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(1);
rect.setBorderColor(BaseColor.RED);
cb.rectangle(rect);
}
对原始代码的进一步修改。
table.getDefaultCell().setFixedHeight(112f)