获取整个页面的PDF文件 [英] Get the whole page as a PDF

查看:304
本文介绍了获取整个页面的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:

我想在我的页面点击该页面的全部内容保存为PDF形式的按钮。

I want a button on my page on clicking which the whole content of the page is saved in the form of pdf.

查询:

因此​​,任何人有任何想法,有没有图书馆或组装,或任何一件code,可以为我提供此功能?

So any one has any idea that, is there any library or assembly or any piece of code that can provide me with this feature?

在此先感谢

推荐答案

正文部分

 <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部分

.cs part

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)
{
}

试试这个,让我知道

try this and let me know

这篇关于获取整个页面的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆