Well this question seems to be repeated but i tried some code with the help of google and it dont work for me. actually it generate blank pdf file.
i want to export datatable to PDF file and save the file in serverside folder in my web application. for that i just want to call the the web service method using ajax call and expoet datatable to pdf file and save it to server side and return the file name and on ajax sucess i print the pdf file on client side. can any one suggest me the other better way.
What I have tried:
//my web service
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string ExportPdfReport(DataTable dt)
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strUploadPath+"/"+strFilename, FileMode.Create));
document.Open();
Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);
PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f, 4f, 4f,4f, 4f, 4f, 4f,4f, 4f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
int iCol = 0;
string colname = "";
PdfPCell cell = null;
cell.Colspan = dt.Columns.Count;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, font5));
}
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
table.AddCell(new Phrase(r[3].ToString(), font5));
}
} document.Add(table);
document.Close();
return strFilename;
}