Pierre
Pierre

Reputation: 9052

iTextSharp Image minimum Table cell height

I am trying to append images to a PDF document, but the image width must = doc.PageSize.Width and height in ratio with the image width.

I am appending individual images, each in its own table and in a cell using the following method

public void AddImage(Document doc, iTextSharp.text.Image Image)
{
    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;
    table.TotalWidth = doc.PageSize.Width;

    PdfPCell c = new PdfPCell(Image, true);
    c.Border = PdfPCell.NO_BORDER;
    c.Padding = 5;
    c.FixedHeight = (doc.PageSize.Width / Image.Width) * Image.Height;
    c.MinimumHeight = (doc.PageSize.Width / Image.Width) * Image.Height;

    table.AddCell(c);

    doc.Add(table);
}

The Document code part (Do not think this is necessary though):

using (PDFBuilder pdf = new PDFBuilder())
{
    using (var doc = new Document())
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        var image = iTextSharp.text.Image.GetInstance(Request.MapPath("~/Images/Nemo.jpg"));
        pdf.AddImage(doc, image);
        pdf.AddImage(doc, image);
        pdf.AddImage(doc, image);
    }
}

What I want is for the images to be 100% width, and if not, the image must append on the next page.

This is currently what I am getting

enter image description here

And this is what I want

enter image description here

Any help would be greatly appreciated, thank you in advance!

Upvotes: 0

Views: 3545

Answers (1)

Pierre
Pierre

Reputation: 9052

Got It!

This line needed to be added:

c.Image.ScaleToFitHeight = false;

My Method

public void AddImage(Document doc, iTextSharp.text.Image Image)
{
    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;

    PdfPCell c = new PdfPCell(Image, true);
    c.Border = PdfPCell.NO_BORDER;
    c.Padding = 5;
    c.Image.ScaleToFitHeight = false; /*The new line*/

    table.AddCell(c);

    doc.Add(table);
}

Upvotes: 2

Related Questions