ITextSharp:文本没有环绕单元格中的图像 [英] ITextSharp: Text not wrapping around image within cell

查看:247
本文介绍了ITextSharp:文本没有环绕单元格中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了真正的问题,我有一个带短语的单元格,我想在短语的左边有一个小图标,但每个元素都在新行上呈现

I am having real problems, I have a cell with a phrase, and I want a small icon to the left of the phrase, but each element is being rendered on 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;

这里是输出:

< img src =https://i.stack.imgur.com/Un6Kn.pngalt =在此处输入图片说明>

任何帮助将不胜感激

- 编辑:
这里是imgIcon的对齐属性设置的输出

--edit: here is the output with imgIcon's alignment property set

推荐答案

iTextSharp 图像对象显示为元素(以CSS术语表示)。您需要在 Chunk 中明确地包装 Image 以获取内联显示,类似于这个:

The iTextSharp Image object is displayed as a block element (in CSS terms). You need to explicitly wrap the Image in a Chunk to get inline 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's containing text."));
cell.AddElement(p);
table.AddCell(cell);
document.Add(table);

代码段结果:

这篇关于ITextSharp:文本没有环绕单元格中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆