Different picture sizes in PDF despite having same resolution & scaletofit, ASP.net iTextSharp

穿精又带淫゛_ 提交于 2019-12-13 04:38:40

问题


I am developing a webapp which is suppose to generate a PDF file that contains 10 pictures. Unfortunately, there are problems with the picture size. Despite having all 10 pictures with the same resolution, the image size turns out differently in the PDF. I have programmatically sized the picture via this method for all 10 pictures

image(number).ScaleToFit(size, size);

And here is the 2 screenshots of the PDF file generated. I somehow managed to align the all of C-IMG(s) in both screenshot.

However as for the P-IMG(s), the first screenshot is working fine. However if i were to add more text into the 2nd page, the picture would not be align properly

How do I ensure those picture are aligned properly? Here is my entire code. I have been trying to solve this for half a day :(

protected void btnPDF_Click(object sender, EventArgs e)
    {

        var doc1 = new Document();
        var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
        var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
        iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
        doc1.Open();


        PdfPTable table = new PdfPTable(1);

        table.TotalWidth = 585f;
        table.LockedWidth = true;


        PdfPTable table2 = new PdfPTable(1);
        table2.TotalWidth = 585f;
        table2.LockedWidth = true;

        iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell();

        var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg"));
        doc1.Add(logo);

        var titleFont = FontFactory.GetFont("Arial", 15, Font.BOLD);
        doc1.Add(new Paragraph("Official Report. Member Report ID : " + DDLCase.SelectedValue, titleFont));

        var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD);
        var phrase = new Phrase();
        var phrase2 = new Phrase();

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");

        SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, pr.policeid,  pr.prdatetime, mr.citizenreport,  mr.image1, mr.image2, mr.image3, mr.image4, mr.image5, pr.policereport, pr.image1, pr.image2, pr.image3, pr.image4, pr.image5, aor.officialreport from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro, AdminOfficialReport aor where mr.memberreportid = '" + DDLCase.SelectedValue + "' and mr.memberreportid=pr.memberreportid and pr.policereportid=aor.policereportid", con);
        con.Open();
        SqlDataReader dr;

        dr = cm.ExecuteReader();

        if (dr.Read())
        {

            phrase.Add(new Chunk("Full Name :", normalFont));
            phrase.Add(dr[0].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Contact :", normalFont));
            phrase.Add(dr[1].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Type Of Crime :", normalFont));
            phrase.Add(dr[2].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Location :", normalFont));
            phrase.Add(dr[3].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Citizen Report Date Time :", normalFont));
            phrase.Add(dr[4].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Police ID :", normalFont));
            phrase.Add(dr[5].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Police Report Date Time :", normalFont));
            phrase.Add(dr[6].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Citizen Report :", normalFont));
            phrase.Add(dr[7].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);


            phrase.Add(new Chunk("C-IMG1 :\u00a0", normalFont));
            Byte[] bytes1 = (Byte[])dr[8];
            iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(480f, 480f);
            Chunk imageChunk1 = new Chunk(image1, 0, 0);
            phrase.Add(imageChunk1);

            phrase.Add(new Chunk("C-IMG2 :\u00a0", normalFont));
            Byte[] bytes2 = (Byte[])dr[9];
            iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(bytes2);
            image2.ScaleToFit(480f, 480f);
            Chunk imageChunk2 = new Chunk(image2, 0, 0);
            phrase.Add(imageChunk2);

            phrase.Add(new Chunk("C-IMG3 :\u00a0", normalFont));
            Byte[] bytes3 = (Byte[])dr[10];
            iTextSharp.text.Image image3 = iTextSharp.text.Image.GetInstance(bytes3);
            image3.ScaleToFit(480f, 480f);
            Chunk imageChunk3 = new Chunk(image3, 0, 0);
            phrase.Add(imageChunk3);

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("C-IMG4 :\u00a0", normalFont));
            Byte[] bytes4 = (Byte[])dr[11];
            iTextSharp.text.Image image4 = iTextSharp.text.Image.GetInstance(bytes4);
            image4.ScaleToFit(1150f, 1150f);
            Chunk imageChunk4 = new Chunk(image4, 0, 0);
            phrase.Add(imageChunk4);

            phrase.Add(new Chunk("C-IMG5 :\u00a0", normalFont));
            Byte[] bytes5 = (Byte[])dr[12];
            iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(1150f, 1150f);
            Chunk imageChunk5 = new Chunk(image5, 0, 0);
            phrase.Add(imageChunk5);

            phrase2.Add(new Chunk("Police Report :", normalFont));
            phrase2.Add(dr[13].ToString());

            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);

            phrase2.Add(new Chunk("P-IMG1 :\u00a0", normalFont));
            Byte[] bytes6 = (Byte[])dr[14];
            iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6);
            image6.ScaleToFit(380f, 380f);
            Chunk imageChunk6 = new Chunk(image6, 0, 0);
            phrase2.Add(imageChunk6);

            phrase2.Add(new Chunk("P-IMG2 :\u00a0", normalFont));
            Byte[] bytes7 = (Byte[])dr[15];
            iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7);
            image7.ScaleToFit(380f, 380f);
            Chunk imageChunk7 = new Chunk(image7, 0, 0);
            phrase2.Add(imageChunk7);


            phrase2.Add(new Chunk("P-IMG3 :\u00a0", normalFont));
            Byte[] bytes8 = (Byte[])dr[16];
            iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8);
            image8.ScaleToFit(380f, 380f);
            Chunk imageChunk8 = new Chunk(image8, 0, 0);
            phrase2.Add(imageChunk8);

            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);

            phrase2.Add(new Chunk("P-IMG4 :\u00a0", normalFont));
            Byte[] bytes9 = (Byte[])dr[17];
            iTextSharp.text.Image image9 = iTextSharp.text.Image.GetInstance(bytes9);
            image9.ScaleToFit(710f, 710f);
            Chunk imageChunk9 = new Chunk(image9, 0, 0);
            phrase2.Add(imageChunk9);

            phrase2.Add(new Chunk("P-IMG5 :\u00a0", normalFont));
            Byte[] bytes10 = (Byte[])dr[18];
            iTextSharp.text.Image image10 = iTextSharp.text.Image.GetInstance(bytes10);
            image10.ScaleToFit(710f, 710f);
            Chunk imageChunk10 = new Chunk(image10, 0, 0);
            phrase2.Add(imageChunk10);

            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);

            phrase2.Add(new Chunk("Official Report :", normalFont));
            phrase2.Add(dr[19].ToString());

            table.AddCell(phrase);
            table2.AddCell(phrase2);

        }

        dr.Close();
        doc1.Add(table);
        doc1.NewPage();
        doc1.Add(table2);
        doc1.Close();
    }

来源:https://stackoverflow.com/questions/17310875/different-picture-sizes-in-pdf-despite-having-same-resolution-scaletofit-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!