简体   繁体   English

aspx页面保存为pdf

[英]aspx page save on pdf

Here is my ASPX Code: 这是我的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>

and my C# Code: 和我的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);
    }
}

I want that if i click on Send Button my ASPX Page save on PDF 如果我单击“发送”按钮,我希望ASPX页面保存为PDF

My ASPX Page is that: 我的ASPX页面是: 在此处输入图片说明

The problem is that when i click on Send button it gives the following error 问题是,当我单击“发送”按钮时,出现以下错误

The document has no pages 该文档无页面

I am using Visual Studio 2010 and ASP.Net C# 我正在使用Visual Studio 2010和ASP.Net C#

I solved my issue using this Code 我使用此代码解决了我的问题

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);

Thanks for helping me. 谢谢你帮我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM