Setting margins, or cell spacing, between a grid of images in iTextSharp PdfPTable
<p>I am trying to render a PDF with a grid of images, using iTextSharp (v5.5.10). The images will all have the same dimensions and should be evenly distributed on one page. </p>

<p>However, with the code mentioned below, I'm having difficulty in setting the appropriate margins, or spacing between the cells.</p>

<p>Visually this means that the expected result is this:</p>

<p><a href="https://i.stack.imgur.com/n2e6x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n2e6x.png" alt="expected"></a></p>

<p>The <strong>yellow highlighted lines</strong> are the problem where I'm getting the following result instead:</p>

<p><a href="https://i.stack.imgur.com/X5XEi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X5XEi.png" alt="actual"></a></p>

<p>Notice how there are no spaces between the images? This is based on my following code:</p>

<pre class="lang-cs prettyprint-override"><code>   public void CreateGridOfImages(string outputFilePath)
    {
        // note: these constants are in millimeters (mm), 
        // which are converted using the ToPoints() helper later on
        const float spacingBetweenCells = 7;
        const float imageWidth = 80;
        const float imageHeight = 80;
        const string[] images = new [] { "a.jpg", "b.jpg", "c.jpg" };

        using (var stream = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None))
        {
            var document = new iTextSharp.text.Document(PageSize.B2, 0f, 0f, 0f, 0f);

            var writer = PdfWriter.GetInstance(document, stream);

            try
            {
                document.Open();

                var table = new PdfPTable(5);
                table.DefaultCell.Border = Rectangle.NO_BORDER;

                foreach (var imagePath in images)
                {
                    var img = iTextSharp.text.Image.GetInstance(imagePath);
                    img.ScaleToFit(ToPoints(imageWidth), ToPoints(imageHeight));

                    var cell = new PdfPCell();

                    // THIS IS THE PROBLEM... HOW TO SET IMAGE SPACING?
                    var cellMargin = ToPoints(spacingBetweenCells);                       

                    cell.AddElement(img);

                    table.AddCell(cell);
                }

                document.Add(table);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                document.Close();
            }
        }
    }

    private float ToPoints(float millimeters)
    {
        // converts millimeters to points
        return iTextSharp.text.Utilities.MillimetersToPoints(millimeters);
    }
</code></pre>

<p>Now this seems trivial. And it problably is, but I've tried several options and could none of them to work properly (or at all):</p>

<ul>
<li>Adding Paragraph() objects with paddings between each </li>
<li>Adding Padding to PdfPCell does not seem to work for me</li>
<li>Looked into custom IPdfPCellEvent samples</li>
<li>Absolutely positioning the images alltogether (forgetting the PdfPTable)</li>
</ul>

<p>My hunch is that the <code>IPdfPCellEvent</code> seems the right approach. But all the iText options and variations are simply overwhelming.</p>

<p>Summarized, does anyone know how can I properly <strong>set the margins/spacing between the cells</strong>?</p>