0

I am trying to display some images in pdf using iTextSharp.Its working fine but my problem is, some images displays in zooming from its actual size like below image,

enter image description here The code I tried to display is,

iTextSharp.text.Font fontH1 = new iTextSharp.text.Font(Currier, 18, iTextSharp.text.Font.BOLD);
Document doc1 = new Document();
string path1 = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
path1 = path1 + "\\DOC\\Mathematicsquestions.pdf";
iTextSharp.text.Rectangle pageSize1 = doc1.PageSize;
PdfWriter pdf = PdfWriter.GetInstance(doc1, new FileStream(path1, FileMode.Create));
doc1.Open();
pdf.Open();

PdfPTable table = new PdfPTable(2);
//actual width of table in points
table.TotalWidth = 500f;
//fix the absolute width of the table
table.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
float[] widths = new float[] { 0.15f, 2.5f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
//leave a gap before and after the table
table.SpacingBefore = 0f;
table.SpacingAfter = 0f;

PdfPCell cell = new PdfPCell(new Phrase("MATHEMATICS", fontH1));
cell.Colspan = 2;
cell.Border = 0;
cell.HorizontalAlignment = 1;
table.AddCell(cell);

PdfPCell cell1 = new PdfPCell(new Phrase("  "));
cell1.Colspan = 2;
cell1.Border = 0;
cell1.HorizontalAlignment = 1;
table.AddCell(cell1);

for (int i = 0; i < mach.Count; i++)
{
    string temsub = mach[i].ToString();
    var quepaper1 = from fm in en.Entrance_jee where fm.En_Chapter == temsub select fm;
    foreach (Entrance_jee re in quepaper1)
    {
        if (newsno == 0)
        {
            newsno = 1;
        }
        else
        {
            newsno = newsno + 1;
        }
        if (re.En_Isimage == true)
        {
            imgepath = path + re.En_Questionpage1 + ".png";
            imgepath2 = path + re.En_Answer + ".png";
            filename = System.IO.Path.GetFileName(imgepath);
            filename2 = System.IO.Path.GetFileName(imgepath2);
            decfile = decfile1 + "\\R1\\CF\\" + filename;
            decfile2 = decfile1 + "\\R1\\CF\\" + filename2;
            string status = encobj.DecryptFile(imgepath, decfile);
            status = encobj.DecryptFile(imgepath2, decfile2);
            if (status == "decrypted")
            {
                byte[] file = File.ReadAllBytes(decfile);
                File.Delete(decfile);
                iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(file);
                byte[] file2 = File.ReadAllBytes(decfile2);
                File.Delete(decfile2);
                iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(file2);
                //iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell();
                //imgCell1.AddElement(new Chunk(img2, 0, 0));

                table.AddCell(newsno.ToString());
                table.AddCell(img1);
                table.AddCell(" ");
                table.AddCell(img2);
            }
        }
        else
        {
            table.AddCell(newsno.ToString());
            table.AddCell(re.En_Questionpage1.ToString());
            table.AddCell(" ");
            table.AddCell(re.En_Answer.ToString());
        }
    }
}

doc1.Add(table);
doc1.Close();

Updated : Here is my orginal images,

enter image description here

enter image description here

Someone tell me where I am wrong?

Liath
  • 9,913
  • 9
  • 51
  • 81
MMMMS
  • 2,179
  • 9
  • 43
  • 83
  • Can you share the original images? – mkl Dec 10 '14 at 08:52
  • The answer of @Kami already indicates the problem. I initially was a bit surprised that the question seemed to not have been enlarged, but your posted images show that the question image has quite a lot of white area to its right; thus, the image already uses all the cell width. – mkl Dec 10 '14 at 11:24
  • 1
    Have you tried `table.AddCell(new PdfPCell(img2))` instead of `table.AddCell(img2)` as indicated in the message Bruno linked to? – mkl Dec 10 '14 at 13:52

1 Answers1

0

The image appears to be scaled to the full width of the table. You need to scale it to your exact size requirements.

Try something like

iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(file);
iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(file2);

img1.ScaleToFit(500f, 37f);
img2.ScaleToFit(81f, 36f);

Or alternatively you can add a container within the cell with the required dimensions and add the image to it. See full details on - http://www.mikesdotnetting.com/article/87/itextsharp-working-with-images

Kami
  • 19,134
  • 4
  • 51
  • 63