Document pdfDoc = new Document(PageSize.A4.Rotate(), 20, 20, 20, 20);
String newfile = "";
String newappfile = "";
try
{
newappfile = DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf";
newfile = HttpContext.Current.Server.MapPath("~/ExportPdfFile/" + newappfile);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(newfile, FileMode.Create));
//PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
//PdfWriter.GetInstance(pdfDoc, new FileStream(HttpContext.Current.Server.MapPath("~/ExportPdfFile/" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf"), FileMode.Create));
pdfDoc.Open();
//Chunk c = new Chunk("" + System.Web.HttpContext.Current.Session["CompanyName"] + "", FontFactory.GetFont("Verdana", 11));
//Paragraph p = new Paragraph();
//p.Alignment = Element.ALIGN_JUSTIFIED_ALL;
//p.Add(c);
//pdfDoc.Add(p);
//--- Add Logo of PDF ----
string Page = pdfDoc.PageNumber.ToString();
string imageFilePath = ""; //System.Web.HttpContext.Current.Server.MapPath("~/img/bluestar_logo.png");
//iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
//jpg.ScaleToFit(120f, 100f);
//Give space before image
//jpg.SpacingBefore = 0f;
//Give some space after the image
//jpg.SpacingAfter = 1f;
//jpg.Alignment = Element.HEADER;
//pdfDoc.Add(jpg);
iTextSharp.text.Font font8 = FontFactory.GetFont("ARIAL", 7);
//--- Add new Line ------------
Phrase phrase1 = new Phrase(Environment.NewLine);
pdfDoc.Add(phrase1);
//-------------------------------
DataTable dt = myDataTable;//Datatable Object
if (dt != null)
{
//---- Add Result of DataTable to PDF file With Header -----
PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
//relative col widths in proportions - 1/3 and 2/3
pdfTable.TotalWidth = 830f;
//fix the absolute width of the table
pdfTable.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
//float[] widths = new float[] { 2.5f, 2f, 2f, 2f, 2f, 2f, 2.5f, 3f, 3f, 3f, 2f, 2f, 2.5f, 3f, 3f, 2f, 2f , 2.5f, 2f, 2f };
//pdfTable.SetWidths(widths);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100; // percentage
pdfTable.DefaultCell.BorderWidth = 1;
pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
pdfTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
foreach (DataColumn column in dt.Columns)
{
//pdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#b4ceec"));
//pdfTable.AddCell(FormatHeaderPhrase(column.ColumnName));
if (column.ColumnName == "STONEID")
{
pdfTable.AddCell(new PdfPCell(new Phrase(column.ColumnName, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#003e7e"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D4E1F0")) });
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(column.ColumnName, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#003e7e"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D4E1F0")) });
}
}
pdfTable.HeaderRows = 1; // this is the end of the table header
pdfTable.DefaultCell.BorderWidth = 1;
//foreach (DataRow row in dt.Rows)
//{
// foreach (object cell in row.ItemArray)
// {
// //assume toString produces valid output
// pdfTable.AddCell(FormatPhrase(cell.ToString()));
// }
//}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (i % 2 == 0)
{
if (j == 7 || j == 8 || j == 9)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_RIGHT, BorderWidth = 1, Padding = 3 });
}
else
{
if (j == 1 || j == 2)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3 });
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3 });
}
}
}
else if (i % 2 != 0)
{
if (j == 7 || j == 8 || j == 9)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_RIGHT, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D3D3D3")) });
}
else
{
if (j == 1 || j == 2)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D3D3D3")) });
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D3D3D3")) });
}
}
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3 });
}
}
}
pdfDoc.Add(pdfTable);
}
pdfDoc.Close();
//String stReturnFileName = HttpContext.Current.Server.MapPath(Common.GetConfigValue("PdfRootFolder")) + Convert.ToString(HttpContext.Current.Session["party_id"]) + "\\" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf";
//HttpContext.Current.Response.ContentType = "application/pdf";
//HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
//System.Web.HttpContext.Current.Response.Write(pdfDoc);
//HttpContext.Current.Response.Flush();
////HttpContext.Current.Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
//HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.ContentType = "application/pdf";
//HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf");
////HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/ExportPdfFile/" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf"));
//byte[] byteArray = File.ReadAllBytes(newfile);
//HttpContext.Current.Response.BinaryWrite(byteArray);
}
catch (DocumentException de)
{
//System.Web.HttpContext.Current.Response.Write(de.Message);
}
catch (IOException ioEx)
{
//System.Web.HttpContext.Current.Response.Write(ioEx.Message);
}
catch (Exception ex)
{
//System.Web.HttpContext.Current.Response.Write(ex.Message);
}
return newappfile; //Pdf File Path
String newfile = "";
String newappfile = "";
try
{
newappfile = DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf";
newfile = HttpContext.Current.Server.MapPath("~/ExportPdfFile/" + newappfile);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(newfile, FileMode.Create));
//PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
//PdfWriter.GetInstance(pdfDoc, new FileStream(HttpContext.Current.Server.MapPath("~/ExportPdfFile/" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf"), FileMode.Create));
pdfDoc.Open();
//Chunk c = new Chunk("" + System.Web.HttpContext.Current.Session["CompanyName"] + "", FontFactory.GetFont("Verdana", 11));
//Paragraph p = new Paragraph();
//p.Alignment = Element.ALIGN_JUSTIFIED_ALL;
//p.Add(c);
//pdfDoc.Add(p);
//--- Add Logo of PDF ----
string Page = pdfDoc.PageNumber.ToString();
string imageFilePath = ""; //System.Web.HttpContext.Current.Server.MapPath("~/img/bluestar_logo.png");
//iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
//jpg.ScaleToFit(120f, 100f);
//Give space before image
//jpg.SpacingBefore = 0f;
//Give some space after the image
//jpg.SpacingAfter = 1f;
//jpg.Alignment = Element.HEADER;
//pdfDoc.Add(jpg);
iTextSharp.text.Font font8 = FontFactory.GetFont("ARIAL", 7);
//--- Add new Line ------------
Phrase phrase1 = new Phrase(Environment.NewLine);
pdfDoc.Add(phrase1);
//-------------------------------
DataTable dt = myDataTable;//Datatable Object
if (dt != null)
{
//---- Add Result of DataTable to PDF file With Header -----
PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
//relative col widths in proportions - 1/3 and 2/3
pdfTable.TotalWidth = 830f;
//fix the absolute width of the table
pdfTable.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
//float[] widths = new float[] { 2.5f, 2f, 2f, 2f, 2f, 2f, 2.5f, 3f, 3f, 3f, 2f, 2f, 2.5f, 3f, 3f, 2f, 2f , 2.5f, 2f, 2f };
//pdfTable.SetWidths(widths);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100; // percentage
pdfTable.DefaultCell.BorderWidth = 1;
pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
pdfTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
foreach (DataColumn column in dt.Columns)
{
//pdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#b4ceec"));
//pdfTable.AddCell(FormatHeaderPhrase(column.ColumnName));
if (column.ColumnName == "STONEID")
{
pdfTable.AddCell(new PdfPCell(new Phrase(column.ColumnName, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#003e7e"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D4E1F0")) });
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(column.ColumnName, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#003e7e"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D4E1F0")) });
}
}
pdfTable.HeaderRows = 1; // this is the end of the table header
pdfTable.DefaultCell.BorderWidth = 1;
//foreach (DataRow row in dt.Rows)
//{
// foreach (object cell in row.ItemArray)
// {
// //assume toString produces valid output
// pdfTable.AddCell(FormatPhrase(cell.ToString()));
// }
//}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (i % 2 == 0)
{
if (j == 7 || j == 8 || j == 9)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_RIGHT, BorderWidth = 1, Padding = 3 });
}
else
{
if (j == 1 || j == 2)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3 });
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3 });
}
}
}
else if (i % 2 != 0)
{
if (j == 7 || j == 8 || j == 9)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_RIGHT, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D3D3D3")) });
}
else
{
if (j == 1 || j == 2)
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D3D3D3")) });
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3, BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#D3D3D3")) });
}
}
}
else
{
pdfTable.AddCell(new PdfPCell(new Phrase(dt.Rows[i][j].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8, new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#000000"))))) { HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1, Padding = 3 });
}
}
}
pdfDoc.Add(pdfTable);
}
pdfDoc.Close();
//String stReturnFileName = HttpContext.Current.Server.MapPath(Common.GetConfigValue("PdfRootFolder")) + Convert.ToString(HttpContext.Current.Session["party_id"]) + "\\" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf";
//HttpContext.Current.Response.ContentType = "application/pdf";
//HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
//System.Web.HttpContext.Current.Response.Write(pdfDoc);
//HttpContext.Current.Response.Flush();
////HttpContext.Current.Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
//HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.ContentType = "application/pdf";
//HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf");
////HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/ExportPdfFile/" + DateTime.Now.ToString("MMddyyyy") + "_" + DateTime.Now.ToString("hhmmss") + ".pdf"));
//byte[] byteArray = File.ReadAllBytes(newfile);
//HttpContext.Current.Response.BinaryWrite(byteArray);
}
catch (DocumentException de)
{
//System.Web.HttpContext.Current.Response.Write(de.Message);
}
catch (IOException ioEx)
{
//System.Web.HttpContext.Current.Response.Write(ioEx.Message);
}
catch (Exception ex)
{
//System.Web.HttpContext.Current.Response.Write(ex.Message);
}
return newappfile; //Pdf File Path
0 Comments