Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:25:31+00:00 2026-06-03T06:25:31+00:00

I am using iTextSharp to export Griddata to PDF. I am not able to

  • 0

I am using iTextSharp to export Griddata to PDF. I am not able to insert .jpg image to a cell of a table. I want to export griddata(billdata) to PDF I can insert text on a cell but not able to insert image.

My code is as below

protected void ToPDF(object sender, EventArgs e)
        {      
            System.IO.MemoryStream PDFData = new System.IO.MemoryStream();
            iTextSharp.text.Document newDocument = new iTextSharp.text.Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
            iTextSharp.text.pdf.PdfWriter newPdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(newDocument, PDFData);
            DataSet newDataSet = null;

            string json = GridData.Value.ToString();
            if (json != "[]")
            {
                StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
                XmlNode xml = eSubmit.Xml;

                if (xml != null)
                {
                    XmlTextReader xtr = new XmlTextReader(xml.OuterXml, XmlNodeType.Element, null);
                    newDataSet = new DataSet();
                    newDataSet.ReadXml(xtr);


                    int totalColumns = newDataSet.Tables[0].Columns.Count;
                    iTextSharp.text.pdf.PdfPTable newPdfTable = new iTextSharp.text.pdf.PdfPTable(totalColumns - 3+1);
                    newPdfTable.DefaultCell.Padding = 1;
                    newPdfTable.WidthPercentage = 80;
                    newPdfTable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                    newPdfTable.DefaultCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;    
                    newPdfTable.HeaderRows = 1; 
                    newPdfTable.DefaultCell.BorderColor = new iTextSharp.text.BaseColor(255, 255, 255);
                    newPdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255);

                    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("D:\\company_logo.jpg");
                    image.Alignment = iTextSharp.text.Image.ALIGN_LEFT;

                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image);
                    cell.Rowspan = 5;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                   cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Your company name", FontFactory.GetFont("Times New Roman", 18, Font.BOLD, new iTextSharp.text.BaseColor(80, 80, 80))));
                    cell.Colspan = 6;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine));
                    cell.Colspan = 6;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Street Address", FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                    cell.Colspan = 6;
                    cell.HorizontalAlignment = 0;
                    //cell.Border =1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("City, Pincode", FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                    cell.Colspan = 5; 
                    cell.HorizontalAlignment = 0;                   
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    string dt=Convert.ToString( Convert.ToDateTime(dtBill.Text).ToShortDateString());
                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Date : " + dt, FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                   //cell.Colspan = 2;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Phone no., Website, etc.", FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                    cell.Colspan = 5; 
                    cell.HorizontalAlignment = 0;
                    //cell.Border =1;      
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Bill no : " + cmbBill.Text, FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                    //cell.Colspan = 2;                   
                    cell.HorizontalAlignment = 0;                   
                    //cell.Border =1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine));
                    cell.Colspan = 7;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine));
                    cell.Colspan = 7;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine));
                    cell.Colspan = 7;
                    cell.HorizontalAlignment = 0;
                    //cell.Border = 1;
                    newPdfTable.AddCell(cell);

                    for (int i = 0; i <= totalColumns-1 ; i++)
                    {

                        if ((i != 0) & (i != 1) & (i != 2))
                        {
                            cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(newDataSet.Tables[0].Columns[i].ColumnName, FontFactory.GetFont("Tahoma", 12, Font.BOLD, new iTextSharp.text.BaseColor(80, 80, 80))));
                            cell.BorderColor = new iTextSharp.text.BaseColor(80, 80, 80);
                            cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.LightGray);
                            cell.VerticalAlignment =iTextSharp.text.Element.ALIGN_MIDDLE; 
                            if (i == 5)
                            {
                                cell.Colspan = 2;
                                cell.HorizontalAlignment = 1;       
                            }
                            else
                            {
                                cell.HorizontalAlignment = 1;
                            }
                            newPdfTable.AddCell(cell);
                        }

                    }
                    int j = 0;
                    foreach (DataRow record in newDataSet.Tables[0].Rows)
                    {
                        for (int i = 0; i <= totalColumns - 1; i++)
                        {

                            if ((i != 0) & (i != 1) & (i != 2))
                            {
                                if (i == 3)
                                {
                                    if(record[i].ToString()=="true")
                                        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Yes", FontFactory.GetFont("Tahoma", 10, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                                    else
                                        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("No", FontFactory.GetFont("Tahoma", 10, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                                }
                                else
                                {
                                        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(record[i].ToString(), FontFactory.GetFont("Tahoma", 10, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                                }

                                if ((j % 2) == 0)
                                {
                                    //cell.BorderColor = new iTextSharp.text.BaseColor(System.Drawing.Color);
                                }
                                else
                                {
                                    cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.WhiteSmoke);
                                }
                                cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                                if (i == 3)
                                    cell.Border = iTextSharp.text.pdf.PdfPCell.NO_BORDER;
                                else
                                    cell.Border = iTextSharp.text.pdf.PdfPCell.LEFT_BORDER;


                                if (i == 5)
                                {
                                    cell.Colspan = 2;
                                    cell.HorizontalAlignment = 0;
                                }
                                else
                                {
                                    if ((i == 6) | (i == 7) | (i == 8))
                                    {
                                        cell.HorizontalAlignment = 2;
                                    }

                                    else
                                    {
                                        cell.HorizontalAlignment = 0;
                                    }
                                }

                                newPdfTable.AddCell(cell);
                            }
                        }
                        j++;
                    }

                    newDocument.Open();
                    newDocument.Add(new Phrase(Environment.NewLine));
                    newDocument.Add(new Phrase(Environment.NewLine));
                    newDocument.Add(newPdfTable);  
                    //newDocument.Add(new Phrase("                                                                                                                                Total  " + txtTotalQty.Text + "      " + txtTotalAmount.Text, FontFactory.GetFont("Trebuchet MS", 14, Font.BOLD, new iTextSharp.text.BaseColor(21, 66, 157))));
                    newDocument.Add(new Phrase(Environment.NewLine));
                    //newDocument.Add(new Phrase("Printed On: " + DateTime.Now.ToString(), FontFactory.GetFont("Tahoma", 9, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80))));
                    newDocument.Close();
                    Response.ContentType = "application/pdf";
                    Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
                    Response.AppendHeader("Content-Type", "application/pdf");
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + this.ID + ".pdf");
                    Response.OutputStream.Write(PDFData.GetBuffer(), 0, PDFData.GetBuffer().Length);
                    Response.OutputStream.Flush();
                    Response.OutputStream.Close();
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:25:32+00:00Added an answer on June 3, 2026 at 6:25 am

    Just replace newPdfTable.HeaderRows = 1; to newPdfTable.HeaderRows = 0;

    and one more thing

    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image);
    

    and one more parameter as true

    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image,true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to export gridview to pdf by using the itextsharp library. The problem
I've been able (using itextsharp) to insert data into fields on a pdf and
I'm using iTextsharp library to create PDF files. I can declare for A4 Landscape
I am generating PDF using iTextSharp.dll, but the problem is that I am not
I'm using iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style) to convert an html table into a pdf
I am using iTextSharp to generate pdf in my .net application. Now I want
I have just looked at using iTextSharp 5.0, however things like table/cell have been
I am using iTextSharp to generate PDF out of HTML. I can save the
I am using iTextSharp to read text contents from PDF. I am able to
I am generating a pdf file in asp.net c# using itextsharp. i am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.