转换为PDF时出错:文档在document.close()处没有页面
本文关键字:close document PDF 出错 文档 转换 | 更新日期: 2023-09-27 18:16:21
我使用下面的代码将数据面板转换为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);
}
看不到Panel
服务器控件中的内容,但是您的代码看起来不错。如果面板只包含简单的HTML,您不一定需要单独添加HTML IElement
对象(当调用HTMLWorker.ParseToList()
时)到Document
对象,如@VahidN所建议的。下面是一个简单的例子——.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();
}
}
话虽这么说,如果面板包含子控件或复杂的HTML(例如图像),您将遇到问题。你需要考虑HTMLWorker是一个非常简单的解析器
htmlworker.Parse
方法给出已解析的元素。它解析html元素并将它们转换为iTextSharp的等效元素。现在您应该将它们添加到文档中。