如何在PDF表格中添加单元格并将图片插入其中

本文关键字:插入 添加 PDF 表格 单元格 | 更新日期: 2023-09-27 18:20:24

我正在尝试创建一个包含10个图像的pdf。通过使用下面的方法,可以直接从数据库中检索所有这10幅图像。

这是我用来创建PdfPTable以将数据插入其中的代码。

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

这是我的数据库连接代码,我将SQL server数据库与我的项目链接在一起。

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

这就是我如何使用datareader读取SQL服务器中的数据,并检索将生成并显示在PDF中的相应图像和信息。

 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(new Chunk("Contact :", normalFont));
            phrase.Add(dr[1].ToString());
            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(new Chunk("Location :", normalFont));
            phrase.Add(dr[3].ToString());
            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(new Chunk("Citizen Report :", normalFont));
            phrase.Add(dr[5].ToString());
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(new Chunk("Citizen Images :", normalFont));
            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("1.", normalFont));
            Byte[] bytes1 = (Byte[])dr[10];
            iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(850f, 850f);
            image1.SetDpi(96,96);
            Chunk imageChunk1 = new Chunk(image1, 0, 0);
            phrase.Add(imageChunk1);
            phrase.Add(new Chunk("2.", normalFont));
            Byte[] bytes5 = (Byte[])dr[11];
            iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(850f, 850f);
            image5.SetDpi(96, 96);
            Chunk imageChunk5 = new Chunk(image5, 0, 0);
            phrase.Add(imageChunk5);
            phrase.Add(new Chunk("3.", normalFont));
            Byte[] bytes6 = (Byte[])dr[12];
            iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6);
            image6.ScaleToFit(850f, 850f);
            image6.SetDpi(96, 96);
            Chunk imageChunk6 = new Chunk(image6, 0, 0);
            phrase.Add(imageChunk6);
            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("4.", normalFont));
            Byte[] bytes7 = (Byte[])dr[13];
            iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7);
            image7.SetDpi(96,96);
            image7.ScaleToFit(2067f, 2064f);
            Chunk imageChunk7 = new Chunk(image7, 0, 0);
            phrase.Add(imageChunk7);
            phrase.Add(new Chunk("5.", normalFont));
            Byte[] bytes8 = (Byte[])dr[14];
            iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8);
            image8.ScaleToFit(2067f, 2064f);
            image8.SetDpi(96, 96);
            Chunk imageChunk8 = new Chunk(image8, 0, 0);
            phrase.Add(imageChunk8);
            //phrase2.Add(new Chunk("Police ID :", normalFont));
            //phrase2.Add(dr[6].ToString());
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(new Chunk("Police Report Date Time :", normalFont));
            //phrase2.Add(dr[7].ToString());
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(new Chunk("Police Report :", normalFont));
            //phrase2.Add(dr[8].ToString());
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(new Chunk("Official Report :", normalFont));
            //phrase2.Add(dr[9].ToString());
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(Chunk.NEWLINE);
            //phrase2.Add(new Chunk("Police Images :", normalFont));
            //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("1 :'u00a0", normalFont));
            //Byte[] bytes10 = (Byte[])dr[15];
            //iTextSharp.text.Image image10 = iTextSharp.text.Image.GetInstance(bytes10);
            //image10.ScaleToFit(850f, 850f);
            //image5.SetDpi(1024, 768);
            //Chunk imageChunk10 = new Chunk(image10, 0, 0);
            //phrase2.Add(imageChunk10);
            //phrase2.Add(new Chunk("2 :'u00a0", normalFont));
            //Byte[] bytes11 = (Byte[])dr[16];
            //iTextSharp.text.Image image11 = iTextSharp.text.Image.GetInstance(bytes11);
            //image11.ScaleToFit(850f, 850f);
            //image5.SetDpi(1024, 768);
            //Chunk imageChunk11 = new Chunk(image11, 0, 0);
            //phrase2.Add(imageChunk11);

            //phrase2.Add(new Chunk("3 :'u00a0", normalFont));
            //Byte[] bytes12 = (Byte[])dr[17];
            //iTextSharp.text.Image image12 = iTextSharp.text.Image.GetInstance(bytes12);
            //image12.ScaleToFit(850f, 850f);
            //image5.SetDpi(1024, 768);
            //Chunk imageChunk12 = new Chunk(image12, 0, 0);
            //phrase2.Add(imageChunk12);
            //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("4 :'u00a0", normalFont));
            //Byte[] bytes13 = (Byte[])dr[18];
            //iTextSharp.text.Image image13 = iTextSharp.text.Image.GetInstance(bytes13);
            //image13.ScaleToFit(850f, 850f);
            //image5.SetDpi(1024, 768);
            //Chunk imageChunk13 = new Chunk(image13, 0, 0);
            //phrase2.Add(imageChunk13);
            //phrase2.Add(new Chunk("5 :'u00a0", normalFont));
            //Byte[] bytes14 = (Byte[])dr[19];
            //iTextSharp.text.Image image14 = iTextSharp.text.Image.GetInstance(bytes14);
            //image14.ScaleToFit(850f, 850f);
            //image5.SetDpi(1024, 768);
            //Chunk imageChunk14 = new Chunk(image14, 0, 0);
            //phrase2.Add(imageChunk14);

            table.AddCell(phrase);
            //table2.AddCell(phrase2);
        }
        dr.Close();
        doc1.Add(table);
        doc1.NewPage();
        doc1.Add(table2);
        doc1.Close();
    }

}
}

我还有一个PdfPTable,它只有一列,我无法添加单元格来将特定的图像分配到特定的单元格中。

正如您所看到的,我直接从SQL数据库中检索图像,因此我相信将图像添加到相应的单元格中会更加困难。

以下是我发布的帖子的一些链接1和链接2,它们都有图片对齐问题。上面提供的链接都有显示大的对齐问题的图片。因此,我能想到的正确对齐图片的唯一方法是添加一个单元格,并将图像放入相应的单元格中。我希望照片并排排列,大小相同。

如何在PDF表格中添加单元格并将图片插入其中

我之前一直在做这样的事情。数据库中的图像字节数组进入图像处的GetInstance。

PdfPTable table = new PdfPTable(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagebytearray);
PdfPcell cell = new PdfPCell(image);
table.AddCell(cell);

对于图片大小:image。ScaleToFit()