简体   繁体   English

使用itextsharp在PDF中保持相似的图像大小

[英]Maintain similar image size in PDF using itextsharp

i'm using itextsharp to bring my picture over to the pdf file. 我正在使用itextsharp将我的图片带到pdf文件中。 I uses chunk. 我用chunk。 However when i uses the chunk new line, my image changes it size despite me putting the same scaletofit. 然而,当我使用chunk new line时,尽管我使用相同的scaletofit,但我的图像会改变它的大小。

在此输入图像描述

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, mr.citizenreport, pr.policeid,  pr.prdatetime, pr.policereport, aor.officialreport,  mr.image1, mr.image2, mr.image3, mr.image4, mr.image5,  pr.image1, pr.image2, pr.image3, pr.image4, pr.image5 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())
        {


            phrase2.Add(new Chunk("Citizen Images :", normalFont));

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

            phrase2.Add(new Chunk("1.", normalFont));
            Byte[] bytes1 = (Byte[])dr[10];
            iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(750f, 750f);
            Chunk imageChunk1 = new Chunk(image1, 0, -30);
            phrase2.Add(imageChunk1);

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


            phrase2.Add(new Chunk("2.", normalFont));
            Byte[] bytes5 = (Byte[])dr[11];
            iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(750f, 750f);
            Chunk imageChunk5 = new Chunk(image5, 0, -30);
            phrase2.Add(imageChunk5);

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

            phrase2.Add(new Chunk("3.", normalFont));
            Byte[] bytes6 = (Byte[])dr[12];
            iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6);
            image6.ScaleToFit(750f, 750f);
            Chunk imageChunk6 = new Chunk(image6, 0, -30);
            phrase2.Add(imageChunk6);

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


            phrase2.Add(new Chunk("4.", normalFont));
            Byte[] bytes7 = (Byte[])dr[13];
            iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7);
            image7.ScaleToFit(750f, 750f);
            Chunk imageChunk7 = new Chunk(image7, 0, -30);
            phrase2.Add(imageChunk7);

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

            phrase2.Add(new Chunk("5.", normalFont));
            Byte[] bytes8 = (Byte[])dr[14];
            iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8);
            image8.ScaleToFit(750f, 750f);
            Chunk imageChunk8 = new Chunk(image8, 0, -30);
            phrase2.Add(imageChunk8);

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


            table2.AddCell(phrase2);

        }

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


}

I completely forgot about this question, but I'll answer it now for further reference. 我完全忘记了这个问题,但我现在回答它以供进一步参考。 iTextSharp resizes images automatically (which is what you'd want in most cases), but you can avoid this by setting ScaleToFitHeight = false to each image. iTextSharp会自动调整图像大小(在大多数情况下这是您想要的),但您可以通过为每个图像设置ScaleToFitHeight = false来避免这种情况。

phrase2.Add(new Chunk("1.", normalFont));
Byte[] bytes1 = (Byte[])dr[10];
iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
image1.ScaleToFit(750f, 750f);
//set scale to fit height = false
image1.ScaleToFitHeight = false;
Chunk imageChunk1 = new Chunk(image1, 0, -30);
phrase2.Add(imageChunk1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM