这是我的ASPX代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" EnableEventValidation="false" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<table align="center">
<tr>
<td colspan="3">
<asp:Label ID="Label6" runat="server" Text="Header Text" style="color:Black;"></asp:Label>
</td>
<td colspan="2" align="right">
<asp:Label ID="Label2" runat="server" Text="Invoice Number : " style="color:Black;"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="" style="color:Black;"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label7" runat="server" Text="Address :" style="color:Black;"></asp:Label><br />
</td>
</tr>
<tr>
<td height="30px" colspan="5">
</td>
</tr>
<tr>
<td align="center" width="150px">
<asp:Label ID="Label22" runat="server" Text="Item" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label23" runat="server" Text="Quantity" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label24" runat="server" Text="Amount" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label25" runat="server" Text="Paid Amount" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label26" runat="server" Text="Balance Amount" style="color:Black;"></asp:Label>
</td>
</tr>
<tr>
<td colspan="5" height="5px">
</td>
</tr>
<tr>
<td colspan="5" height="30px">
</td>
</tr>
<tr>
<td colspan="3" valign="top" align="left">
</td>
<td colspan="2" align="right" valign="top">
<table>
<tr>
<td>
<asp:Label ID="Label37" runat="server" Text="Total Amount" style="color:Black;"></asp:Label>
</td>
<td>
<asp:Label ID="Label38" runat="server" Text="" style="color:Black;"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label39" runat="server" Text="Paid Amount" style="color:Black;"></asp:Label>
</td>
<td>
<asp:Label ID="Label40" runat="server" Text="" style="color:Black;"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="5" height="20px">
</td>
</tr>
<tr>
<td colspan="5">
<asp:Label ID="Label41" runat="server" Text="Terms & Condition" style="color:Black;"></asp:Label><br />
</td>
</tr>
<tr>
<td colspan="5" align="right">
<asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" />
</td>
</tr>
</table>
</asp:Content>
和我的C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;
using System.Drawing;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=" + "abc" + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
h_textw.AddStyleAttribute("font-size", "7pt");
h_textw.AddStyleAttribute("color", "Black");
Document doc = new Document();
doc = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Verdana", 80, iTextSharp.text.Color.RED);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
StringReader s_tr = new StringReader(s_tw.ToString());
HTMLWorker html_worker = new HTMLWorker(doc);
html_worker.Parse(s_tr);
doc.Close();
Response.Write(doc);
}
}
我希望如果我点击发送按钮我的ASPX页面保存在PDF
我的ASPX页面是这样的:
问题是,当我点击发送按钮时,它会出现以下错误
该文件没有页面
我正在使用Visual Studio 2010和ASP.Net C#
答案 0 :(得分:1)
我使用此代码解决了我的问题
string attachment = "attachment; filename=" + Session["pdf_name"] + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
h_textw.AddStyleAttribute("font-size", "8pt");
h_textw.AddStyleAttribute("color", "Black");
Panel1.RenderControl(h_textw);//Name of the Panel
Document doc = new Document();
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
StringReader s_tr = new StringReader(s_tw.ToString());
HTMLWorker html_worker = new HTMLWorker(doc);
html_worker.Parse(s_tr);
doc.Close();
Response.Write(doc);
感谢您的帮助。