0

我有一个新要求,即在我的网页中有一个按钮,点击后,它会将网页显示为 PDF。在研究中,我发现iTextSharp用于此目的。但是我的Telerik网页中也有很多 ASP 控件。我可以在 PDF 中使用iTextSharp吗?

如果是,那么任何人都可以为我提供一个基本链接以开始使用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 文档,但我也收到了一些不需要的文本,如果有人遇到同样的问题,我该如何删除它?

4

3 回答 3

1

一个非常简单的谷歌搜索将我们带到以下论坛主题,其中包含您的“解决方案”: http ://forums.asp.net/t/1039486.aspx/1

于 2012-05-03T13:04:55.637 回答
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();
    }   
于 2012-05-06T09:14:47.397 回答
0

iTextSharp 附带一个小伙伴:XML Worker

如需演示,请查看此处

尽管文档引用了 Java API,但对 C# 的改编应该很简单。

至于 Telerik/ASP 标签,您可以扩展 XMLWorker 并定义如何将这些标签转换为 PDF 元素。

于 2012-05-03T13:08:05.517 回答