title: How do you add a paragraph with an image to a cell in a table in iTextSharp?
body: <p>I'm creating a parser that converts Word documents to PDFs with some specialized processing along the way. I've chosen to make it a recursive parser to match with the structure of OpenXml. </p>
  
  <p>I've run into some problems handling images. Given the structure of OpenXml, an image is always going to be a drawing element inside of a paragraph. If the paragraph is directly in the doc, this works fine, essentially like so (recursion untangled for this example):</p>
  
  <pre><code>using (var document = new Document(PageSize.A4))
  {
      PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
      document.Open();
  
      var paragraph = new Paragraph();
  
      var image = Image.GetInstance(@"C:\image1.jpg");
  
      paragraph.Add(image);
      document.Add(paragraph);
  
      document.Close();
  }
  </code></pre>
  
  <p>This code correctly inserts the image into the document. The problem arises when the image is inside a table, which is common in the documents we're working with. The OpenXml structure there is going to end up like so:</p>
  
  <p>DocumentBody => Table => Cell => Paragraph => Drawing</p>
  
  <p>So in iTextSharp terms, that maps to:</p>
  
  <p>Document => Table => Cell => Paragraph => Image</p>
  
  <p>Adding a paragraph with an image directly to a cell produces an empty, zero-height table. If I add a chunk to the paragraph, the image appears but is drastically resized (smaller) - I can't figure out what the basis for that resizing is:</p>
  
  <pre><code>using (var document = new Document(PageSize.A4))
  {
      PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
      document.Open();
  
      var paragraph = new Paragraph();
  
      var image = Image.GetInstance(@"C:\image1.jpg");
  
      paragraph.Add(new Chunk(image, 0, 0));
  
      var table = new PdfPTable(1);
  
      var cell = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 };
  
      cell.HorizontalAlignment = Element.ALIGN_CENTER;
      cell.AddElement(paragraph);
  
      table.AddCell(cell);
  
      document.Add(table);
  
      document.Close();
  }
  </code></pre>
  
  <p>If someone could help me get this image into the cell <em>without being resized</em>, that would be perfect. </p>
  
  <p>UPDATE <em>*</em></p>
  
  <p>I have determined that the image itself is not being resized natively - if I extract the image from the resulting pdf and save it, it retains the dimensions of the original image.</p>