使用itextsharp将多个图像彼此相邻放在pdfcell中 [英] Putting several images next to each other in a pdfcell with itextsharp

查看:178
本文介绍了使用itextsharp将多个图像彼此相邻放在pdfcell中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有aspmvc项目的itextsharp来创建某些页面的PDF版本。我有一个非常基本的解析器,它采用简单的html(加上一些单独提供的样式信息)并创建一个pdf。当我的解析器装入一个表时,它会循环遍历行然后是单元格,为每个单元格创建一个PdfPCell。然后它循环遍历单元的子元素,将它们添加到PdfPCell。这是非常基本的,但到目前为止它对我有用。

I'm using itextsharp with a aspmvc project to create PDF versions of some of my pages. I have a very basic parser which takes simple html (plus some style info supplied seperately) and creates a pdf. When my parser encounts a table it loops over rows then cells, creating a PdfPCell for each cell. It then loops over child elements of the cell adding them to the PdfPCell. It's pretty basic but it's worked for me so far.

问题是我现在有一个表,其中一列包含许多图标,表示不同的状态行。当这些图像被添加后,然后在pdf中一个在另一个上面,而不是彼此相邻。

The problem is that I now have a table one column of which contains a number of icons, indicating different status for the row. When these images get added then end up one above the other in the pdf, instead of next to each other.

我使用

Dim I As iTextSharp.text.Image = Image.GetInstance(HttpContext.Current.Server.MapPath(El.Attributes("src").InnerText))

我试过了

I.Alignment = Image.TEXTWRAP Or Image.ALIGN_LEFT Or Image.ALIGN_MIDDLE

并添加一个文本块后面包含一个空格,但它没有帮助。我看到的唯一建议是使用 I.SetAbsolutePosition()。我宁愿避免绝对的位置,但我准备尝试 - 除了我无法弄清楚如何找到当前的X位置使用?

and adding a text chunk afterwards containing a space but it doesn't help. The only suggestion I have seen is to use I.SetAbsolutePosition(). I'd rather avoid absolute position but am prepared to try it - except I can't work out how to find the current X position to use?

任何帮助非常感谢。

Adam

推荐答案

为了得到正确的方法侧流,将图像/文本包装在段落对象中,使用和<$ c逐个添加它们$ c>短语对象。某事(对不起,我不做VB)是这样的:

To get the proper side-by-side flow, wrap the images/text in a Paragraph object, adding them one by one using Chunk and Phrase objects. Something (sorry, I don't do VB) like this:

PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
p.Add(new Phrase("Test "));
p.Add(new Chunk(image, 0, 0));
p.Add(new Phrase(" more text "));
p.Add(new Chunk(image, 0, 0));
p.Add(new Chunk(image, 0, 0));
p.Add(new Phrase(" end."));
cell.AddElement(p);
table.AddCell(cell);
table.AddCell(new PdfPCell(new Phrase("test 2")));
document.Add(table);

编辑:在图片之间添加空格的一种方法。将处理图片;如果您尝试使用混合文本/图像,它们将重叠:

EDIT: One way to add whitespace between the images. Will only work with images; if you try this with mixed text/image(s), they will overlap:

PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
float offset = 20;
for (int i = 0; i < 4; ++i) {
  p.Add(new Chunk(image, offset * i, 0));
}
cell.AddElement(p);
table.AddCell(cell);
table.AddCell(new PdfPCell(new Phrase("cell 2")));
document.Add(table);

参见块文档

这篇关于使用itextsharp将多个图像彼此相邻放在pdfcell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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