protected void Download_Click(object sender, EventArgs e)
{
// Check condition
if (!GridView1.Columns[GridView1.Columns.Count - 1].Visible)
{
// Create PDF Document
String Path = Server.MapPath("~\\Bangdiem\\DKHP\\DKHP_" + userName + ".pdf");
Document myDocument = new Document(PageSize.A4, 5, 5, 30, 10);
if (!File.Exists(Path))
{
PdfWriter.GetInstance(myDocument, new FileStream(Path, FileMode.CreateNew));
// Open document
myDocument.Open();
BaseFont bf = BaseFont.CreateFont(Server.MapPath(@"~\Font\TIMES.TTF"), BaseFont.IDENTITY_H, true);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 12);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/UIT.png"));
image.Alignment = iTextSharp.text.Image.UNDERLYING;
image.ScaleToFit(30f, 30f);
Chunk c1 = new Chunk("TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN", font);
c1.SetUnderline(0.5f, -4f);
Paragraph Header = new Paragraph(15);
Header.IndentationLeft = 15;
Header.Alignment = 3;
Header.Font = font;
Header.Add(image);
Header.SpacingBefore = 5f;
Header.Add(" ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH \n ");
Header.Add(c1);
Header.Add("\n\n\n");
myDocument.Add(Header);
// Add gridview to
iTextSharp.text.Table table = new iTextSharp.text.Table(5);
// set table style properties
table.BorderWidth = 1;
table.BorderColor = Color.DARK_GRAY;
table.Padding = 4;
table.Alignment = 1;
table.Width = 90;
// set *column* widths
float[] widths = { 0.05f, 0.23f, 0.17f, 0.45f, 0.1f };
table.Widths = widths;
string[] col = { "TT", "Mã Lớp", "Mã Môn", "Tên Môn Học", "Số TC" };
font = new iTextSharp.text.Font(bf, 13, 1);
// create the *table* header row
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(new Phrase(col[i], font));
cell.Header = true;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
}
table.EndHeaders();
int sum = 0;
font = new iTextSharp.text.Font(bf, 12);
int order = 0;
foreach (GridViewRow row in GridView1.Rows)
{
Cell c = new Cell(new Phrase((++order).ToString(), font));
c.HorizontalAlignment = 1;
table.AddCell(c);
c = new Cell(new Phrase(row.Cells[1].Text, font));
c.HorizontalAlignment = 1;
table.AddCell(c);
c = new Cell(new Phrase(((HiddenField)row.FindControl("SubID")).Value, font));
c.HorizontalAlignment = 1;
table.AddCell(c);
c = new Cell(new Phrase(" " + ((LinkButton)row.FindControl("SubNm")).Text, font));
table.AddCell(c);
c = new Cell(new Phrase(row.Cells[3].Text, font));
c.HorizontalAlignment = 1;
try { sum += Int16.Parse(row.Cells[3].Text); }
catch (Exception ex) { }
table.AddCell(c);
}
font = new iTextSharp.text.Font(bf, 14);
Paragraph p = new Paragraph("ĐĂNG KÍ HỌC PHẦN HK " + getTerm() + " " + getYear() + " \n", font);
p.Alignment = 1;
p.Add("MSSV : " + userName);
myDocument.Add(p);
font = new iTextSharp.text.Font(bf, 12);
c1 = new Chunk("\n\nHọ Tên : ", font);
font = new iTextSharp.text.Font(bf, 12, 2);
Chunk c2 = new Chunk(((Label)StudentData.Items[0].FindControl("StuNmLB")).Text, font);
font = new iTextSharp.text.Font(bf, 12);
Chunk c3 = new Chunk(" Khoa : ", font);
font = new iTextSharp.text.Font(bf, 12, 2);
Chunk c4 = new Chunk(((Label)StudentData.Items[0].FindControl("DeptLB")).Text, font);
Paragraph p2 = new Paragraph();
p2.IndentationLeft = 30f;
p2.Alignment = 3;
p2.Add(c1);
p2.Add(c2);
p2.Add(c3);
p2.Add(c4);
myDocument.Add(p2);
// Add Gridview
myDocument.Add(table);
if (sum > 25)
{
myDocument.Close();
if (File.Exists(Path))
try { File.Delete(Path); }
catch (Exception ex)
{
}
return;
}
font = new iTextSharp.text.Font(bf, 12);
p = new Paragraph(String.Format("Tổng số TC : {0} ", sum.ToString()), font);
p.Alignment = 2;
myDocument.Add(p);
// Add sign
font = new iTextSharp.text.Font(bf, 13);
p = new Paragraph("\n\n\n Chữ ký SV", font);
p.Add(" Chữ ký PĐT\n\n");
p.Add(" .......................");
p.Add(" ........................\n");
myDocument.Add(p);
// Check
List<string> DateL = new List<string>();
List<string> Derror = new List<string>();
String error = "";
bool first = true;
foreach (GridViewRow grow in GridView1.Rows)
{
string Date = ((Label)grow.FindControl("Day")).Text;
string Period = ((Label)grow.FindControl("Period")).Text;
string[] dates = Date.Replace("<br/>", ",").Split(',');
string[] periods = Period.Replace("<br/>", ",").Split(',');
for (int i = 0; i < dates.Length; i++)
{
string dateandperiod = dates[i] + periods[i];
if (DateL.Contains(dateandperiod)) // Error on samq datetime
{
if (!Derror.Contains(dateandperiod))
{
if (first)
{
error += "Thứ " + dates[i] + " ca " + periods[i];
first = false;
}
else error += ", Thứ " + dates[i] + " ca " + periods[i];
Derror.Add(dateandperiod);
}
}
else DateL.Add(dateandperiod);
}
}
if (error != "")
{
font = new iTextSharp.text.Font(bf, 12);
p = new Paragraph("\n\n Ghi chú : trùng giờ học ", font);
p.Add("\n (" + error + ")");
myDocument.Add(p);
}
font = new iTextSharp.text.Font(bf, 11);
p = new Paragraph("\n In vào :" + DateTime.UtcNow.ToShortTimeString() +
" " + DateTime.UtcNow.ToShortDateString(), font);
p.Add("\n Chú ý : Sinh viên \n không được tự ý thay đổi nội dung file này.");
myDocument.Add(p);
// Close document
myDocument.Close();
// Check connection and trangfer file
using (SqlConnection scon = new SqlConnection(ConnectionString))
{
scon.Open();
using (SqlCommand scom = new SqlCommand("Insert into DownloadLog values(@StuID,getdate())", scon))
{
scom.Parameters.Add("@StuID", userName);
try
{
scom.ExecuteNonQuery();
}
catch (Exception ex)
{
using (SqlCommand scom1 = new SqlCommand("Update Downloadlog set log = getdate() where StuID = @StuID", scon))
{
try
{
scom1.Parameters.Add("@StuID", userName);
scom1.ExecuteNonQuery();
}
catch (Exception ex1) { }
}
}
}
}
}
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader
("Content-Disposition", "attachment; filename = DKHP_" + userName + ".pdf");
Response.TransmitFile(Path);
Response.End();
Response.Flush();
Response.Clear();
}
}