We help IT Professionals succeed at work.

create table

1jaws
1jaws asked
on
311 Views
Last Modified: 2012-05-09
How to create to table using iTextSharp and fill fields using data from stored proc. I cant see the table..
I used this http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

Thanks..
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);       

        doc.Open();
        PdfPTable table = new PdfPTable(3);

        PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));

        cell.Colspan = 3;

        cell.HorizontalAlignment = 1;  
        table.AddCell(cell);

        table.AddCell("Col 1 Row 1");

        table.AddCell("Col 2 Row 1");

        table.AddCell("Col 3 Row 1");

        table.AddCell("Col 1 Row 2");

        table.AddCell("Col 2 Row 2");

        table.AddCell("Col 3 Row 2");

        doc.Add(table);

        
        table.TotalWidth = 216f;

        
        table.LockedWidth = true;



        
        float[] widths = new float[] { 1f, 2f };

        

        table.HorizontalAlignment = 0;

       

        table.SpacingBefore = 20f;

        table.SpacingAfter = 30f;

        cell.Colspan = 2;

        cell.Border = 0;

        cell.HorizontalAlignment = 1;

        table.AddCell(cell);

        
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString);
        SqlDataReader reader = null;

       
            try
            {
            conn.Open();

            SqlCommand comm = new SqlCommand("selectPDF conn);
            comm.CommandType = CommandType.StoredProcedure;

           
            comm.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
            comm.Parameters["@ID"].Value = Convert.ToInt32(lblID.Text);

            reader = comm.ExecuteReader();

            while (reader.Read())
            {
                table.AddCell(reader[0].ToString());

                table.AddCell(reader[1].ToString());

            }

            }

            catch (Exception ex)
            {

                Response.Write(ex.Message);

            }

            doc.Add(table);
            doc.Close();
        }

Open in new window

Comment
Watch Question

Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Work fine for me :

(code behind)
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MemoryStream m = new MemoryStream();
        Document document = new Document();
        try
        {
            Response.ContentType = "application/pdf";
            PdfWriter writer = PdfWriter.GetInstance(document, m);
            writer.CloseStream = false;

            document.Open();

            PdfPTable table = new PdfPTable(3);
            PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
            cell.Colspan = 3;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 2 Row 1");
            table.AddCell("Col 3 Row 1");
            table.AddCell("Col 1 Row 2");
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");

            document.Add(table);
            table.TotalWidth = 216f;
            table.LockedWidth = true;
            float[] widths = new float[] { 1f, 2f };
            table.HorizontalAlignment = 0;
            table.SpacingBefore = 20f;
            table.SpacingAfter = 30f;
            cell.Colspan = 2;
            cell.Border = 0;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

        }
        catch (DocumentException ex)
        {
            Console.Error.WriteLine(ex.StackTrace);
            Console.Error.WriteLine(ex.Message);
        }
        document.Close();

        Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();
        m.Close();

        
    }
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

}

Open in new window

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
the "page" :


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

Open in new window

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
requirements changed for this question so closing this.. thanks for the effort!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.