簡體   English   中英

單擊按鈕將網頁顯示為pdf

[英]Displaying a web page as a pdf with click of a button

我有一個新要求,即在我的網頁上有一個按鈕,點擊后,它會將該網頁顯示為PDF。 在研究中我發現iTextSharp用於此目的。 但我的網頁上也有很多Telerik和ASP控件。 我可以使用iTextSharp在PDF中獲得它嗎?

如果是,那么任何人都可以請給我一個開始使用iTextSharp的基本鏈接,因為我還沒有遇到過這個工具。

根據@ahaliav的回答,我嘗試使用以下代碼。 但是,我沒有得到pdf上的控件。

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        MemoryStream msOutput = new MemoryStream();
        TextReader reader = new StringReader(HtmlContent);

        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);
        HTMLWorker worker = new HTMLWorker(document);
        // step 2:
        // we create a writer that listens to the document
        // and directs a XML-stream to a file
        PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

        // step 3: we create a worker parse the document

        // step 4: we open document and start the worker on the document
        document.Open();
        worker.StartDocument();

        // step 5: parse the html into the document
        worker.Parse(reader);

        // step 6: close the document and the worker
        worker.EndDocument();
        worker.Close();
        document.Close();
        //Response.Write(document);
        //Response.End();

    }
    protected override void Render(HtmlTextWriter writer)
    {
        // setup a TextWriter to capture the markup
        TextWriter tw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(tw);

        // render the markup into our surrogate TextWriter
        base.Render(htw);

        // get the captured markup as a string
        string pageSource = tw.ToString();

        // render the markup into the output stream verbatim
        writer.Write(pageSource);

        // remove the viewstate field from the captured markup
        HtmlContent = Regex.Replace(pageSource,
            "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
            "", RegexOptions.IgnoreCase);

        // the page source, without the viewstate field, is in viewStateRemoved
        // do what you like with it

    }

請幫忙 。 我還有其他選項,那就是當我點擊一個按鈕時顯示完整網頁的圖像。任何人都可以指導我找到這兩個問題的任何可能的方向。

我終於改變了我的代碼:

        Response.ContentType = "application/msword";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        MemoryStream msOutput = new MemoryStream();

有了這個,我能夠得到包含我所有控件的msword文檔,但我也得到了一些不需要的文本,如果有人遇到同樣的問題,我怎么能刪除它?

一個非常簡單的谷歌搜索帶我們到以下論壇帖子,其中有你的“sollution”: http//forums.asp.net/t/1039486.aspx/1

經過一些研究后我沒有找到任何解決方案來顯示html中的控件這個代碼工作正常但沒有顯示控件,過去我使用這個http://www.winnovative-software.com/它運行良好對於html頁面,包括所有控件,你也可以測試它,“唯一”的問題是你需要支付許可證

protected void btnExportToPdf_Click(object sender, EventArgs e)
    {
        renderPDF = true;
    }

    protected override void Render(HtmlTextWriter writer)
    {           
        if (renderPDF == true)
        {
            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);
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            HtmlTextWriter tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
            base.Render(tw);
            // get the captured markup as a string
            string pageSource = tw.ToString();
            //Get the rendered content
            string sContent = sb.ToString();
            //Now output it to the page, if you want
            writer.Write(sContent);
        }
    }

    public void CreatePDFDocument(string strHtml)
    {


        string strHTMLpath = Server.MapPath("MyHTML.html");
        StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
        strWriter.Write(strHtml);
        strWriter.Close();
        string strFileName = HttpContext.Current.Server.MapPath("map1.pdf"); //    strFileName    "C:\\Inetpub\\wwwroot\\Test\\map1.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);
        TextReader tr = new StreamReader(Server.MapPath("MyHTML.html"));

        //add the collection to the document
        document.Open();                        
        /////////
        iTextSharp.text.html.simpleparser.HTMLWorker worker = new iTextSharp.text.html.simpleparser.HTMLWorker(document);

        worker.StartDocument();

        //// step 5: parse the html into the document
        worker.Parse(tr);

        //// step 6: close the document and the worker
        worker.EndDocument();

        //worker.Parse(tr); // getting error "Illegal characters in path"
        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();
    }   

iTextSharp附帶了一個小伙伴: XML Worker

有關演示,請查看此處

盡管文檔引用了Java API,但對C#的適應應該是直截了當的。

對於Telerik / ASP標記,您可以擴展XMLWorker並定義如何將此類標記轉換為PDF元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM