ITextSharp: text not wrapping an image inside a cell

I have real problems, I have a cell with the phrase, and I want a small icon to the left of the phrase, but each item is displayed in a new line

here is my code that returns the cell:

var cell = new PdfPCell(); Bitmap bitmap = new Bitmap(21, 21); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); SolidBrush brush = new SolidBrush(colour); g.FillEllipse(brush, 0, 0, 20, 20); brush.Dispose(); g.DrawImageUnscaled(bitmap, 0, 0); g.Dispose(); var imgIcon = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Dispose(); //imgIcon.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_RIGHT; cell.AddElement(imgIcon); var phrase = new Phrase(o.ToString(), Report.Fonts.Table); cell.AddElement(phrase); //this code scales the image so that it does not fit the cell foreach (IElement element in cell.CompositeElements) { PdfPTable tblImg = element as PdfPTable; if (tblImg != null) { tblImg.TotalWidth = 10; tblImg.LockedWidth = true; } } return cell; 

here is the result:

enter image description here

any help would be greatly appreciated

- edit: the imgIcon alignment set property is displayed here

enter image description here

+4
source share
2 answers

The iTextSharp Image object is displayed as a block element (in terms of CSS). You need to explicitly wrap Image in Chunk to get a built-in display, something like this:

 PdfPTable table = new PdfPTable(1) { TotalWidth = 100, LockedWidth = true, HorizontalAlignment = Element.ALIGN_LEFT }; PdfPCell cell = new PdfPCell(); Phrase p = new Phrase(new Chunk(image, 0, 0)); p.Add(new Phrase("Print")); cell.AddElement(p); table.AddCell(cell); cell = new PdfPCell(); p = new Phrase(new Chunk(image, 0, 0)); p.Add(new Phrase("A long phrase that will make the PdfPCell wrap it containing text.")); cell.AddElement(p); table.AddCell(cell); document.Add(table); 

The result of the code snippet:

Code snippet result

+3
source

I think this answer:

Aligning images in text?

Maybe the answer. You need to set the image alignment:

 .... imgIcon.Alignment = 6; cell.AddElement(imgIcon); .... 
0
source

Source: https://habr.com/ru/post/1413943/


All Articles