图像显示尺寸问题

时间:2014-12-10 06:58:31

标签: c# itextsharp

我正在尝试使用iTextSharp以pdf格式显示一些图像。它工作正常,但我的问题是,有些图像显示为从其实际尺寸缩放,如下图所示

enter image description here 我试图显示的代码是,

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

更新:这是我的原始图片,

enter image description here

enter image description here

有人告诉我哪里错了吗?

1 个答案:

答案 0 :(得分:0)

图像似乎缩放到表格的整个宽度。您需要将其缩放到您确切的尺寸要求。

尝试类似

的内容
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);

或者,您可以在单元格中添加具有所需尺寸的容器,并将图像添加到其中。查看有关 - http://www.mikesdotnetting.com/article/87/itextsharp-working-with-images

的完整详情