TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ankit Kumar
NA
159
48.6k
Problem with iTextSharp
Oct 11 2017 1:23 AM
Dear all,
i am using iTextSharp to export my C# gridview data to PDF but the problem is file is getting downloaded but it's not being downloaded with data and in proper pdf format.
Kindly guide me.
below is my action method and i am calling it from ajax request, There is no view for that action method, the data is coming in datatable but file is not getting created correctly.
Here is code
public
FileStreamResult exporttopdf(
int
empid,
int
branchid,
int
departmentid,
int
designationid,
string
selectedmonth,
string
year)
{
GridView gvExportData =
new
GridView();
DataTable Attendance =
new
DataTable();
MemoryStream workStream =
new
MemoryStream();
using
(SqlConnection connection = DLConnection.getConnectionInstance())
{
if
(year ==
""
)
{
year = DateTime.Now.Year.ToString();
}
SqlParameter[] parameter =
new
SqlParameter[6];
parameter[0] =
new
SqlParameter(
"@employeeid"
, empid);
parameter[1] =
new
SqlParameter(
"@BranchId"
, branchid);
parameter[2] =
new
SqlParameter(
"@DepartmentId"
, departmentid);
parameter[3] =
new
SqlParameter(
"@DesignationId"
, designationid);
parameter[4] =
new
SqlParameter(
"@NameOfTheMonth"
, selectedmonth);
parameter[5] =
new
SqlParameter(
"@year"
, year);
SqlDataReader dr;
dr = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure,
"Usp_getattendance"
, parameter);
Attendance.Load(dr);
}
SqlConnection sqlconnection = DLConnection.getConnectionInstance();
string
employeenamequery =
"select FirstName+' '+LastName from employee where employeeid='"
+ empid +
"'"
;
string
empname = SqlHelper.ExecuteScalar(sqlconnection, CommandType.Text, employeenamequery).ToString();
gvExportData.DataSource = Attendance;
gvExportData.DataBind();
Response.ClearContent();
Response.ContentType =
"application/pdf"
;
Response.AddHeader(
"content-disposition"
,
"attachment;filename="
+ empname +
" Attendance.pdf"
);
Response.Charset =
"UTF-8"
;
Response.ContentEncoding = Encoding.UTF8;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
PdfManager PdfInstance =
new
PdfManager();
using
(StringWriter sw =
new
StringWriter())
{
using
(HtmlTextWriter hw =
new
HtmlTextWriter(sw))
{
gvExportData.RenderControl(hw);
StringReader sr =
new
StringReader(sw.ToString());
//string page = ddlPaperSize.SelectedValue.ToString();
using
(iTextSharp.text.Document pdfDoc =
new
iTextSharp.text.Document(PageSize.A2, 10f, 10f, 30f, 30f))
{
HTMLWorker htmlparser =
new
HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, workStream);
//set the PageEvent of the pdfWriter instance to the instance of our PDFPage class
writer.PageEvent = PdfInstance;
pdfDoc.Open();
Paragraph ReportHeading =
new
Paragraph();
ReportHeading.Add(
"Monthly Attendance"
);
ReportHeading.Alignment = Element.ALIGN_CENTER;
ReportHeading.Font.Size = 25;
pdfDoc.Add(ReportHeading);
pdfDoc.Add(
new
Paragraph(
"\n"
));
Phrase data =
new
Phrase();
htmlparser.Parse(sr);
byte
[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
gvExportData.Visible =
false
;
}
}
}
return
new
FileStreamResult(workStream,
"application/pdf"
);
}
Reply
Answers (
6
)
How to use Output cache for page VarybyUrl
on popup box enter key not working