使用iTextSharp的维护PDF类似的图片大小 [英] Maintain similar image size in PDF using itextsharp

查看:154
本文介绍了使用iTextSharp的维护PDF类似的图片大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的把我的照片到PDF文件。我使用的块。然而,当我使用了一块新的生产线,我的形象,尽管我把同样的scaletofit改变它的大小。

 保护无效btnPDF_Click(对象发件人,EventArgs的发送)
    {        VAR DOC1 =新的文件();
        变种文件名=MyTestPDF+ DateTime.Now.ToString(yyyyMMddHHmmssfff)+.PDF;
        无功输出=新的FileStream(Path.Combine(C:\\\\ \\\\用户\\\\ apr13mpsip下载,文件名),FileMode.Create);
        iTextSharp.text.pdf.PdfWriter.GetInstance(DOC1,输出);
        doc1.Open();
        PdfPTable表=新PdfPTable(1);        table.TotalWidth = 585f;
        table.LockedWidth = TRUE;
        PdfPTable表2 =新PdfPTable(1);
        table2.TotalWidth = 585f;
        table2.LockedWidth = TRUE;        iTextSharp.text.pdf.PdfPCell imgCell1 =新iTextSharp.text.pdf.PdfPCell();        VAR标志= iTextSharp.text.Image.GetInstance(使用Server.Mappath(〜/图像/ logo.jpg));
        doc1.Add(标识);        VAR titleFont = FontFactory.GetFont(宋体,15,Font.BOLD);
        doc1.Add(新段(官方报告会员报告编号:+ DDLCase.SelectedValue,titleFont));        变种normalFont = FontFactory.GetFont(FontFactory.HELVETICA,14,Font.BOLD);
        VAR短语=新词();
        VAR phrase2 =新词();        SqlConnection的CON =新的SqlConnection(数据源=本地主机;初始目录=项目;集成安全性= SSPI);        的SqlCommand厘米=新的SqlCommand(选择lro.fullname,lro.contact,mr.typeofcrime,mr.location,mr.crdatetime,mr.citizenreport,pr.policeid,pr.prdatetime,pr.policereport,aor.officialreport,先生。此搜索,mr.image2,mr.image3,mr.image4,mr.image5,pr.image1,pr.image2,pr.image3,pr.image4,pr.image5从MemberReport先生,PoliceReport PR,LoginRegisterOthers LRO,AdminOfficialReport AOR哪里mr.memberreportid ='+ DDLCase.SelectedValue +'和mr.memberreportid = pr.memberreportid和pr.policereportid = aor.policereportid,CON);
        con.Open();
        SqlDataReader的博士;        博士= cm.ExecuteReader();        如果(dr.Read())
        {
            phrase2.Add(新组块(公民图片:normalFont));            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);
            phrase2.Add(Chunk.NEWLINE);            phrase2.Add(新块(1。,normalFont));
            字节[] = bytes1(字节[])博士[10];
            iTextSharp.text.Image此搜索= iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(750F,750F);
            大块imageChunk1 =新的块(此搜索,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(新块(2,normalFont));
            字节[] = bytes5(字节[])博士[11];
            iTextSharp.text.Image图像5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(750F,750F);
            大块imageChunk5 =新的块(图像5,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(新块(3,normalFont));
            字节[] = bytes6(字节[])博士[12];
            iTextSharp.text.Image图片6 = iTextSharp.text.Image.GetInstance(bytes6);
            image6.ScaleToFit(750F,750F);
            大块imageChunk6 =新的块(图片6,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(新块(4,normalFont));
            字节[] = bytes7(字节[])博士[13];
            iTextSharp.text.Image图片7 = iTextSharp.text.Image.GetInstance(bytes7);
            image7.ScaleToFit(750F,750F);
            大块imageChunk7 =新的块(图片7,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(新组块(5,normalFont));
            字节[] = bytes8(字节[])博士[14];
            iTextSharp.text.Image图片8 = iTextSharp.text.Image.GetInstance(bytes8);
            image8.ScaleToFit(750F,750F);
            大块imageChunk8 =新的块(图片8,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(表);
        doc1.NewPage();
        doc1.Add(表2);
        doc1.Close();
    }
}


解决方案

我完全忘了这个问题,但现在我会回答它进一步参考。 iTextSharp的自动调整图像(这是你在大多数情况下想要什么),但你可以通过 ScaleToFitHeight = FALSE 设置为每个图像避免这种情况。

  phrase2.Add(新组块(1,normalFont));
字节[] = bytes1(字节[])博士[10];
iTextSharp.text.Image此搜索= iTextSharp.text.Image.GetInstance(bytes1);
image1.ScaleToFit(750F,750F);
//设置规模,以适应高度= FALSE
image1.ScaleToFitHeight = FALSE;
大块imageChunk1 =新的块(此搜索,0,-30);
phrase2.Add(imageChunk1);

i'm using itextsharp to bring my picture over to the pdf file. I uses chunk. However when i uses the chunk new line, my image changes it size despite me putting the same 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.

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

这篇关于使用iTextSharp的维护PDF类似的图片大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆