hii...
i am very tried to create header and footer for pdf using c#...here am using itextsharp for converting gridview(details) to pdf file...plz anyone help me..urgent
private void button1_Click(object sender, EventArgs e)
{
PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 30;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
//try
//{
// PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
// cell.BackgroundColor = new BaseColor(System.Drawing.Color.Red);
// cell.BackgroundColor = BaseColor.RED;
//}
//catch (NullReferenceException ex)
//{
// MessageBox.Show(ex.Message);
//}
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
try
{
if (cell != null)
{
pdfTable.AddCell(cell.Value.ToString());
}
}
catch (NullReferenceException ex)
{
// MessageBox.Show(ex.Message);
}
}
}
//Exporting to PDF
string folderPath = "Resources/pdf/";
if (!Directory.Exists(folderPath))
{
try
{
Directory.CreateDirectory(folderPath);
}
catch (NullReferenceException ex)
{
MessageBox.Show(ex.Message);
}
}
using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
{
try
{
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}