我正在创建一个应用程序,它将ASP.NET页面设置为pdf来打印它。我正在使用iTextSharp。它之前在hTextWriter中显示错误,但我添加了EnableEventValidation =" false",它超过了该错误并显示了以下错误。 它适用于单个网格视图,但不适用于多视图:主视图。 编辑:我认为"登录"脚本与" iduser"中断此脚本,因为它表明它们在调试时互相矛盾。
导出为pdf后显示的错误:
System.ArgumentException: 'Script control is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors(). Parameter name: scriptControl'
我的代码:
1.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="1.aspx.cs" Inherits="abc"
EnableEventValidation="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<input type="button" onclick="PrintDiv();" value="Print" /><asp:Button ID="btnExport" runat="server"
OnClick="btnExport_Click"
Text="Export to PDF" />
<td>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<asp:MultiView ID="MainView" runat="server">
<asp:View ID="View1" runat="server">
等
1.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Collections;
using System.Net;
protected void btnExport_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
form.Controls.Add(MainView);
StringWriter sw = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(hTextWriter);
string html = sw.ToString();
Document Doc = new Document();
//PdfWriter.GetInstance
//(Doc, new FileStream(Request.PhysicalApplicationPath
//+ "\\AmitJain.pdf", FileMode.Create));
PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath
(Environment.SpecialFolder.Desktop) + "\\exportedfile.pdf", FileMode.Create));
Doc.Open();
Chunk c = new Chunk("Export GridView to PDF Using iTextSharp \n", FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk("exported file \n", FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
Doc.Add(p);
Doc.Add(p1);
System.Xml.XmlTextReader xmlReader =
new System.Xml.XmlTextReader(new StringReader(html));
HtmlParser.Parse(Doc, xmlReader);
Doc.Close();
string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\exportedfile.pdf";
ShowPdf(Path);
}
private void ShowPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
}
public override void VerifyRenderingInServerForm(Control control)
{
}