如何将ASP.Net网页导出为PDF

时间:2012-11-17 06:53:54

标签: asp.net pdf

如何将ASP.Net网页导出为PDF? 我想将当前的ASP.Net网页导出为PDF格式。

3 个答案:

答案 0 :(得分:2)

我认为你应该使用'itextSharp'。您可以浏览此链接
http://www.codeproject.com/Articles/81118/ITextSharp-Helper-Class
你可以从这里下载它(.dll)
http://sourceforge.net/projects/itextsharp/

答案 1 :(得分:1)

您可以在浏览器中加载页面,并使用CutePDF等免费工具将其打印为PDF。

此工具添加了一个打印机,使您可以将文档/网页另存为PDF。

http://www.cutepdf.com/

答案 2 :(得分:1)

由于asp.net网页在HTMl中呈现,因此您可以使用ITextSharp将HTML页面转换为PDF

这是将HTML转换为PDF的代码

protected override void Render(HtmlTextWriter writer)
    {
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);
        strmRdr.BaseStream.Position = 0;
        string pageContent = strmRdr.ReadToEnd();
        strmRdr.Dispose();
        mem.Dispose();
        writer.Write(pageContent);
        CreatePDFDocument(pageContent);
    }
    public  void CreatePDFDocument(string strHtml)
    {

        string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
        // step 1: creation of a document-object
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se);
        document.Close();
        ShowPdf(strFileName);

    }
    public void ShowPdf(string strFileName)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        Response.ContentType = "application/pdf";
        Response.WriteFile(strFileName);
        Response.Flush();
        Response.Clear();
    }