English 中文(简体)
ITextSharrp: 单元格内不环绕图像的文本
原标题:ITextSharp: Text not wrapping around image within 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;

这是输出 :

""https://i.sstatic.net/U6Kn.png" alt="此处的内置图像描述"/ >

如有任何帮助,将不胜感激。

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

""https://i.sstatic.net/5qUjY.png" alt="此处输入图像描述"/ >

最佳回答

iTextSharp < code> Image 对象显示为 < em> block 元素( 在 CSS 术语中) 。 您需要将 < code> Image 明确包裹在 < code > Chunk 中, 才能获得 < em > inline 显示, 类似 :

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

代码片断结果 :

"https://i.sstatic.net/c8A0g.png" alt="代码片断结果"/ >

问题回答

我认为,这一答案:

文本中的图像对齐?

可能找到答案。 您需要设置图像对齐 :

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签