简体   繁体   English

转换为PDF时出错:文档在document.close()处没有页面

[英]Error converting to PDF: The document has no pages at document.close()

The below code that I am using for convert the panel of the data to PDF.But it is giving an error: 我用于下面的代码将数据面板转换为PDF。但是它给出了一个错误:

The document has no pages at the place of document.close() 该文档在document.close()的位置没有页面

Here is my code: 这是我的代码:

protected void ConvertPDF_click(object sender, EventArgs e)
{
    string attachment = "attachment; filename=test.pdf";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    htextw.AddStyleAttribute("font-size", "7pt");
    htextw.AddStyleAttribute("color", "Black");

    Panel1.RenderControl(htextw);//Name of the Panel
    Document document = new Document();
    document = new Document(PageSize.A4, 5, 5, 15, 5);
    FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
    PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();

    StringReader str = new StringReader(stw.ToString());
    HTMLWorker htmlworker = new HTMLWorker(document);
    htmlworker.Parse(str);

    document.Close();
    Response.Write(document);
}

htmlworker.Parse method gives you the parsed elements. htmlworker.Parse方法为您提供解析后的元素。 It parses the html elements and converts them to their iTextSharp equivalent elements. 它解析html元素并将它们转换为iTextSharp等效元素。 Now you should add them to the document. 现在您应该将它们添加到文档中。

Can't see what's in your Panel server control, but your code looks OK. 无法看到Panel服务器控件中的内容,但您的代码看起来没问题。 And if the Panel only contains simple HTML, you don't necessarily need to add HTML IElement objects individually (when calling HTMLWorker.ParseToList() ) to the Document object as suggested by @VahidN. 如果Panel只包含简单的HTML,则不一定需要按照HTMLWorker.ParseToList()建议单独添加HTML IElement对象(当调用HTMLWorker.ParseToList() )到Document对象。 Here's a simple example - .aspx file: 这是一个简单的例子 - .aspx文件:

<%@ Page Language='C#' AutoEventWireup='true' CodeFile='panelTest.aspx.cs' Inherits='panelTest' %>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat='server'><title></title></head>
<body><form id='form1' runat='server'>
<asp:Panel ID='testPanel' runat='server'>
<h1>A H1 Heading</h1>
<table width='100%' border='1' align='center' 
  cellpadding='4' cellspacing='0' 
>
<tr><td>TABLE ROW 1: CELL 1</td></tr>
<tr><td>TABLE ROW 2: CELL 1</td></tr>
</table>
<p>A Paragraph with <strong>bold</strong> and <em>italic</em> text.</p>
</asp:Panel>
<asp:Button runat='server'
  oncommand='process'
  text='Convert HtmlControl to PDF'
/>
</form></body></html>

Code-behind file: 代码隐藏文件:

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class panelTest : Page {
  protected void process(object sender, CommandEventArgs e) {
    string attachment = "attachment; filename=test.pdf";
    Response.AddHeader("content-disposition", attachment);    
    Response.ContentType = "application/pdf";
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    htmlWriter.AddStyleAttribute("font-size", "10pt");
    htmlWriter.AddStyleAttribute("color", "Black");      
    testPanel.RenderControl(htmlWriter);
    using (Document document = new Document()) {
      PdfWriter.GetInstance(document, Response.OutputStream);
      document.Open();
      StringReader stringReader = new StringReader(stringWriter.ToString());
      HTMLWorker htmlworker = new HTMLWorker(document);
      htmlworker.Parse(stringReader);    
    }
    Response.End();
  }
}

With that being said - if the Panel contains child controls or complex HTML (images for example), you will have problems. 话虽如此 - 如果Panel包含子控件或复杂的HTML(例如图像),您将遇到问题。 You need to take into account that HTMLWorker is a very simple parser. 您需要考虑到HTMLWorker是一个非常简单的解析器。

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

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