gpt4 book ai didi

android - 创建一个矩形,在其中添加段落并使用 iText 根据文本调整矩形的高度

转载 作者:行者123 更新时间:2023-12-03 14:38:04 25 4
gpt4 key购买 nike

我能够创建一个矩形并在其中添加段落和图像。矩形的宽度也很好,但我只想根据段落中的文本设置矩形的高度。此外,我想以特定方式在矩形内添加数据,这就是我在其中创建表格的原因。那么如何让表格填满整个矩形。有人可以帮我吗?

        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;
}

目前它看起来像这样:
enter image description here

最佳答案

如果您的内容在表格中,并且您想在其周围绘制一个矩形,最直接的方法是使用表格边框。这消除了绘制额外矩形的需要。

如果需要单独绘制矩形,您可以简单地添加内容并获取结果垂直位置 (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);

}

对原代码的进一步修改:
  • 去掉了表格单元格的固定高度,可以更清楚地展示矩形的增长和收缩:去掉table.getDefaultCell().setFixedHeight(112f)
  • 移除表格边框并将矩形颜色更改为红色以更清晰地显示矩形。

  • 带有短文本的结果:
    Result with short text

    长文本的结果:
    Result with long text

    关于android - 创建一个矩形,在其中添加段落并使用 iText 根据文本调整矩形的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61674422/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com