Putting multiple images next to each other in a pdf file using itextsharp

I am using itextsharp with an aspmvc project to generate PDF versions of some of my pages. I have a very simple parser that takes simple html (plus some style information provided separately) and creates a pdf file. When my parser concatenates the table, it iterates over rows and then cells, creating a PdfPCell for each cell. It then iterates over the children of the cell, adding them to PdfPCell. It's pretty basic, but it worked for me so far.

The problem is that I now have a table, one column of which contains multiple icons indicating a different status for the row. When these images are added, they appear one above the other in the pdf, and not next to each other.

I am creating an image with

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

      

I tried

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

      

and adding a text snippet containing a space, but that doesn't help. The only one I have seen is using I.SetAbsolutePosition()

. I would rather avoid the absolute position, but I'm willing to give it a try - can't I figure out how to find the current X position?

Any help is greatly appreciated.

Adam

+2


source to share


1 answer


To get the right flow side by side, wrap the images / text in an object Paragraph

, adding them one by one using objects Chunk

and Phrase

. 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 spaces between images. Will only work with images; if you try this with mixed text / images they 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);

      

See Chunk documentation .

+5


source







All Articles