简体   繁体   English

将Sharepoint页面转换为pdf时,文档没有页面异常

[英]The document has no pages exception while converting sharepoint page to pdf

Using itextSharp and sharepoint visual webpart coding I am converting current aspx page to pdf but I am not to able convert. 使用itextSharp和sharepoint可视化webpart编码,我将当前的aspx页面转换为pdf,但无法转换。 I have found that issue is in this line "this.Page.RenderControl(hw)" If I comment this line I get "The document has no pages" exception and If I uncomment this line I get "A page can have only one server-side Form tag" error. 我发现问题在“ this.Page.RenderControl(hw)”这一行中侧表单标签”错误。 How can I handle this? 我该如何处理? Pls somebody help me pls.. 请有人帮助我。

protected void BtnSubmit_Click(object sender, EventArgs e)
{
 try
         {
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter(); 
            HtmlTextWriter hw = new HtmlTextWriter(sw); 

            //this.Page.RenderControl(hw);

            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            HttpContext.Current.Response.Write(pdfDoc);
            HttpContext.Current.Response.End();


            }
        catch (Exception exp)
            {


            } 
 }

protected override void Render(HtmlTextWriter writer)
    {
        // Ensure that the control is nested in a server form.
        if (Page != null)
        {
            Page.VerifyRenderingInServerForm(this);
        }
        base.Render(writer);
   }

Because of the way ASP.NET web forms page lifecycle works (sharepoint very similar), you need to move the PDF generation code into the Render stage. 由于ASP.NET Web表单页面生命周期的工作方式(共享点非常相似),您需要将PDF生成代码移到“ Render阶段。 IIRC that's why you see the " A page can have only one server-side Form tag " Exception - your Page.RenderControl() call in the button click event handler comes before the Render() stage, and basically results in rendering the page contents twice. IIRC就是为什么您看到“ 一个页面只能有一个服务器端Form标签 ”异常-按钮单击事件处理程序中的Page.RenderControl()调用位于Render()阶段之前,并且基本上导致呈现页面内容两次。

Anyway, here's a simple working example using XMLWorker to get you started: 无论如何,这是一个使用XMLWorker的简单工作示例,帮助您入门:

first make sure to include these namespaces: 首先请确保包括以下名称空间:

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.xml;
    using iTextSharp.tool.xml;

and then: 接着:

// flag when  page renders normally / when you're sending PDF
      private bool _convertToPdf;
// set _convertToPdf in your BtnSubmit_Click() - I never use AutoEventWireup
      public void ProcessPage(object sender, CommandEventArgs e) {
          _convertToPdf = true;
      }

      protected override void Render(HtmlTextWriter writer) {
        if (!_convertToPdf) { base.Render(writer); }
        else {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=thisPage.pdf");
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb)) {
                using (var htmlTextWriter = new HtmlTextWriter(sw)) {
                    base.Render(htmlTextWriter);
                    using (var document = new Document()) {
                        var pdfWriter = PdfWriter.GetInstance(document, Response.OutputStream);
                        document.Open();
                        using (var stringReader = new StringReader(sb.ToString())) {
                            XMLWorkerHelper.GetInstance().ParseXHtml(
                                pdfWriter, document, stringReader
                            );
                        }
                    }
                }
            }
            Response.End();        
        }

Don't have high expectations regarding how well the PDF displays compared to your aspx page. 不要对PDF与aspx页面的显示效果抱有很高的期望。 iTextSharp isn't meant to be a full-blown HTML to PDF converter. iTextSharp并非是成熟的HTML到PDF转换器。

You need to add following lines: 您需要添加以下行:

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
printableArea.RenderControl(hw); // Here printable area is nothing but div on aspx page with id and runaat="server" tag.

StringReader sr = new StringReader(sw.ToString());
 string strHtml = sr.ReadToEnd();
 sr.Close();

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

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