![](/img/trans.png)
[英]HorizontalAlignment on image not working in PdfPCell in itextsharp
[英]How to put an image on top of another image in a PdfPCell (iTextSharp)?
我有兩個圖像( iTextSharp.text.Image
),我想將它們直接放在PdfPCell
中彼此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
。
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 ); } }
創建PdfPCell
,將其設置為CellEvent
屬性,然后添加“頂部”圖像。
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); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.