Hi,
1. First create a method which will retrieve you the records from db.Then,
2. Write the event for button click and call the method to retrieve the data.
3. Assign the return value from the method to the object
4. Create a document and a PDFtable,add the return data to the datatable using for loop.
Then follow the type specification method.
Given below is the sample.
protected void btnExportPDF_Click(object sender, EventArgs e)
{
MasterData objMaster = new MasterData();
using (MemoryStream MStream = new MemoryStream())
{
ServiceAccess objservice = new ServiceAccess();
objMaster = objservice.GetMasterDataDescription();
if (objMaster != null)
{
// step 1: creation of a document-object
Document document = new Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document,MStream);
// step 3: we open the document
document.SetMargins(5, 12, 12, 5);
document.Open();
//step 4:Add data to document table cell
int NumColumns = 12;
PdfPTable datatable = new PdfPTable(NumColumns);
Font fnt = FontFactory.GetFont("Times New Roman",1);
Phrase ph=new Phrase("GIFTREGISTRY-ASSIGNMENT1");
document.Add(ph);
int[] headerwidths = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };// percentage
datatable.SetWidths(headerwidths);
datatable.TotalWidth = 100;
datatable.WidthPercentage = 100;
datatable.AddCell("Registry#");
datatable.AddCell("EventDate");
datatable.AddCell("RegDate");
datatable.AddCell("RegFName");
datatable.AddCell("RegLName");
datatable.AddCell("CoFName");
datatable.AddCell("CoLName");
datatable.AddCell("Source");
datatable.AddCell("SKU");
datatable.AddCell("EventName");
datatable.AddCell("Itemname");
datatable.AddCell("UPC");
for (int j = 0; j < objMaster.ReturnDataList.Length; j++)
{
datatable.AddCell(objMaster.ReturnDataList[j].RCustcode);
datatable.AddCell(objMaster.ReturnDataList[j].REvent_Date.ToString("dd-MMM-yyyy"));
datatable.AddCell(objMaster.ReturnDataList[j].RRegister_Date.ToString("dd-MMM-yyyy"));
datatable.AddCell(objMaster.ReturnDataList[j].RRegFirst_Name);
datatable.AddCell(objMaster.ReturnDataList[j].RRegLast_Name);
datatable.AddCell(objMaster.ReturnDataList[j].RCoregFirst_Name);
datatable.AddCell(objMaster.ReturnDataList[j].RCoregLast_Name);
datatable.AddCell(objMaster.ReturnDataList[j].RSource);
datatable.AddCell(objMaster.ReturnDataList[j].RSKU);
datatable.AddCell(objMaster.ReturnDataList[j].REvent_Name);
datatable.AddCell(objMaster.ReturnDataList[j].RItemName);
datatable.AddCell(objMaster.ReturnDataList[j].RUPC);
}
// step 5: we add a table data to the document
document.Add(datatable);
// step 6: we close the document
document.Close();
Response.Clear();
Response.Charset = string.Empty;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + "EventPdfReport" + ".pdf");
Response.BinaryWrite(MStream.GetBuffer());
Response.End();
}
}
}
Regards,
Vijetha.M.M
Sathiya_Narayanan, if this helps please login to Mark As Answer. | Alert Moderator