0

问题:

我想在我的页面上单击一个按钮,该按钮将页面的全部内容以 pdf 的形式保存。

查询:

因此,任何人都知道,是否有任何库或程序集或任何代码可以为我提供此功能?

提前致谢

4

1 回答 1

1

身体的一部分

 <asp:PlaceHolder ID="PlaceholderPdf" runat="server">   
 <asp:Label ID="Label1" runat="server" Text="hi hw r u ...i m fine wht about u"></asp:Label>
</asp:PlaceHolder>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click1" 
        Text="converting to pdf" />

下载itextsharp.dll

一些必要的名称空间是

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Text;

.cs 部分

protected void Button2_Click1(object sender, EventArgs e)
{
//SIMPLE TEXT IS CONVERTING
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Filename.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    //Render PlaceHolder to temporary stream
    System.IO.StringWriter stringWrite = new StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    PlaceholderPdf.RenderControl(htmlWrite);
    StringReader reader = new StringReader(stringWrite.ToString());
    //Create PDF document
    Document doc = new Document(PageSize.A4);
    HTMLWorker parser = new HTMLWorker(doc);
    PdfWriter.GetInstance(doc, Response.OutputStream);
    doc.Open();
    try
    {
        //Create a footer that will display page number
        //Parse Html
        parser.Parse(reader);

    }
    catch (Exception ex)
    {
        //Display parser errors in PDF.
        //Parser errors will also be wisible in Debug.Output window in VS
        Paragraph paragraph = new Paragraph("Error! " + ex.Message);
        Chunk text = paragraph.Chunks[0] as Chunk;
        if (text != null)
        {
        }
        doc.Add(paragraph);
    }
    finally
    {
        doc.Close();
    }
}
 public override void VerifyRenderingInServerForm(Control control)
{
}

试试这个,让我知道

于 2013-11-13T07:42:19.280 回答