2

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.

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.

I create the image with

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

I have tried

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

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?

Any help much appreciated.

Adam

kuujinbo
  • 9,272
  • 3
  • 44
  • 57
Adam
  • 6,539
  • 3
  • 39
  • 65

1 Answers1

6

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

See the Chunk documentation.

kuujinbo
  • 9,272
  • 3
  • 44
  • 57
  • Many thanks. I had tried putting it in a chunk but got an exception - it compiles with just adding the image to the chunk but then you get an "Unable to cast object of type 'iTextSharp.text.ImgRaw' to type 'iTextSharp.text.pdf.draw.IDrawInterface'" exception at runtime. Adding the extra paramaters to the chunk constructor fixed that. One follow-up question: it doesn't seem to give effect to the SpacingAfter property on the image this way? I set I.SpacingAfter = 30 but the images still are right up against each other. I had to add a chunk containing a space between them to get any space. – Adam Dec 13 '11 at 09:22
  • Correct - `Chunk` isn't whitespace-aware like `Paragraph`, for example. That includes newlines. The easiest way to see this is to add a very long string (past the page margins) - you end up with garbled text because the string overwrites the same line over and over. If you do web design/development, it's analogous to inline/block elements. Lastly, `SpacingAfter` is for **vertical** spacing. See the edited answer for one way to put space between the images. Update/addition will **only** work with images; if you're adding text, the image(s) and text will overlap. – kuujinbo Dec 13 '11 at 11:24