Step 1: First of all we have to download the iTextSharp pdf library.
Step2 : After downloading file add the reference of iTextSharp.dll to your project by right clicking on solution explorer.
Step 3 : Now add the Namespace to your .cs or .vb file
using iTextSharp.text;
using iTextSharp.text.pdf;
Step 4 : Now one whatever click you want to generate the file you can write the below code. in this example i am generating the pdf file on button click event.
Now we will create document
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
if (txtfromdate.Text != "" && txtTodate.Text != "")
{
string dat = (DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
Document document1 = new Document(PageSize.A4);
PdfWriter.GetInstance(document1, new FileStream("D:\\Sankou\\Reports\\Stock\\" + dat + "-StockReport.pdf", FileMode.Create));
document1.Open();
Paragraph para1 = new Paragraph();
para1.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Font _font24normal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _fontsize24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontunder = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontitalic = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLDITALIC, BaseColor.BLACK);
Paragraph paragraph2 = new Paragraph();
paragraph2.Alignment = Element.ALIGN_RIGHT;
paragraph2.Add("Date" + DateTime.Now.ToShortDateString() + "\n\n");
document1.Add(paragraph2);
Phrase ps = new Phrase("S.K Enterprise" + "\n", _fontunder);
Phrase ps1 = new Phrase("Stock Report" + "\n", _fontunder);
Phrase p1 = new Phrase("TS.NO.9832,SIVAPURAM" + "\n");//, _fontunder);
Phrase p4 = new Phrase("2ND STREET,CHENNAI" + "\n");//, _fontunder);
Phrase p2 = new Phrase("04322-34343232,237657941" + "\n");//, _fontunder);
Phrase p5 = new Phrase("TIN-3353423406" + "\n");//, _fontunder);
Phrase p6 = new Phrase("E-Mail:skpdkt@gmail.com" + "\n");//, _fontunder);
para1.Add(ps);
para1.Add(ps1);
para1.Add(p1);
para1.Add(p4);
para1.Add(p2);
para1.Add(p5);
para1.Add(p6);
document1.Add(para1);
Paragraph paragraph4 = new Paragraph();
paragraph4.Alignment = Element.ALIGN_CENTER;
paragraph4.Font.IsBold();
paragraph4.Add("Report on (" + txtfromdate.Text + ")-(" + txtTodate.Text + ")\n\n");
document1.Add(paragraph4);
//PdfPTable table1 = new PdfPTable(4);
//table1.WidthPercentage = 80;
var table1 = new PdfPTable(4) { WidthPercentage = 100}; var colWidthPercentages = new[] { 60f, 10f, 20f, 20f }; table1.SetWidths(colWidthPercentages);
table1.AddCell(dataGridView1.Columns[0].HeaderText);
table1.AddCell(dataGridView1.Columns[1].HeaderText);
table1.AddCell(dataGridView1.Columns[2].HeaderText);
table1.AddCell(dataGridView1.Columns[3].HeaderText);
for (int x1 = 0; x1 < dataGridView1.Rows.Count - 1; x1++)
{
table1.AddCell(dataGridView1.Rows[x1].Cells[0].Value.ToString());
table1.AddCell(dataGridView1.Rows[x1].Cells[1].Value.ToString());
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[2].Value.ToString()).ToString("N", CInfo));
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[3].Value.ToString()).ToString("N", CInfo));
}
document1.Add(table1);
Paragraph paragraph5 = new Paragraph();
paragraph5.Alignment = Element.ALIGN_CENTER;
paragraph5.Font.IsBold();
paragraph5.Add("\n");
paragraph5.Add("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTotal Amount: " + label5.Text + "\n");
document1.Add(paragraph5);
document1.Close();
System.Diagnostics.Process.Start("D:\\Sankou\\Reports\\Stock\\" + dat + "-StockReport.pdf");
}
else
{
string dat = (DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
Document document1 = new Document(PageSize.A4);
PdfWriter.GetInstance(document1, new FileStream("D:\\Sankou\\Reports\\Stock\\" + dat + "-StockReport.pdf", FileMode.Create));
document1.Open();
Paragraph para1 = new Paragraph();
para1.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Font _font24normal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _fontsize24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontunder = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontitalic = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLDITALIC, BaseColor.BLACK);
Paragraph paragraph2 = new Paragraph();
paragraph2.Alignment = Element.ALIGN_RIGHT;
paragraph2.Add("Date" + DateTime.Now.ToShortDateString() + "\n\n");
document1.Add(paragraph2);
Phrase ps = new Phrase("S.K Enterprise" + "\n", _fontunder);
Phrase p1 = new Phrase("TS.NO.9932,MARTHANDAPURAM" + "\n");//, _fontunder);
Phrase p4 = new Phrase("2ND STREET,PUDUKKOTTAI" + "\n");//, _fontunder);
Phrase p2 = new Phrase("04322-233556,220556" + "\n");//, _fontunder);
Phrase p5 = new Phrase("TIN-33574105206" + "\n");//, _fontunder);
Phrase p6 = new Phrase("E-Mail:svsvijayatyrespdkt@gmail.com" + "\n");//, _fontunder);
para1.Add(ps);
para1.Add(p1);
para1.Add(p4);
para1.Add(p2);
para1.Add(p5);
para1.Add(p6);
document1.Add(para1);
Paragraph paragraph4 = new Paragraph();
paragraph4.Alignment = Element.ALIGN_CENTER;
paragraph4.Font.IsBold();
paragraph4.Add("Stock Report on " + dat + "\n\n");
document1.Add(paragraph4);
var table1 = new PdfPTable(4) { WidthPercentage = 100 }; var colWidthPercentages = new[] { 60f, 10f, 20f, 20f }; table1.SetWidths(colWidthPercentages);
table1.AddCell(dataGridView1.Columns[0].HeaderText);
table1.AddCell(dataGridView1.Columns[1].HeaderText);
table1.AddCell(dataGridView1.Columns[2].HeaderText);
table1.AddCell(dataGridView1.Columns[3].HeaderText);
for (int x1 = 0; x1 < dataGridView1.Rows.Count - 1; x1++)
{
table1.AddCell(dataGridView1.Rows[x1].Cells[0].Value.ToString());
table1.AddCell(dataGridView1.Rows[x1].Cells[1].Value.ToString());
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[2].Value.ToString()).ToString("N", CInfo));
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[3].Value.ToString()).ToString("N", CInfo));
}
document1.Add(table1);
Paragraph paragraph5 = new Paragraph();
paragraph5.Alignment = Element.ALIGN_CENTER;
paragraph5.Font.IsBold();
paragraph5.Add("\n");
paragraph5.Add("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTotal Amount Rs: " + label5.Text + "\n");
document1.Add(paragraph5);
document1.Close();
System.Diagnostics.Process.Start("D:Reports\\Stock\\" + dat + "-StockReport.pdf");
}
}
catch (Exception)
{
}
finally
{
}
Sample PDF As Follows:
Step2 : After downloading file add the reference of iTextSharp.dll to your project by right clicking on solution explorer.
Step 3 : Now add the Namespace to your .cs or .vb file
using iTextSharp.text;
using iTextSharp.text.pdf;
Step 4 : Now one whatever click you want to generate the file you can write the below code. in this example i am generating the pdf file on button click event.
Now we will create document
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
if (txtfromdate.Text != "" && txtTodate.Text != "")
{
string dat = (DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
Document document1 = new Document(PageSize.A4);
PdfWriter.GetInstance(document1, new FileStream("D:\\Sankou\\Reports\\Stock\\" + dat + "-StockReport.pdf", FileMode.Create));
document1.Open();
Paragraph para1 = new Paragraph();
para1.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Font _font24normal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _fontsize24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontunder = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontitalic = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLDITALIC, BaseColor.BLACK);
Paragraph paragraph2 = new Paragraph();
paragraph2.Alignment = Element.ALIGN_RIGHT;
paragraph2.Add("Date" + DateTime.Now.ToShortDateString() + "\n\n");
document1.Add(paragraph2);
Phrase ps = new Phrase("S.K Enterprise" + "\n", _fontunder);
Phrase ps1 = new Phrase("Stock Report" + "\n", _fontunder);
Phrase p1 = new Phrase("TS.NO.9832,SIVAPURAM" + "\n");//, _fontunder);
Phrase p4 = new Phrase("2ND STREET,CHENNAI" + "\n");//, _fontunder);
Phrase p2 = new Phrase("04322-34343232,237657941" + "\n");//, _fontunder);
Phrase p5 = new Phrase("TIN-3353423406" + "\n");//, _fontunder);
Phrase p6 = new Phrase("E-Mail:skpdkt@gmail.com" + "\n");//, _fontunder);
para1.Add(ps);
para1.Add(ps1);
para1.Add(p1);
para1.Add(p4);
para1.Add(p2);
para1.Add(p5);
para1.Add(p6);
document1.Add(para1);
Paragraph paragraph4 = new Paragraph();
paragraph4.Alignment = Element.ALIGN_CENTER;
paragraph4.Font.IsBold();
paragraph4.Add("Report on (" + txtfromdate.Text + ")-(" + txtTodate.Text + ")\n\n");
document1.Add(paragraph4);
//PdfPTable table1 = new PdfPTable(4);
//table1.WidthPercentage = 80;
var table1 = new PdfPTable(4) { WidthPercentage = 100}; var colWidthPercentages = new[] { 60f, 10f, 20f, 20f }; table1.SetWidths(colWidthPercentages);
table1.AddCell(dataGridView1.Columns[0].HeaderText);
table1.AddCell(dataGridView1.Columns[1].HeaderText);
table1.AddCell(dataGridView1.Columns[2].HeaderText);
table1.AddCell(dataGridView1.Columns[3].HeaderText);
for (int x1 = 0; x1 < dataGridView1.Rows.Count - 1; x1++)
{
table1.AddCell(dataGridView1.Rows[x1].Cells[0].Value.ToString());
table1.AddCell(dataGridView1.Rows[x1].Cells[1].Value.ToString());
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[2].Value.ToString()).ToString("N", CInfo));
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[3].Value.ToString()).ToString("N", CInfo));
}
document1.Add(table1);
Paragraph paragraph5 = new Paragraph();
paragraph5.Alignment = Element.ALIGN_CENTER;
paragraph5.Font.IsBold();
paragraph5.Add("\n");
paragraph5.Add("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTotal Amount: " + label5.Text + "\n");
document1.Add(paragraph5);
document1.Close();
System.Diagnostics.Process.Start("D:\\Sankou\\Reports\\Stock\\" + dat + "-StockReport.pdf");
}
else
{
string dat = (DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
Document document1 = new Document(PageSize.A4);
PdfWriter.GetInstance(document1, new FileStream("D:\\Sankou\\Reports\\Stock\\" + dat + "-StockReport.pdf", FileMode.Create));
document1.Open();
Paragraph para1 = new Paragraph();
para1.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Font _font24normal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _font24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font _fontsize24 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontunder = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
iTextSharp.text.Font _fontitalic = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLDITALIC, BaseColor.BLACK);
Paragraph paragraph2 = new Paragraph();
paragraph2.Alignment = Element.ALIGN_RIGHT;
paragraph2.Add("Date" + DateTime.Now.ToShortDateString() + "\n\n");
document1.Add(paragraph2);
Phrase ps = new Phrase("S.K Enterprise" + "\n", _fontunder);
Phrase p1 = new Phrase("TS.NO.9932,MARTHANDAPURAM" + "\n");//, _fontunder);
Phrase p4 = new Phrase("2ND STREET,PUDUKKOTTAI" + "\n");//, _fontunder);
Phrase p2 = new Phrase("04322-233556,220556" + "\n");//, _fontunder);
Phrase p5 = new Phrase("TIN-33574105206" + "\n");//, _fontunder);
Phrase p6 = new Phrase("E-Mail:svsvijayatyrespdkt@gmail.com" + "\n");//, _fontunder);
para1.Add(ps);
para1.Add(p1);
para1.Add(p4);
para1.Add(p2);
para1.Add(p5);
para1.Add(p6);
document1.Add(para1);
Paragraph paragraph4 = new Paragraph();
paragraph4.Alignment = Element.ALIGN_CENTER;
paragraph4.Font.IsBold();
paragraph4.Add("Stock Report on " + dat + "\n\n");
document1.Add(paragraph4);
var table1 = new PdfPTable(4) { WidthPercentage = 100 }; var colWidthPercentages = new[] { 60f, 10f, 20f, 20f }; table1.SetWidths(colWidthPercentages);
table1.AddCell(dataGridView1.Columns[0].HeaderText);
table1.AddCell(dataGridView1.Columns[1].HeaderText);
table1.AddCell(dataGridView1.Columns[2].HeaderText);
table1.AddCell(dataGridView1.Columns[3].HeaderText);
for (int x1 = 0; x1 < dataGridView1.Rows.Count - 1; x1++)
{
table1.AddCell(dataGridView1.Rows[x1].Cells[0].Value.ToString());
table1.AddCell(dataGridView1.Rows[x1].Cells[1].Value.ToString());
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[2].Value.ToString()).ToString("N", CInfo));
table1.AddCell(Convert.ToDecimal(dataGridView1.Rows[x1].Cells[3].Value.ToString()).ToString("N", CInfo));
}
document1.Add(table1);
Paragraph paragraph5 = new Paragraph();
paragraph5.Alignment = Element.ALIGN_CENTER;
paragraph5.Font.IsBold();
paragraph5.Add("\n");
paragraph5.Add("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTotal Amount Rs: " + label5.Text + "\n");
document1.Add(paragraph5);
document1.Close();
System.Diagnostics.Process.Start("D:Reports\\Stock\\" + dat + "-StockReport.pdf");
}
}
catch (Exception)
{
}
finally
{
}
Sample PDF As Follows:
No comments:
Post a Comment