iTextSharp - Add image to PDF from Datatable

戏子无情 提交于 2019-11-30 23:01:34

What exactly is the problem? What happens when you use your problem code?

Try one of the other Image.GetInstance() overloads:

You can pass the byte array directly:

byte[] byt = (byte[])r[6];
Image img = Image.GetInstance(byt);

Or you can pass the Stream:

byte[] byt = (byte[])r[6];
MemoryStream ms = new MemoryStream(byt);
Image img = Image.GetInstance(ms);

Or you can give iTextSharp more info about the image format:

byte[] byt = (byte[])r[6];
MemoryStream ms = new MemoryStream(byt);
System.Drawing.Image sdi = System.Drawing.Image.FromStream(ms);
Image img = Image.GetInstance(sdi, ImageFormat.Png);

If your column can be cast to a System.Drawing.Image, then you can use it directly:

Image img = Image.GetInstance((System.Drawing.Image)r[6], System.Drawing.Imaging.ImageFormat.Png);
Anjan Kant

I have suggested steps how shows how to add image into PDF, given below code snippet show how to add logo into your PDF using iTextsharp, follow provided below steps:

  1. I have provided link to download "itextsharp" component from given link http://sourceforge.net/projects/itextsharp/
  2. You have to add reference into your application.
  3. Next you have to add required namespaces "iTextsharp.text.html", "iTextsharp.text" to consume its best properties.
  4. Now you have to add code snippet into your application given at the end, add code snippet under "button click" in code behind.

Hope it will work for you !!!

protected void btnPDF_Click(object sender, ImageClickEventArgs e)
    {
        DataTable dtn = new DataTable();
        dtn = GetDataTable();
        dtPDF = dtn.Copy();
        for (int i = 0; i <= dtn.Rows.Count - 1; i++)
        {
            ExportToPdf(dtPDF);
        }
     }

public void ExportToPdf(DataTable myDataTable)
    {
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
        try
        {
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            Chunk c = new Chunk("" + System.Web.HttpContext.Current.Session["CompanyName"] + "", FontFactory.GetFont("Verdana", 11));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            pdfDoc.Add(p);
            string clientLogo = Server.MapPath(".") + "/logo/tpglogo.jpg";
            string imageFilePath = Server.MapPath(".") + "/logo/tpglogo.jpg";
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
            //Resize image depend upon your need  
            jpg.ScaleToFit(80f, 60f);
            //Give space before image  
            jpg.SpacingBefore = 0f;
            //Give some space after the image  
            jpg.SpacingAfter = 1f;
            jpg.Alignment = Element.HEADER;
            pdfDoc.Add(jpg);
            Font font8 = FontFactory.GetFont("ARIAL", 7);
            DataTable dt = myDataTable;
            if (dt != null)
            {
                //Craete instance of the pdf table and set the number of column in that table 
                PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                PdfPCell PdfPCell = null;
                for (int rows = 0; rows < dt.Rows.Count; rows++)
                {
                    for (int column = 0; column < dt.Columns.Count; column++)
                    {
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                        PdfTable.AddCell(PdfPCell);
                    }
                }
                //PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table           
                pdfDoc.Add(PdfTable); // add pdf table to the document  
            }
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
            System.Web.HttpContext.Current.Response.Write(pdfDoc);
            Response.Flush();
            Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest(); 
        }
        catch (DocumentException de)
        {
            System.Web.HttpContext.Current.Response.Write(de.Message);
        }
        catch (IOException ioEx)
        {
            System.Web.HttpContext.Current.Response.Write(ioEx.Message);
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write(ex.Message);
        }
    }    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!