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

时间:2011-12-02 11:01:15

标签: c# .net pdf exception-handling itextsharp

我用于将数据面板转换为PDF的以下代码。但是它给出了一个错误:

  

该文档在document.close()

的位置没有页面

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

htmlworker.Parse方法为您提供已解析的元素。它解析html元素并将它们转换为iTextSharp等效元素。现在您应该将它们添加到文档中。

答案 1 :(得分:1)

无法看到Panel服务器控件中的内容,但您的代码看起来还不错。如果Panel仅包含简单的HTML,则不一定需要按@VahidN的建议单独(在调用IElement时)向HTMLWorker.ParseToList()对象添加HTML Document对象。这是一个简单的例子 - .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>

代码隐藏文件:

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

话虽如此 - 如果Panel包含子控件或复杂的HTML(例如图像),您将遇到问题。您需要考虑到HTMLWorker是一个非常简单的解析器。