简体   繁体   English

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

[英]How to Export ASP.Net Web Page to PDF

How to Export ASP.Net Web Page to PDF?如何将 ASP.Net 网页导出为 PDF? I want to export current ASP.Net web page into PDF.我想将当前的 ASP.Net 网页导出为 PDF。

I think you should use 'itextSharp'. 我认为您应该使用“ itextSharp”。 You can go through this link 你可以通过这个链接
http://www.codeproject.com/Articles/81118/ITextSharp-Helper-Class http://www.codeproject.com/Articles/81118/ITextSharp-Helper-Class
You can download it (.dll) from here 您可以从这里下载(.dll)
http://sourceforge.net/projects/itextsharp/ http://sourceforge.net/projects/itextsharp/

You could load the page in a browser and print it to a PDF using a free tool like CutePDF. 您可以将页面加载到浏览器中,然后使用类似CutePDF的免费工具将其打印为PDF。

This tool adds a printer that enables you to save the document/web page as a PDF. 此工具添加了一个打印机,使您可以将文档/网页另存为PDF。

http://www.cutepdf.com/ http://www.cutepdf.com/

As asp.net web page get render in HTMl so you can user ITextSharp to convert HTML page into PDF 由于asp.net网页在HTMl中呈现,因此您可以使用ITextSharp将HTML页面转换为PDF

here is code to Convert HTML to 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();
    }

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

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