-1

On the public/images folder in my server I have uploaded the jpg image with 2048x1536 pixel and 290 kb.

I have memorized in MySQL table database on the field Pic this link : http://mywebserver.xxx/aspx/public/images/IMG0006A.jpg

I need print this image on pdf file using iTextSharp library and resized this image because its original file size are off page pdf.

I have find this similar question and I have tried the suggestion without success because in the output the jpg image printed on pdf file is always out page.

How to do resolve this ?

Can you help me?

Thank you in advance, my code below.

.aspx markup

<table border="1">
    <tr>
        <td colspan="2">
            <label for="Pic">
                Pic<br />
            </label>
            <asp:Image ID="Pic" runat="server" /></td>
    </tr>
</table>

.cs code-behind

    if (!String.IsNullOrEmpty(reader["Pic"].ToString()))
    {
        lbPic.ImageUrl = reader["Pic"].ToString();
        iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell();
        iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(Server.MapPath("\\public\\images\\IMG0006A.jpg"));
        imgCell1.AddElement(new Chunk(img1, 0, 0));
        img1.ScaleToFit(120f, 155.25f);
        img1.ScaleAbsolute(120f, 155.25f);
    }
Community
  • 1
  • 1
Antonio Mailtraq
  • 1,397
  • 5
  • 34
  • 82
  • I wonder how the image can show at all as both your cell and your image objects are out of range at the end of that snippet without being used otherwise.. – mkl Aug 10 '16 at 11:07
  • I am not sure you need to use both `ScaleAbsolute` and `ScaleToFit`. – Fred Feb 12 '20 at 12:31

1 Answers1

0

I don't use and don't know iTextSharp library but I think that you resolve this problem using resizing image method in upload file.

e.g.

private int ResizeMethod(FileUpload upload, int iFileCnt)
{
    Stream strm = upload.PostedFile.InputStream;
    using (var image = System.Drawing.Image.FromStream(strm))
    {
        int newWidth = 450;
        int newHeight = 350;

        var thumbImg = new Bitmap(newWidth, newHeight);
        var thumbGraph = Graphics.FromImage(thumbImg);
        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
        thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
        var imgRectangle = new Rectangle(0, 0, newWidth, newHeight);
        thumbGraph.DrawImage(image, imgRectangle);

        string extension = Path.GetExtension(upload.PostedFile.FileName);
        string targetPath = Server.MapPath("\\public\\images\\" + upload.FileName.ToString());
        thumbImg.Save(targetPath, image.RawFormat);
        iFileCnt += 1;
    }

    return iFileCnt;
}

Hope that this is help for you-

Hamamelis
  • 1,983
  • 8
  • 27
  • 41