Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将图像置于PdfPCell(iTextSharp)中的另一图像之上?_C#_.net_Pdf Generation_Itextsharp - Fatal编程技术网

C# 如何将图像置于PdfPCell(iTextSharp)中的另一图像之上?

C# 如何将图像置于PdfPCell(iTextSharp)中的另一图像之上?,c#,.net,pdf-generation,itextsharp,C#,.net,Pdf Generation,Itextsharp,我有两个图像(iTextSharp.text.Image),我想把它们直接放在PdfPCell中的一个图像上。其中一幅图像是透明的,应该放在最上面 我尝试了以下方法: imgOpaque.Alignment = Image.UNDERLYING; imgOpaque.SetAbsolutePosition(10f, 10f); imgTransparent.SetAbsolutePosition(10f, 10f); var cell = new PdfPCell(); cell.AddEle

我有两个图像(
iTextSharp.text.Image
),我想把它们直接放在
PdfPCell
中的一个图像上。其中一幅图像是透明的,应该放在最上面

我尝试了以下方法:

imgOpaque.Alignment = Image.UNDERLYING;
imgOpaque.SetAbsolutePosition(10f, 10f);
imgTransparent.SetAbsolutePosition(10f, 10f);

var cell = new PdfPCell();
cell.AddElement(imgOpaque);
cell.AddElement(imgTransparent);
table.AddCell(cell);
但这会导致第二个图像位于第一个图像之后,而不是顶部

如何将这两个图像叠加在一起?


iTextSharp版本是5.4.3。

为“底部”图像实现
IPdfPCellEvent

创建
PdfPCell
,设置它的
CellEvent
属性,并添加“top”图像

public class CellBackgroundImage : IPdfPCellEvent {
    private Image _background;
    public void SetImage(string path)  {
        _background = Image.GetInstance(path);
    }
    public void CellLayout(
        PdfPCell cell, Rectangle rectangle, PdfContentByte[] pcb) 
    {
        PdfContentByte cb = pcb[PdfPTable.BACKGROUNDCANVAS];
        cb.AddImage(_background, 
            rectangle.Width, 0, 0, rectangle.Height
           ,rectangle.Left, rectangle.Bottom
        );
    }
}
using (Document document = new Document()) {
    PdfWriter.GetInstance(document, stream); // any Stream object
    document.Open();
    PdfPTable table = new PdfPTable(1);
    CellBackgroundImage cbi = new CellBackgroundImage();
    cbi.SetImage(imageBottomPath);
    Image imageTop = Image.GetInstance(imageTopPath);
    PdfPCell imageCell = new PdfPCell();
    imageCell.CellEvent = cbi;
    imageCell.AddElement(imageTop);
    table.AddCell(imageCell);
    document.Add(table);
}