Click here to Skip to main content
15,887,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
}
Posted
Updated 15-Mar-16 19:38pm

1 solution

 
Share this answer
 
Comments
Member 12366212 16-Mar-16 6:26am    
i have already tried this....... my above code generate pdf file but blank

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900