User-854977000 posted
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;
}
}
how can I make the page landscape because actually I have tons of columns that I need to display on the page?