我有一个webform,我使用了ajax toolkit的HtmlEditorExtender1
。它工作正常。现在我尝试在点击按钮时使用iTextSharp将整个webform转换为pdf。但是我收到了这个错误:
Extender control 'HtmlEditorExtender1' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().
Parameter name: extenderControl
我认为这与HtmlEditorExtender1
<ajaxToolkit:HtmlEditorExtender ID="HtmlEditorExtender1" runat="server" TargetControlID="txtkeyresultforyear" EnableSanitization="false" />
我的网络表单顶部如下所示:
<%@ Page Title="" Language="C#" MasterPageFile="~/ModuleMain.master" AutoEventWireup="true"
ValidateRequest="false" CodeBehind="xyz.aspx.cs" EnableEventValidation = "false" Inherits="abc.xyz" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
我的代码点击按钮
protected void btntopdf_Click(object sender, EventArgs e)
{
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();
}
修改
我收到此错误:
this.Page.RenderControl(hw);
答案 0 :(得分:0)
如果你生成了pdf
然后尝试此代码
pdfWriter.CloseStream = false;
pdfDoc.Close();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition:", "attachment;filename=" + _fileName + ".pdf");
// HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Write(pdfDoc);
保存为pdf。