iText 5: create PdfPcell containing 2 background c

2019-02-20 13:34发布

问题:

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.

回答1:

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.



标签: java itext