android - 创建一个矩形,在其中添加段落并使用 iText 根据文本调整矩形的高度
问题描述
我能够创建一个矩形并在其中添加段落和图像。矩形的宽度也很好,但我只想根据段落中的文本设置矩形的高度。此外,我想以特定方式在矩形内添加数据,这就是我在其中创建表格的原因。那么如何让表格填满整个矩形。有人可以帮我吗?
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 文档。然后,您可以使用来自该模拟运行的信息来调整第二阶段的实际渲染。
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);
}
对原代码的进一步修改:
- 去掉了表格单元格的固定高度,可以更清楚地展示矩形的增长和收缩:removed
table.getDefaultCell().setFixedHeight(112f)
- 移除表格边框并将矩形颜色更改为红色以更清晰地显示矩形。
推荐阅读
- git - 多个 Visual Studio 存储库的相同标记
- iis - 负载平衡时如何检测 IIS 故障
- python - 在生产服务器上工作的端口值应该是多少?
- javascript - innerHTML 没有设置 SVG 元素的值
- multithreading - 每个请求的 Asp.Net Core 异步延迟初始化
- javascript - React js-显示状态值取决于 redux props 值
- haskell - 用 ghci 调查 (->) 并试图找到它的根源
- java - 在 Java 中从 XML 中检索数据并在 Oracle 中插入数据
- c# - 自动完成 lambda 表达式(当它使用类似函数参数时)(C#,Visual Studio 2019)
- google-chrome-extension - 如何在 chrome 扩展中将 mHTML 转换为 pdf?