first download iTextSharp.dll and add reference to ur application
protected void Page_Load(object
sender, EventArgs e)
{
string fileName = "Report/pdf.png";
string pdfpath = Server.MapPath("Report/Summary.pdf");
string imagepath = Server.MapPath("Report/pdf.png");
string pdfTemplate = Server.MapPath("Report\\Report.pdf");//@"C:\Report.pdf";
string imagePath = String.Empty;
PdfReader pdfReader = null;
// Create the form filler
FileStream pdfOutputFile = new FileStream(pdfTemplate,
FileMode.Create);
pdfReader = new PdfReader(Server.MapPath("Report\\Summary.pdf"));//"C:\\Template.pdf"
PdfStamper pdfStamper = null;
pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);
// Get the form fields
AcroFields testForm = pdfStamper.AcroFields;
sql = "select ReciId,FEASIBILITYID,FEASIBILITY_DATE,CUSTOMER_NAME,CUSTOMER_ADDRESS from ovlfeasibility_report where FEASIBILITYID='" + FSID + "'";
b.fetchdata(sql,out
dt_feasibility);
testForm.SetField("txtDate",
DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString()
+ "-" + DateTime.Now.Year.ToString()
+ " " + DateTime.Now.ToLocalTime().ToString().Split(' ')[0] + "
" + DateTime.Now.ToLocalTime().ToString().Split(' ')[1]);
if (dt_feasibility.Rows.Count > 0)
{
testForm.SetField("txtRID",
dt_feasibility.Rows[0]["ReciId"].ToString());
testForm.SetField("txtFID",
dt_feasibility.Rows[0]["FEASIBILITYID"].ToString());
testForm.SetField("txtCustName",
dt_feasibility.Rows[0]["Customer_Name"].ToString());
testForm.SetField("txtCustAddress",
dt_feasibility.Rows[0]["Customer_Address"].ToString());
testForm.SetField("imgchart",
imagepath);
iTextSharp.text.Image
instanceImg = iTextSharp.text.Image.GetInstance(imagepath);
PdfContentByte overContent =
pdfStamper.GetOverContent(1);
float[] imageArea =
testForm.GetFieldPositions("imgchart");
iTextSharp.text.Rectangle
imageRect = new Rectangle(imageArea[1],
imageArea[2], imageArea[3], imageArea[4]);
instanceImg.ScaleToFit( 600, 270);
instanceImg.SetAbsolutePosition(-10, 20);
overContent.AddImage(instanceImg);
overContent = null;
}
//PdfContentByte overContent =
pdfStamper.GetOverContent(1);
pdfStamper.FormFlattening = true;
pdfStamper.Close();
pdfReader.Close();
//----start----Disposing Object--------
pdfReader = null;
pdfOutputFile.Dispose();
pdfOutputFile = null;
pdfStamper = null;
testForm = null;
//----end------Disposing Object--------
b = null;
dt_feasibility.Dispose();
dt_feasibility = null;
Response.Clear();
string strpdfpath2 = Server.MapPath("Report\\Report.pdf"); //@"C:/Report.pdf";
Response.ContentType = "text/pdf";
Response.AppendHeader("Content-Disposition",
"attachment;filename=Report.pdf");
Response.TransmitFile(strpdfpath2);
//Directory.Delete("C:\\PDF1",true);
Response.End();
}
No comments :
Post a Comment