2012-05-03 59 views
0

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

如果是,那么任何人都可以,请给我提供一个基础环节,因为我还没有碰到过这个工具来又开始使用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文件,但我也得到了一些不必要的文本,我怎么能删除,如果有人遇到同样的问题?

+1

我使用许可[wkhtmltopdf(http://code.google.com/p/wkhtmltopdf/)我的一个项目是基于网页生成PDF。 –

+0

我可以得到telerik控件也在那个pdf –

+0

不知道,但我的猜测是否定的。 –

回答

1

经过一番研究,我没有发现显示在html控件的任何解决方案下面这段代码工作正常,但用了显示控制,在我用这个http://www.winnovative-software.com/过去 和它的作品很好的HTML页面,包括所有controlls您还可以测试它onlie,“唯一”的问题是你需要支付

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(); 
    } 
0

iTextSharp的自带的小伙伴:XML Worker

对于一个演示,看看here

即使documentation引用了Java API,对C#的适应也应该很简单。

至于Telerik的/ ASP代码,你可以扩展XMLWorker并确定如何这样的标签转换成PDF元素。

+0

我进一步修改了我的问题,请帮助 –

+0

您向原始问题添加了一个新问题。你应该为此创建一个新问题。 –

+0

对不起,但你可以请你帮我,我会发布一个新的问题 –

相关问题