首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ITextSharp:单元格中的文本不绕排图像

ITextSharp:单元格中的文本不绕排图像
EN

Stack Overflow用户
提问于 2012-05-23 20:37:30
回答 2查看 4.7K关注 0票数 4

我遇到了真正的问题,我有一个包含短语的单元格,我想在短语的左侧添加一个小图标,但每个元素都显示在新行上

下面是我返回单元格的代码:

代码语言:javascript
复制
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;

下面是输出:

任何帮助都将不胜感激

--edit:下面是设置了imgIcon的对齐属性的输出

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-25 15:54:43

iTextSharp Image对象显示为块元素(在CSS术语中)。您需要显式地将Image包装在Chunk中以获得内联显示,如下所示:

代码语言:javascript
复制
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);

代码片段结果:

票数 3
EN

Stack Overflow用户

发布于 2012-05-23 20:43:23

我认为这个答案是:

Image alignment in text?

可能会有答案。您需要设置图像的对齐方式:

代码语言:javascript
复制
....
imgIcon.Alignment = 6;
cell.AddElement(imgIcon);
....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10720064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档