带有文本 itextsharp 的单元格背景图像

cell background image with text itextsharp

在此先感谢,我知道你很忙..所以我根据你给我的代码编辑了这个...... 首先我想让你看看当我尝试你的代码时我得到了什么..

我用作背景的图片是:

如你所见,我遇到了几个问题:

1- 图像不是单元格的背景,我希望它被拉伸。

2- 我尝试将文本放在不同的位置,但我失败了。

3 - 我还丢失了一个未显示的单元格。

我使用的代码是:

1- ImageEvent class :

 class ImageEvent : IPdfPCellEvent
{
protected Image img;
public ImageEvent(Image img) {
    this.img = img;
}
 void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
    img.ScaleToFit(position.Width, position.Height);

    img.SetAbsolutePosition(position.Left + (position.Width - img.Width) / 2,
            position.Bottom + (position.Height - img.ScaledHeight / 2));
    PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
    try {
        canvas.AddImage(img);
    } catch (DocumentException ex) {
        // do nothing
    }
}

}

2- 位置 class :

 class PositionEvent : IPdfPCellEvent
{
    protected Phrase content;
    protected string pos;

    public PositionEvent(Phrase content, string pos)
    {
        this.content = content;
        this.pos = pos;
    }

     void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
    {
        PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
        float x = 0;
        float y = 0;
        int alignment = 0;
        switch (pos)
        {
            case "TOP_LEFT":
                x = position.GetLeft(3);
                y = position.GetTop(content.Leading);
                alignment = Element.ALIGN_LEFT;
                break;
            case "TOP_RIGHT":
                x = position.GetRight(3);
                y = position.GetTop(content.Leading);
                alignment = Element.ALIGN_RIGHT;
                break;
            case "BOTTOM_LEFT":
                x = position.GetLeft(3);
                y = position.GetBottom(3);
                alignment = Element.ALIGN_LEFT;
                break;
            case "BOTTOM_RIGHT":
                x = position.GetRight(3);
                y = position.GetBottom(3);
                alignment = Element.ALIGN_RIGHT;
                break;
            case "CENTER_TOP":
                x = position.GetRight(3) + position.GetLeft(3) / 2; 
                y = position.GetTop(3);
                alignment = Element.ALIGN_RIGHT;
                break;
            case "CENTER_BOTTOM":
                x = position.GetRight(3) + position.GetLeft(3) / 2;
                y = position.GetBottom(3);
                alignment = Element.ALIGN_RIGHT;
                break;
            case "CENTER_MIDDLE":
                x = position.GetRight(3) + position.GetLeft(3) / 2;
                y = x;
                alignment = Element.ALIGN_RIGHT;
                break;
        }
        ColumnText.ShowTextAligned(canvas, alignment, content, x, y, 0);
    }
}

3- 方法:

  public void createPdf(string dest)
    {
        // 1. Create a Document which contains a table:
        Document document = new Document();
        PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));

        document.Open();
        PdfPTable table = new PdfPTable(3);
        table.WidthPercentage = 100f;
        PdfPCell cell1 = new PdfPCell();
        PdfPCell cell2 = new PdfPCell();
        PdfPCell cell3 = new PdfPCell();
        PdfPCell cell4 = new PdfPCell();
        PdfPCell cell5 = new PdfPCell();
        PdfPCell cell6 = new PdfPCell();
        PdfPCell cell7 = new PdfPCell();
        // 2. Inside that table, make each cell with specific height:
        cell1.FixedHeight=50;
        cell2.FixedHeight = 50;
        cell3.FixedHeight = 50;
        cell4.FixedHeight = 50;
        cell5.FixedHeight = 50;
        cell6.FixedHeight = 50;
        cell7.FixedHeight = 50;
        // 3. Each cell has the same background image
        string path = string.Concat(this.openFileDialog_pic.FileName);
        string imageFilePath = string.Concat(Environment.GetEnvironmentVariable("."), path);
        iTextSharp.text.Image IMG = iTextSharp.text.Image.GetInstance(imageFilePath);

        ImageEvent imgEvent = new ImageEvent(iTextSharp.text.Image.GetInstance(IMG));
        cell1.CellEvent=imgEvent;
        cell2.CellEvent = imgEvent;
        cell3.CellEvent = imgEvent;
        cell4.CellEvent = imgEvent;
        cell5.CellEvent = imgEvent;
        cell6.CellEvent = imgEvent;
        cell7.CellEvent = imgEvent;
        // 4. Add text in front of the image at specific position
        cell1.CellEvent= new PositionEvent(new Phrase("Top left"), "TOP_LEFT");
        cell2.CellEvent=new PositionEvent(new Phrase("Top right"), "TOP_RIGHT");
        cell3.CellEvent=new PositionEvent(new Phrase("Bottom left"), "BOTTOM_LEFT");
        cell4.CellEvent=new PositionEvent(new Phrase("Bottom right"), "BOTTOM_RIGHT");
        cell5.CellEvent = new PositionEvent(new Phrase("Center Top"), "CENTER_TOP");
        cell6.CellEvent = new PositionEvent(new Phrase("Center Bottom"), "CENTER_BOTTOM");
        cell7.CellEvent = new PositionEvent(new Phrase("Center Middle"), "CENTER_MIDDLE");
        // Wrap it all up!
        table.AddCell(cell1);
        table.AddCell(cell2);
        table.AddCell(cell3);
        table.AddCell(cell4);
        table.AddCell(cell5);
        table.AddCell(cell6);
        table.AddCell(cell7);
        document.Add(table);
        document.Close();
    }

在附加评论中,您阐明了您的要求:

  1. 我想创建一个包含 table 的文档。在 table、细胞
  2. 里面
  3. 我想让每个单元格都有特定的高度
  4. 每个单元格都有相同的背景图片
  5. 我想在单元格内我想要的位置的图像前面放置一个文本。例如:单元格的左上角,单元格的右下角

换句话说:你想要这样的东西:position_content_in_cell.pdf

有不止一种方法可以做到这一点。我不明白您在问题中使用的代码示例。它使用嵌套 tables 我不明白为什么你需要嵌套 tables.

PositionContentInCell 示例中,我使用了一种方法,可以让您真正微调文本的确切位置。我创建了一个 ImageEvent 来缩放和居中图像:

class ImageEvent implements PdfPCellEvent {
    protected Image img;
    public ImageEvent(Image img) {
        this.img = img;
    }
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        img.scaleToFit(position.getWidth(), position.getHeight());
        img.setAbsolutePosition(position.getLeft() + (position.getWidth() - img.getScaledWidth()) / 2,
                position.getBottom() + (position.getHeight() - img.getScaledHeight()) / 2);
        PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
        try {
            canvas.addImage(img);
        } catch (DocumentException ex) {
            // do nothing
        }
    }
}

我创建了一个 PositionEvent 以在单元格内添加文本:

class PositionEvent implements PdfPCellEvent {
    protected Phrase content;
    protected POSITION pos;

    public PositionEvent(Phrase content, POSITION pos) {
        this.content = content;
        this.pos = pos;
    }

    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
        float x = 0;
        float y = 0;
        int alignment = 0;
        switch (pos) {
            case TOP_LEFT:
                x = position.getLeft(3);
                y = position.getTop(content.getLeading());
                alignment = Element.ALIGN_LEFT;
                break;
            case TOP_RIGHT:
                x = position.getRight(3);
                y = position.getTop(content.getLeading());
                alignment = Element.ALIGN_RIGHT;
                break;
            case BOTTOM_LEFT:
                x = position.getLeft(3);
                y = position.getBottom(3);
                alignment = Element.ALIGN_LEFT;
                break;
            case BOTTOM_RIGHT:
                x = position.getRight(3);
                y = position.getBottom(3);
                alignment = Element.ALIGN_RIGHT;
                break;
        }
        ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
    }
}

这就是我使用这些事件的方式:

public void createPdf(String dest) throws IOException, DocumentException {
    // 1. Create a Document which contains a table:
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell1 = new PdfPCell();
    PdfPCell cell2 = new PdfPCell();
    PdfPCell cell3 = new PdfPCell();
    PdfPCell cell4 = new PdfPCell();
    // 2. Inside that table, make each cell with specific height:
    cell1.setFixedHeight(50);
    cell2.setFixedHeight(50);
    cell3.setFixedHeight(50);
    cell4.setFixedHeight(50);
    // 3. Each cell has the same background image
    ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG));
    cell1.setCellEvent(imgEvent);
    cell2.setCellEvent(imgEvent);
    cell3.setCellEvent(imgEvent);
    cell4.setCellEvent(imgEvent);
    // 4. Add text in front of the image at specific position
    cell1.setCellEvent(new PositionEvent(new Phrase("Top left"), POSITION.TOP_LEFT));
    cell2.setCellEvent(new PositionEvent(new Phrase("Top right"), POSITION.TOP_RIGHT));
    cell3.setCellEvent(new PositionEvent(new Phrase("Bottom left"), POSITION.BOTTOM_LEFT));
    cell4.setCellEvent(new PositionEvent(new Phrase("Bottom right"), POSITION.BOTTOM_RIGHT));
    // Wrap it all up!
    table.addCell(cell1);
    table.addCell(cell2);
    table.addCell(cell3);
    table.addCell(cell4);
    document.add(table);
    document.close();
}

通常情况下,我会以更高效的方式编写此代码,但我以某种方式对代码行进行排序,以便它们从字面上反映您的要求 1、2、3 和 4。

更新:

在评论中,您问了几个额外的问题。例如:如何拉伸图片:

你能够回答大部分问题,例如根据我的提示使用 ScaleAbsolute:

public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
    img.scaleAbsolute(position.getWidth(), position.getHeight());
    img.setAbsolutePosition(position.getLeft(), position.getBottom());
    PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
    try {
        canvas.addImage(img);
    } catch (DocumentException ex) {
        // do nothing
    }
}

你还有一个问题需要额外的例子(很难在评论框中解释)。我将该示例命名为 PositionContentInCell2

您询问是否可以传递 xy 值,而不是使用 POSITION 枚举。你可以这样做,但你可能不会总是知道单元格的宽度和高度,所以为什么不定义诸如 wPcthPct 之类的百分比以及对齐方式:

class PositionEvent implements PdfPCellEvent {
    protected Phrase content;
    protected float wPct;
    protected float hPct;
    protected int alignment;

    public PositionEvent(Phrase content, float wPct, float hPct, int alignment) {
        this.content = content;
        this.wPct = wPct;
        this.hPct = hPct;
        this.alignment = alignment;
    }

    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
        float x = position.getLeft() + wPct * position.getWidth();
        float y = position.getBottom() + hPct * (position.getHeight() - content.getLeading());
        ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
    }
}

现在您可以像这样添加这些事件:

cell1.setCellEvent(new PositionEvent(new Phrase(14, "Top left"), 0, 1, Element.ALIGN_LEFT));
cell2.setCellEvent(new PositionEvent(new Phrase(14, "Top right"), 1, 1, Element.ALIGN_RIGHT));
cell3.setCellEvent(new PositionEvent(new Phrase(14, "Top center"), 0.5f, 1, Element.ALIGN_CENTER));
cell4.setCellEvent(new PositionEvent(new Phrase(14, "Bottom center"), 0.5f, 0, Element.ALIGN_CENTER));
cell5.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
cell6.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
cell7.setCellEvent(new PositionEvent(new Phrase(14, "Bottom left"), 0, 0, Element.ALIGN_LEFT));
cell8.setCellEvent(new PositionEvent(new Phrase(14, "Bottom right"), 1, 0, Element.ALIGN_RIGHT));

当然,如果你更喜欢传递 xy(毕竟:你确实知道高度,因为你定义的是固定高度,代码可以变得更简单:你不要将变量与 position.getWidthposition.getHeight().

相乘