Click here to Skip to main content
15,886,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am Generating PDF using ItextSmart on WEB APPLICATION,
Pdf is Generated from Database,

I want to Set Width of Columns Name & Value Like this,
Example to Generate PDF,

Sr No(Verry Small)| Description(Big) | Unit Rate(Medium) | Qty(Verry Small)| Price(Med)


What I have tried:

sb.Append("<tr>");
                for (int i = 0; i < dt2.Columns.Count; i++)
                {                    
                    string width = i == 1 ? "5" : "1"; // first column will be 50% and others 20%
                    sb.Append("<td colspan=" + width + " >");                   
                    sb.Append(dt2.Columns[i].ColumnName);
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
                foreach (DataRow row in dt2.Rows)
                {
                    sb.Append("<tr>");                   
                    for (int i = 0,j = 0; i < dt2.Columns.Count; i++,j++)
                    {
                        string width = i == 1 ? "5" : "1";                       
                        sb.Append("<td  colspan=" + width + ">");                        
                        sb.Append(row[dt2.Columns[i].ColumnName]);
                        sb.Append("</td>");
                    }
                    sb.Append("</tr>");
                }
Posted
Updated 2-Nov-17 20:20pm
Comments
Karthik_Mahalingam 3-Nov-17 1:26am    
why colspan, replace it with width
Anuj Mehta 3-Nov-17 1:32am    
I tried
property like width and rowspan is not working
Karthik_Mahalingam 3-Nov-17 1:41am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Anuj Mehta 3-Nov-17 1:46am    
yes Sure done..
Anuj Mehta 3-Nov-17 1:54am    
I want to Set Width of Columns Name & Value Like this,

COl-1(Verry Small)|COl-12(Big) | COl-3(Medium) | COl-4(Verry Small)| COl-5(Med)

itextsharp has its own class for tables, PdfPTable, having definitely an easy way for formatting columns and add rows/cells.

Have a look
 
Share this answer
 
try

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.IO;

namespace B13
{ 
    class Program
    { 
        static void Main(string[] args)
        { 
            Document document = new Document(PageSize.A4, 10, 10, 10, 10);
            var pdfPath = @"D:\Projects\CP\CP\bin\Debug\aa.pdf";
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.CreateNew));
            document.Open();

            DataTable dt = new DataTable();
            //Sr No(Verry Small)| Description(Big) | Unit Rate(Medium) | Qty(Verry Small)| Price(Med)
            dt.Columns.Add("Sr No");
            dt.Columns.Add("Description");
            dt.Columns.Add("Unit Rate");
            dt.Columns.Add("Qty");
            dt.Columns.Add("Price");
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            dt.Rows.Add(1, "asdfasdfsadfsadfsadfsadfas", 2323, 1, 344);
            PdfPTable table = new PdfPTable(dt.Columns.Count);
            float[] widths = new float[] { 20f, 60f, 20f, 30f, 30f };
            table.SetWidths(widths);
            foreach (DataColumn col in dt.Columns)
            { 
                table.AddCell(col.ColumnName);
            }        
                

            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                {
                    var value = row[col];
                    table.AddCell(value.ToString());
                }
            } 

            document.Add(table);
            document.Close();


        }
    }
}


refer iTextSharp - Introducing Tables[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900