c# - ITextSharp : Text not wrapping around image within cell

标签 c# image pdf itext

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

这是我返回单元格的代码:

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;

这是输出:

enter image description here

任何帮助将不胜感激

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

enter image description here

最佳答案

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

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);

代码片段结果:

Code snippet result

关于c# - ITextSharp : Text not wrapping around image within cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10720064/

相关文章:

c# - 从 ASP.NET 应用程序使用 AD 密码身份验证连接到 Azure SQL 失败,并显示 "Need user interface to continue"

c# - 在没有 WinForm 控件的情况下在主线程上调用一个方法来调用 Invoke 或 BeginInvoke

c# - Enterprise Library 5.0如何安装?

iOS如何获取PDF页面中的所有单词坐标

java - 如何在java中使用itext检查pdf是否 protected

c# - 使用 Excel Interop 手动编译 PERSONAL.xlsb 时,将快捷键分配给 VBA 宏?

php - 如何为使用 ffmpeg-php 生成的图像文件使用随机名称?

php - 使用 PHP GD 合并两个图像 (.JPG)

reactjs - 如何通过一劳永逸地重新渲染来修复图像闪烁?

ruby-on-rails - Rails Prawn文档的嵌入式字体错误