Get the whole page as a PDF [closed]

我的梦境 提交于 2019-12-02 19:16:52

问题


Question:

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

Queries:

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?

Thanks in advance


回答1:


body part

 <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" />

download itextsharp.dll

some necessary name space are

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

.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



来源:https://stackoverflow.com/questions/19947721/get-the-whole-page-as-a-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!