尽管具有相同的分辨率,但PDF中的图像尺寸不同。 scaletofit,ASP.net iTextSharp

时间:2013-06-26 03:06:51

标签: c# asp.net pdf itext size

我正在开发一个webapp,假设生成一个包含10张图片的PDF文件。不幸的是,图片尺寸存在问题。尽管所有10张照片具有相同的分辨率,但PDF中的图像尺寸也不同。我已通过此方法以编程方式为所有10张图片调整图片大小

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

以下是生成的PDF文件的2个屏幕截图。我以某种方式设法在两个屏幕截图中对齐所有C-IMG。

enter image description here enter image description here

然而,对于P-IMG,第一个截图工作正常。但是,如果我要在第二页中添加更多文本,则图片将无法正确对齐

如何确保这些图片正确对齐? 这是我的整个代码。我一直试图解决这个问题半天:(

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

0 个答案:

没有答案