Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a paragraph with an image to a cell in a table in iTextSharp?

Tags:

c#

itextsharp

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.

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

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

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:

DocumentBody => Table => Cell => Paragraph => Drawing

So in iTextSharp terms, that maps to:

Document => Table => Cell => Paragraph => Image

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:

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

If someone could help me get this image into the cell without being resized, that would be perfect.

UPDATE *

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.

like image 334
Chris B. Behrens Avatar asked Sep 22 '14 17:09

Chris B. Behrens


2 Answers

Try adding the Image to the cell directly instead of adding it to the Paragraph first:

var cell = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 };
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.AddElement(paragraph);
cell.AddElement(image);

Reading your example, you could also omit the cell.AddElement(paragraph); line because your paragraph seems to be empty.

Note that using addElement(image) will scale the image to fit 100% of the width of the table column (you typically don't want the image to overlap with the other columns). You can use the setWidthPercentage() method on the image to change this percentage.

like image 68
Bruno Lowagie Avatar answered Oct 18 '22 20:10

Bruno Lowagie


In your code where you add the image to paragraph add a fourth parameter to Chunk

paragraph.Add(new Chunk(image, 0, 0, true));

The leading is fixed so the image is resized to fit. Setting

changeLeading = true //fourth parameter

the image is displayed normally