Karthik
Karthik

Reputation: 2399

Unable to set a fixed size for image itextsharp

I have been using iTextSharp for creating a report. My report has many images which are of varying sizes. Each image renders in different size eventhough I scale them. I found many solutions but none helped.

My code:

PdfPCell InnerCell;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath(@"images\Logo.png"));
logo.ScaleToFit(80f, 80f);
InnerCell.FixedHeight = 80f;
InnerCell = new PdfPCell(logo); 

I tried adding the image to the chunk but the image positions itself to the top. Since being a dynamic report I can't specify the x and y values in chunk

InnerCell = new PdfPCell(new Phrase(new Chunk(logo, 0, 0))); 

I even tried this but I can't get a fixed size.

Upvotes: 2

Views: 11078

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

ScaleToFit(w,h) will scale an image proportionately based on the larger of the width/height of the source image. When scaling multiple images, unless the ratio of the dimensions are all the same you will end up with different sizes. This is by design.

Using ScaleToFit(80,80):

  • If your source is a square that's 100x100 you'll get a square that's 80x80
  • If your source is a rectangle that's 200x100 you'll get a rectangle that's 80x40
  • If your source is a rectangle that's 100x200 you'll get a rectangle that's 40x80

Whatever comes out, measure the width and height and at least one will be one of the dimensions that you specified.

I created a sample program that created random sized images and it gave me the expected output shown in the image (w=80,h=80,h=80,h=80,w=80)

enter image description here

private void test() {
    //Output the file to the desktop
    var testFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
    //Standard PDF creation here, nothing special
    using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
        using (var doc = new Document()) {
            using (var writer = PdfWriter.GetInstance(doc, fs)) {
                doc.Open();

                //Create a random number generator to create some random dimensions and colors
                var r = new Random();

                //Placeholders for the loop
                int w, h;
                Color c;
                iTextSharp.text.Image img;

                //Create 5 images
                for (var i = 0; i < 5; i++) {
                    //Create some random dimensions
                    w = r.Next(25, 500);
                    h = r.Next(25, 500);
                    //Create a random color
                    c = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
                    //Create a random image
                    img = iTextSharp.text.Image.GetInstance(createSampleImage(w, h, c));
                    //Scale the image
                    img.ScaleToFit(80f, 80f);
                    //Add it to our document
                    doc.Add(img);
                }

                doc.Close();
            }
        }
    }
}

/// <summary>
/// Create a single solid color image using the supplied dimensions and color
/// </summary>
private static Byte[] createSampleImage(int width, int height, System.Drawing.Color color) {
    using (var bmp = new System.Drawing.Bitmap(width, height)) {
        using (var g = Graphics.FromImage(bmp)) {
            g.Clear(color);
        }
        using (var ms = new MemoryStream()) {
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }
    }
}

I think what you're looking for is the ability to scale an image proportionately but also have the image "be that size" which would mean filling in the rest of the pixels with clear or possibly white pixels. See this post for a solution to that.

Upvotes: 6

Related Questions