我有一个网页表单。我已经在一个div中添加了所有内容,并希望以pdf格式导出此div。以pdf格式导出时pdf即将出现。
protected void btnExport_Click(object sender, EventArgs e)
{
String DownloadFile = String.Format("{0}Targetkeywordsofdt-{1}", ddlproject.SelectedItem.Text, txtEndDate.Text.ToString() + ".pdf");
Response.AddHeader("content-disposition", "attachment;filename=" + DownloadFile);
Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
PdfReport.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
string strpath = Server.MapPath("~") + "/Temp/" + ddlproject.SelectedItem.Text.Trim() + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strpath, FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
pdfDoc.Dispose();
Response.Write(pdfDoc);
Response.End();
}