I do need a help on iTextSharp.. I am attaching the code...
I have header columns (col1,col2,col3) .. I need to change their font color and size, I need small size font.)
Please help!!!
protected void btnPrint_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
Phrase phrase = new iTextSharp.text.Phrase(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT", new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5));
doc.Open();
iTextSharp.text.Rectangle page = doc.PageSize;
PdfPTable head = new PdfPTable(1);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(phrase);
c.Border = iTextSharp.text.Rectangle.NO_BORDER;
c.VerticalAlignment = Element.ALIGN_TOP;
c.HorizontalAlignment = Element.ALIGN_CENTER;
//c.Rotation = 90;
head.AddCell(c);
head.WriteSelectedRows(
// first/last row; -1 writes all rows
0, -1,
// left offset
0,
// ** bottom** yPos of the table
page.Height - doc.TopMargin + head.TotalHeight + 20,
writer.DirectContent);
// add image to document
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Request.MapPath("~/images/logoPDF.jpg"));
logo.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
logo.ScalePercent(50f);
doc.Add(logo);
// report "title"
Paragraph p = new Paragraph("Test");
p.Alignment = 1;
doc.Add(p);
// add tabular data
doc.Add(my_table());
doc.Close();
}
private iTextSharp.text.Table my_table()
{
// the column headings
string[] col = { "col1","col2", "col3" };
iTextSharp.text.Table table = new iTextSharp.text.Table(11);
// set table style properties
table.BorderWidth = 1;
table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
table.Padding = 0;
table.Width = 100;
// set *column* widths
//float[] widths = { .1f, .5f, .4f };
//table.Widths = widths;
// create the *table* header row
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(col[i]);
cell.Header = true;
cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);
table.AddCell(cell);
}
table.EndHeaders();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString);
SqlDataReader reader = null;
try
{
conn.Open();
SqlCommand comm = new SqlCommand("test", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
comm.Parameters["@ID"].Value = Session["ID"].ToString().Trim();
reader = comm.ExecuteReader();
while (reader.Read())
{
table.AddCell(reader["col1"].ToString());
table.AddCell(reader["col2"].ToString());
table.AddCell(reader["col3"].ToString());
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
return table;
}
}
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(new Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)));
cell.Header = true;
cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);
table.AddCell(cell);
}
Hi i am using your above example code for show the output in pdf format. But i am not getting the proper result. Please tell me where i am doing mistake. This is my code below.
public void ShowTable()
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
Phrase phrase = new iTextSharp.text.Phrase(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT", new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5));
doc.Open();
iTextSharp.text.Rectangle page = doc.PageSize;
PdfPTable head = new PdfPTable(1);
head.TotalWidth = page.Width;
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(new Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)));
cell.Header = true;
cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);
table.AddCell(cell);
}
table.EndHeaders();
try
{
string connect = "server=192.168.0.10;uid=admin;pwd=admin123;database=updated_bcrs;Pooling=true;Min Pool Size=0;Max Pool Size=10000;Connection Lifetime=0;procedure bodies=false;";
MySqlConnection conn = new MySqlConnection(connect);
MySqlDataReader rdr=null ;
string query = "SELECT Type,Severity, JobName FROM bcms_alert";
MySqlCommand cmd = new MySqlCommand(query, conn);
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
table.AddCell(rdr["Type"].ToString());
table.AddCell(rdr["Severity"].ToString());
table.AddCell(rdr["JobName"].ToString());
}
}
wilsonnet
Member
22 Points
174 Posts
iTextSharp table headers
Jul 12, 2010 12:18 AM|LINK
I do need a help on iTextSharp.. I am attaching the code...
I have header columns (col1,col2,col3) .. I need to change their font color and size, I need small size font.)
Please help!!!
protected void btnPrint_Click(object sender, EventArgs e) { Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream); Phrase phrase = new iTextSharp.text.Phrase(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT", new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)); doc.Open(); iTextSharp.text.Rectangle page = doc.PageSize; PdfPTable head = new PdfPTable(1); head.TotalWidth = page.Width; PdfPCell c = new PdfPCell(phrase); c.Border = iTextSharp.text.Rectangle.NO_BORDER; c.VerticalAlignment = Element.ALIGN_TOP; c.HorizontalAlignment = Element.ALIGN_CENTER; //c.Rotation = 90; head.AddCell(c); head.WriteSelectedRows( // first/last row; -1 writes all rows 0, -1, // left offset 0, // ** bottom** yPos of the table page.Height - doc.TopMargin + head.TotalHeight + 20, writer.DirectContent); // add image to document iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Request.MapPath("~/images/logoPDF.jpg")); logo.Alignment = iTextSharp.text.Image.ALIGN_LEFT; logo.ScalePercent(50f); doc.Add(logo); // report "title" Paragraph p = new Paragraph("Test"); p.Alignment = 1; doc.Add(p); // add tabular data doc.Add(my_table()); doc.Close(); } private iTextSharp.text.Table my_table() { // the column headings string[] col = { "col1","col2", "col3" }; iTextSharp.text.Table table = new iTextSharp.text.Table(11); // set table style properties table.BorderWidth = 1; table.BorderColor = new iTextSharp.text.Color(0, 0, 255); table.Padding = 0; table.Width = 100; // set *column* widths //float[] widths = { .1f, .5f, .4f }; //table.Widths = widths; // create the *table* header row for (int i = 0; i < col.Length; ++i) { Cell cell = new Cell(col[i]); cell.Header = true; cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204); table.AddCell(cell); } table.EndHeaders(); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString); SqlDataReader reader = null; try { conn.Open(); SqlCommand comm = new SqlCommand("test", conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)); comm.Parameters["@ID"].Value = Session["ID"].ToString().Trim(); reader = comm.ExecuteReader(); while (reader.Read()) { table.AddCell(reader["col1"].ToString()); table.AddCell(reader["col2"].ToString()); table.AddCell(reader["col3"].ToString()); } } catch (Exception ex) { Response.Write(ex.Message); } return table; } }Mikesdotnett...
All-Star
154901 Points
19864 Posts
Moderator
MVP
Re: iTextSharp table headers
Jul 12, 2010 04:45 AM|LINK
Try this:
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(new Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)));
cell.Header = true;
cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);
table.AddCell(cell);
}
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wilsonnet
Member
22 Points
174 Posts
Re: iTextSharp table headers
Jul 12, 2010 07:08 AM|LINK
Thank you so much!! it works!!
Can I ask you some additional question?
Same code above, if I want to make some columns wider than other how can I do that?
Also, how can I make the page landscape because actually I have tons of columns that I need to display on the page?
BTW, I have read you articles about that and it was great just me that couldnt figure it out those stuff thats why I am asking here...!!!
Thanks again..
Mikesdotnett...
All-Star
154901 Points
19864 Posts
Moderator
MVP
Re: iTextSharp table headers
Jul 12, 2010 07:25 AM|LINK
Additional questions should be asked in a new thread, otherwise this thread will be in danger of becoming your personal tutorial.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wilsonnet
Member
22 Points
174 Posts
Re: iTextSharp table headers
Jul 12, 2010 07:32 AM|LINK
you are right!! I did asked on the question called."landscape" just now.. Thanks again.
wilsonnet
Member
22 Points
174 Posts
Re: iTextSharp table headers
Jul 12, 2010 10:50 AM|LINK
I added this PdfPCell cell = new PdfPCell(new iTextSharp.text.Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 8)));
and I have noticed that my cells are not actually that font above.. only header fonts is times roman 8.. isnt that should make the all the cells?
Galvin
Member
79 Points
102 Posts
Re: iTextSharp table headers
Sep 02, 2011 04:27 AM|LINK
Hi i am using your above example code for show the output in pdf format. But i am not getting the proper result. Please tell me where i am doing mistake. This is my code below.
public void ShowTable()
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
Phrase phrase = new iTextSharp.text.Phrase(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT", new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5));
doc.Open();
iTextSharp.text.Rectangle page = doc.PageSize;
PdfPTable head = new PdfPTable(1);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(phrase);
c.Border = iTextSharp.text.Rectangle.NO_BORDER;
c.VerticalAlignment = Element.ALIGN_TOP;
c.HorizontalAlignment = Element.ALIGN_CENTER;
//c.Rotation = 90;
head.AddCell(c);
head.WriteSelectedRows(
// first/last row; -1 writes all rows
0, -1,
// left offset
0,
// ** bottom** yPos of the table
page.Height - doc.TopMargin + head.TotalHeight + 20,
writer.DirectContent);
// add image to document
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Request.MapPath("~/images/paste.png"));
logo.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
logo.ScalePercent(50f);
doc.Add(logo);
// report "title"
Paragraph p = new Paragraph("PDF Report");
p.Alignment = 1;
doc.Add(p);
// add tabular data
doc.Add(my_table());
doc.Close();
}
private iTextSharp.text.Table my_table()
{
// the column headings
string[] col = { "Type", "Severity", "JobName" };
iTextSharp.text.Table table = new iTextSharp.text.Table(11);
// set table style properties
table.BorderWidth = 1;
table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
table.Padding = 0;
table.Width = 100;
// set *column* widths
//float[] widths = { .1f, .5f, .4f };
//table.Widths = widths;
// create the *table* header row
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(new Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)));
cell.Header = true;
cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);
table.AddCell(cell);
}
table.EndHeaders();
try
{
string connect = "server=192.168.0.10;uid=admin;pwd=admin123;database=updated_bcrs;Pooling=true;Min Pool Size=0;Max Pool Size=10000;Connection Lifetime=0;procedure bodies=false;";
MySqlConnection conn = new MySqlConnection(connect);
MySqlDataReader rdr=null ;
string query = "SELECT Type,Severity, JobName FROM bcms_alert";
MySqlCommand cmd = new MySqlCommand(query, conn);
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
table.AddCell(rdr["Type"].ToString());
table.AddCell(rdr["Severity"].ToString());
table.AddCell(rdr["JobName"].ToString());
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
return table;
}
}
Note: It give me some unwanted result. Please help me. Here i am very new.
oysterlee
Member
4 Points
5 Posts
Re: iTextSharp table headers
Nov 08, 2011 07:12 AM|LINK
Hi,
I got a questin.
i saw this article have the iTextSharp.text.Table
but why my visual studio show the table namspace is not exist?
anjankant
Member
10 Points
26 Posts
Re: iTextSharp table headers
Mar 27, 2013 08:27 AM|LINK
Below is code how to create header column in itextsharp PDF header
PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfTable.TotalWidth = page.Width;
PdfPCell PdfPCell = null;
foreach (DataColumn dc in dt.Columns)
{
phrase = new Phrase(dc.ColumnName, FontFactory.GetFont("Verdana", 8, iTextSharp.text.Font.BOLD));
c = new PdfPCell(phrase);
c.Border = 0;
c.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
c.BackgroundColor = new iTextSharp.text.Color(135, 206, 250);
//c.GrayFill = 0.7f;
PdfTable.AddCell(c);
}
www.technologycrowds.com