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);
}
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manish Kumar ChoudharyPosted Nov 26, 2014, 11:15 PM
Hi priya santhi,
Go through these links you might got your answer
http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/PDF-Footer-Add-PDF-Footer-in-C-VB.NET.html
http://www.codeproject.com/Tips/573907/Generating-PDF-using-ItextSharp-with-Footer-in-Csh
http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Add-header-in-PDF-with-C-VB.NET.html
santhosh kumarPosted Nov 26, 2014, 11:10 PM
Refer the Following Links..
http://www.aliencoders.com/content/create-pdf-file-using-itextsharp
http://geekswithblogs.net/JaydPage/archive/2011/11/02/using-itextsharp-to-correctly-display-hebrew--arabic-text-right.aspx