iText 5:创建包含2种背景颜色且文本重叠的PdfPcell [英] iText 5: create PdfPcell containing 2 background colors with text overlap

查看:238
本文介绍了iText 5:创建包含2种背景颜色且文本重叠的PdfPcell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中使用iText5并想要对PdfPcell的结果进行颜色编码,如下所示。例如,该表包括2列和3行。





任何人都知道如何实现这一目标?



我可以简单地设置背景颜色

  PdfPCell cell =新的PdfPCell(新短语(60 Pass,40 Fail,myStyle)); 
cell.setBackgroundColor(new BaseColor(0xff,0x0,0x0)); //红色背景

然后,做什么,在单元格中添加绿色矩形?使用模板?不确定。

解决方案

您可以通过单元格事件监听器来完成这些有趣的单元格功能。



例如对于您的任务,您可以实现 PdfPCellEvent ,如下所示:

  public class PercentileCellBackground实现PdfPCellEvent {
public PercentileCellBackground(float percent,BaseColor leftColor,BaseColor rightColor){
this.percent = percent;
this.leftColor = leftColor;
this.rightColor = rightColor;
}

@Override
public void cellLayout(PdfPCell cell,Rectangle position,PdfContentByte [] canvases){
PdfContentByte canvas = canvases [PdfPTable.BACKGROUNDCANVAS];

float xTransition = position.getLeft()+(position.getRight() - position.getLeft())*(percent / 100.0f);
float yTransition =(position.getTop()+ position.getBottom())/ 2f;
float radius =(position.getRight() - position.getLeft())* 0.025f;
PdfShading axial = PdfShading.simpleAxial(canvas.getPdfWriter(),
xTransition - radius,yTransition,xTransition + radius,yTransition,leftColor,rightColor);
PdfShadingPattern着色= new PdfShadingPattern(轴向);

canvas.saveState();
canvas.setShadingFill(着色);
canvas.rectangle(position.getLeft(),position.getBottom(),position.getWidth(),position.getHeight());
canvas.fill();
canvas.restoreState();
}

最终浮动百分比;
final BaseColor leftColor;
final BaseColor rightColor;
}



正如您所看到的,我使用阴影来使绿色/红色过渡不那么刺耳。如果你想要它更苛刻,在事件 cellLayout 方法中减小半径系数 0.025f 或在其中使用矩形绘图说明相反。


Using iText5 in java and want to color-code a PdfPcell's results as shown below. The table includes 2 columns and 3 rows, for example.

Anyone have an idea how to accomplish this?

Could I simply set the background color using

PdfPCell cell = new PdfPCell(new Phrase("60 Pass, 40 Fail", myStyle));
cell.setBackgroundColor(new BaseColor(0xff,0x0,0x0));  // red background

then, do what, add a green rectangle to the cell? Use a template? Not sure.

解决方案

You can accomplish such "interesting" cell features by means of cell event listeners.

E.g. for your task you can implement a PdfPCellEvent like this:

public class PercentileCellBackground implements PdfPCellEvent {
    public PercentileCellBackground(float percent, BaseColor leftColor, BaseColor rightColor) {
        this.percent = percent;
        this.leftColor = leftColor;
        this.rightColor = rightColor;
    }

    @Override
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];

        float xTransition = position.getLeft() + (position.getRight() - position.getLeft()) * (percent/100.0f);
        float yTransition = (position.getTop() + position.getBottom()) / 2f;
        float radius = (position.getRight() - position.getLeft()) * 0.025f;
        PdfShading axial = PdfShading.simpleAxial(canvas.getPdfWriter(),
                xTransition - radius, yTransition, xTransition + radius, yTransition, leftColor, rightColor);
        PdfShadingPattern shading = new PdfShadingPattern(axial);

        canvas.saveState();
        canvas.setShadingFill(shading);
        canvas.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
        canvas.fill();
        canvas.restoreState();
    }

    final float percent;
    final BaseColor leftColor;
    final BaseColor rightColor;
}

(PercentileCellBackground)

You can then use this event listener class like this:

Document document = new Document();
try (OutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "TableWithCellsWithPercentileBackground.pdf"))) {
    PdfWriter.getInstance(document, os);
    document.open();

    Font font = new Font(FontFamily.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, BaseColor.WHITE);
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(40);
    PdfPCell cell = new PdfPCell(new Phrase("Group A"));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase(new Chunk("60 Pass, 40 Fail", font)));
    cell.setCellEvent(new PercentileCellBackground(60, BaseColor.GREEN, BaseColor.RED));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Group B"));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase(new Chunk("70 Pass, 30 Fail", font)));
    cell.setCellEvent(new PercentileCellBackground(70, BaseColor.GREEN, BaseColor.RED));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Group C"));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase(new Chunk("50 Pass, 50 Fail", font)));
    cell.setCellEvent(new PercentileCellBackground(50, BaseColor.GREEN, BaseColor.RED));
    table.addCell(cell);
    document.add(table);

    document.close();
}

(CellsWithPercentileBackground test testCreateTableWithCellsWithPercentileBackground)

The result looks like this:

As you can see, I used a shading to make the green/red transition less harsh. If you want it more harsh, reduce the radius factor 0.025f in the event cellLayout method or use rectangle drawing instructions therein instead.

这篇关于iText 5:创建包含2种背景颜色且文本重叠的PdfPcell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆