gpt4 book ai didi

android - 如何在 PdfPCell 中居中对齐模板元素

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:09:48 31 4
gpt4 key购买 nike

我正在构建一个垂直的月份列表,每个月都有一个水平的天数列表。我每天都添加一个大小和颜色的矩形;大小和颜色取决于来自数据库查询的值。

我正在使用 this answer 中提供的 PdfPTablePdfPCellcbCreateTemplate

除矩形的位置外,其他一切正常(矩形的大小、矩形的颜色):它始终位于 0,0,即使我(认为)我已经设置了 V 和 H定位。代码摘录如下;请指教。

int Severity = args.getPLR().get(i).getItems().get(j).getItems().get(itemIndex).getSeverity();
Severity = Severity + 5; //plump up, so that max (10) should fill the cell
PdfPCell cell = new PdfPCell();
cell.setPadding(0);
template = cb.createTemplate(Severity, Severity);
template.setLineWidth(0.5f);
template.rectangle(0, 0, Severity, Severity);
//we should switch the color
//based on the Severity
switch ((Severity-5)) {
case 0:
template.setColorFill(Color.GREEN);
break;
case 1:
template.setColorFill(Color.GREEN);
break;
case 2:
template.setColorFill(Color.YELLOW);
break;
case 3:
template.setColorFill(Color.YELLOW);
break;
case 4:
template.setColorFill(Color.YELLOW);
break;
case 5:
template.setColorFill(Color.ORANGE);
break;
case 6:
template.setColorFill(Color.ORANGE);
break;
case 7:
template.setColorFill(Color.ORANGE);
break;
case 8:
template.setColorFill(Color.RED);
break;
case 9:
template.setColorFill(Color.RED);
break;
case 10:
template.setColorFill(Color.RED);
break;
}
template.fill();
img = Image.getInstance(template);
chunk = new Chunk(img, 0f, 0f);
cell.addElement(chunk);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
painTable.addCell(cell);

这是显示的图形: should be aligned center/center

应该是Center/Center。我哪里出错了?

这是使用公认解决方案更新后的代码部分:

img = Image.getInstance(template);        
chunk = new Chunk(img, 0f, 0f);
Phrase severityChunk = new Phrase(chunk);
PdfPCell cell = new PdfPCell(severityChunk);
cell.setPadding(0);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
painTable.addCell(cell);

最佳答案

您正在混合使用文本模式复合模式

PdfPCell 级别上设置的对齐属性仅适用于文本模式。一旦切换到复合模式(使用 addElement() 方法),iText 就会忽略为单元格定义的对齐方式,转而采用为单元格内容定义的对齐方式。

在文本模式下,所有内容都具有相同的对齐方式。在复合模式下,您可以让不同的元素具有不同的对齐方式。

您有不同的选择:您可以将 Chunk 放在 Paragraph 中,并为段落而不是为单元格定义对齐方式。您可以使用包含 block 的 Phrase 在文本模式下创建单元格。甚至可以在不使用 Chunk 等的情况下创建带有 Image 的单元格......

这在我写的“iText in Action”一书中都有解释。

关于android - 如何在 PdfPCell 中居中对齐模板元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12573962/

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