【问题标题】:aspx page save on pdfaspx 页面保存为 pdf
【发布时间】:2014-07-30 06:58:40
【问题描述】:

这是我的 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#

【问题讨论】:

  • 哪一行出现错误?
  • @Shaharyar "doc.Close();"
  • 我没做过,但是看了很多文章和问题后发现你没有在文档中添加任何东西。 doc 实例在整个代码中都是空的。
  • @Shaharyar 如果您知道我找不到解决方案,请提供帮助。

标签: c# asp.net pdf


【解决方案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);

谢谢你帮助我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2011-03-17
    • 1970-01-01
    相关资源
    最近更新 更多