在asp.net页面中生成pdf
问题描述:
我需要在asp.net页面中生成pdf页面,该页面包含我搜索过的内容占位符,并尝试了以下代码,但其显示错误是这样的.
>>>扩展程序控件"Search"不是已注册的扩展程序控件.在调用RegisterScriptDescriptors()之前,必须使用RegisterExtenderControl()注册扩展程序控件.
参数名称:extenderControl<<<<
在这段代码中我在此行中出错
Hi,
I need to generate pdf in asp.net page the page contain contentplaceholder i googled and tried the below code but its showing error like this.
>>>Extender control ''Search'' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().
Parameter name: extenderControl <<<<
in this code im getting error in this line
this.Page.RenderControl(hw);
//code//
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.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);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
答
public void ProcessRequest(HttpContext context)
{
int newsId = int.Parse(context.Session["newsId"].ToString());
int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
string dirPathForTextFiles = ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
DataTable UpdatedDateTable = (DataTable)context.Session["theLastUpdatedTextFile"];
UpdatedDateTable.AcceptChanges();
context.Session.Add("currentTextFile", UpdatedDateTable);
List<string> l = new List<string>(UpdatedDateTable.Rows.Count);
try
{
l.Add(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
context.Response.ContentType = getContentType(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
using (FileStream fs = new FileStream(l[0], FileMode.Open, FileAccess.Read))
{
long chunkSize = fs.Length;
byte[] buf = new byte[chunkSize];
int bytesRead = 1;
bytesRead = fs.Read(buf, 0,(int)chunkSize);
if (bytesRead > 0) context.Response.OutputStream.Write(buf, 0, buf.Length);
context.Response.OutputStream.Flush();
}
}
catch (IOException e)
{
string message = e.Message;
}
}
请参阅下面的链接.可能会对您有帮助.
http://www.developerfusion.com/code/5682/create -pdf-files-on-fly-in-c/ [使用C#2.0创建PDF文件的教程 [ ^ ]
谢谢,
Viprat
Hi,
See the below link. It might be help you.
http://www.developerfusion.com/code/5682/create-pdf-files-on-fly-in-c/[^]
Tutorials on creating PDF files using C# 2.0[^]
Thanks,
Viprat