我有一些关于iTextSharp的问题。我尝试将html转换为pdf,在此之前我尝试将gridview转换为pdf.It的find.Now我尝试转换更多然后gridview,在这个视图中,它有图像,gridview等但是它无法将其转换为pdf.My错误是“无法将'iTextSharp.text.html.simpleparser.CellWrapper'类型的对象强制转换为'iTextSharp.text.Paragraph'。”请帮助我,在这种情况下我该怎么做。
这是我的观点(aspx)
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Default.aspx.cs" Inherits="ManageOrg.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="content-type" content="image/jpeg; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script src="js/jquery.min.js"></script>
<script src="js/jquery.scrolly.min.js"></script>
<script src="js/jquery.scrollzer.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-layers.min.js"></script>
<script src="js/init.js"></script>
<script src="js/bootstrap.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/skel.css" rel="stylesheet" />
<link href="css/style.css" rel="stylesheet" />
<link href="css/style-wide.css" rel="stylesheet" />
<link href="css/style-narrow.css" rel="stylesheet" />
<link href="css/mystyle.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlEntirePanel" runat="server">
<!-- Header -->
<div id="header" class="skel-layers-fixed">
<div class="top">
<!-- Logo -->
<div id="logo">
<%--<span class="image avatar48"><img src="http://localhost:41834/Default.aspx/images/avatar.jpg" alt="" /></span>--%>
<h1 id="title">Jane Doe</h1>
<p>Hyperspace Engineer</p>
</div>
<!-- Nav -->
<nav id="nav">
<!--
Prologue's nav expects links in one of two formats:
1. Hash link (scrolls to a different section within the page)
<li><a href="#foobar" id="foobar-link" class="icon fa-whatever-icon-you-want skel-layers-ignoreHref"><span class="label">Foobar</span></a></li>
2. Standard link (sends the user to another page/site)
<li><a href="http://foobar.tld" id="foobar-link" class="icon fa-whatever-icon-you-want"><span class="label">Foobar</span></a></li>
-->
<ul>
<li><a href="#top" id="top-link" class="skel-layers-ignoreHref"><span class="icon fa-home">Intro</span></a></li>
<li><a href="#portfolio" id="portfolio-link" class="skel-layers-ignoreHref"><span class="icon fa-th">Portfolio</span></a></li>
<li><a href="#about" id="about-link" class="skel-layers-ignoreHref"><span class="icon fa-user">About Me</span></a></li>
<li><a href="#contact" id="contact-link" class="skel-layers-ignoreHref"><span class="icon fa-envelope">Contact</span></a></li>
</ul>
</nav>
</div>
<div class="bottom">
<!-- Social Icons -->
<ul class="icons">
<li><a href="#" class="icon fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="#" class="icon fa-facebook"><span class="label">Facebook</span></a></li>
<li><a href="#" class="icon fa-github"><span class="label">Github</span></a></li>
<li><a href="#" class="icon fa-dribbble"><span class="label">Dribbble</span></a></li>
<li><a href="#" class="icon fa-envelope"><span class="label">Email</span></a></li>
</ul>
</div>
</div>
<!-- Main -->
<div id="main">
<!-- Intro -->
<section id="top" class="one dark cover">
<div class="container">
<header>
<h2 class="alt">Hi! I'm <strong>Prologue</strong>, a <a href="http://html5up.net/license">free</a> responsive<br />
site template designed by <a href="http://html5up.net">HTML5 UP</a>.</h2>
<p>Ligula scelerisque justo sem accumsan diam quis<br />
vitae natoque dictum sollicitudin elementum.</p>
</header>
<footer>
<a href="#portfolio" class="button scrolly">Magna Aliquam</a>
</footer>
</div>
</section>
<!-- Portfolio -->
<section id="portfolio" class="two">
<div class="container">
<header>
<h2>Portfolio</h2>
</header>
<p>Vitae natoque dictum etiam semper magnis enim feugiat convallis convallis
egestas rhoncus ridiculus in quis risus amet curabitur tempor orci penatibus.
Tellus erat mauris ipsum fermentum etiam vivamus eget. Nunc nibh morbi quis
fusce hendrerit lacus ridiculus.
<br />
<asp:Button ID="btnPDF" runat="server" Text="Export" OnClick="btnPDF_Click" />
</p>
<div class="row">
<%--<div class="4u">
<article class="item">
<a href="#" class="image fit"><img src="images/pic02.jpg" alt="" /></a>
<header>
<h3>Ipsum Feugiat</h3>
</header>
</article>
<article class="item">
<a href="#" class="image fit"><img src="images/pic03.jpg" alt="" /></a>
<header>
<h3>Rhoncus Semper</h3>
</header>
</article>
</div>
<div class="4u">
<article class="item">
<a href="#" class="image fit"><img src="images/pic04.jpg" alt="" /></a>
<header>
<h3>Magna Nullam</h3>
</header>
</article>
<article class="item">
<a href="#" class="image fit"><img src="images/pic05.jpg" alt="" /></a>
<header>
<h3>Natoque Vitae</h3>
</header>
</article>
</div>
<div class="4u">
<article class="item">
<a href="#" class="image fit"><img src="images/pic06.jpg" alt="" /></a>
<header>
<h3>Dolor Penatibus</h3>
</header>
</article>
<article class="item">
<a href="#" class="image fit"><img src="images/pic07.jpg" alt="" /></a>
<header>
<h3>Orci Convallis</h3>
</header>
</article>
</div>--%>
<div style="font-family:'Cordia New'; font-size:20px;">
<asp:Panel ID="Panel1" runat="server" ScrollBars="Horizontal" Width="1114px" >
<asp:GridView ID="GridView_Boss" runat="server" AutoGenerateColumns="false" CssClass="table-bordered">
<Columns>
<asp:TemplateField>
<HeaderTemplate>ชื่อ - สกุล</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:LinkButton ID="lbl_name" runat="server" Text='<%# Eval("Thai_Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Eng</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:LinkButton ID="lbl_enName" runat="server" Text='<%# Eval("Eng_Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Position</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_position" runat="server" Text='<%# Eval("position") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Position En</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_positionEN" runat="server" Text='<%# Eval("positionEN") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Email</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_email" runat="server" Text='<%# Eval("user_mail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Care</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_care" runat="server" Text='<%# Eval("care")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>CEG</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_ceg" runat="server" Text='<%# Eval("ceg")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>SECRETARY</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_secretary" runat="server" Text='<%# Eval("secretary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>SECRETARY Mail</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_secretary_mail" runat="server" Text='<%# Eval("secre_mail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>ATTN</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_attn" runat="server" Text='<%# Eval("attn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>MVPN</HeaderTemplate>
<ItemStyle Wrap="false" CssClass="rowMid" />
<ItemTemplate>
<asp:Label ID="lbl_mvpn" runat="server" Text='<%# Eval("mvpn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</div>
<%--<asp:Table ID="tb_boss" runat="server" CssClass="table-bordered">
<asp:TableHeaderRow runat="server">
<asp:TableHeaderCell>ชื่อ-สกุล</asp:TableHeaderCell>
<asp:TableHeaderCell>Eng</asp:TableHeaderCell>
<asp:TableHeaderCell>ตำแหน่ง</asp:TableHeaderCell>
<asp:TableHeaderCell>Eng</asp:TableHeaderCell>
<asp:TableHeaderCell>Email</asp:TableHeaderCell>
<asp:TableHeaderCell>ดูแล</asp:TableHeaderCell>
<asp:TableHeaderCell>CEG</asp:TableHeaderCell>
<asp:TableHeaderCell>เลขานุการ</asp:TableHeaderCell>
<asp:TableHeaderCell>เลขา email</asp:TableHeaderCell>
<asp:TableHeaderCell>ATTN</asp:TableHeaderCell>
<asp:TableHeaderCell>MVPN</asp:TableHeaderCell>
</asp:TableHeaderRow>
<ta
</asp:Table>--%>
</div>
</div>
</section>
<!-- About Me -->
<section id="about" class="three">
<div class="container">
<header>
<h2>About Me</h2>
</header>
<a href="#" class="image featured"><img src="images/pic08.jpg" alt="" /></a>
<p>Tincidunt eu elit diam magnis pretium accumsan etiam id urna. Ridiculus
ultricies curae quis et rhoncus velit. Lobortis elementum aliquet nec vitae
laoreet eget cubilia quam non etiam odio tincidunt montes. Elementum sem
parturient nulla quam placerat viverra mauris non cum elit tempus ullamcorper
dolor. Libero rutrum ut lacinia donec curae mus vel quisque sociis nec
ornare iaculis.</p>
</div>
</section>
<!-- Contact -->
<section id="contact" class="four">
<div class="container">
<header>
<h2>Contact</h2>
</header>
<p>Elementum sem parturient nulla quam placerat viverra
mauris non cum elit tempus ullamcorper dolor. Libero rutrum ut lacinia
donec curae mus. Eleifend id porttitor ac ultricies lobortis sem nunc
orci ridiculus faucibus a consectetur. Porttitor curae mauris urna mi dolor.</p>
<form method="post" action="#">
<div class="row 50%">
<div class="6u"><input type="text" name="name" placeholder="Name" /></div>
<div class="6u"><input type="text" name="email" placeholder="Email" /></div>
</div>
<div class="row 50%">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" value="Send Message" />
</div>
</div>
</form>
</div>
</section>
</div>
<!-- Footer -->
<div id="footer">
<!-- Copyright -->
<ul class="copyright">
<li>© Untitled. All rights reserved.</li><li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</div>
</asp:Panel>
</div>
</form>
</body>
</html>
这是我的代码(aspx.cs)
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ManageOrg
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Thai_Name", typeof(string));
dt.Columns.Add("Eng_Name", typeof(string));
dt.Columns.Add("position", typeof(string));
dt.Columns.Add("positionEN", typeof(string));
dt.Columns.Add("user_mail", typeof(string));
dt.Columns.Add("care", typeof(string));
dt.Columns.Add("ceg", typeof(string));
dt.Columns.Add("secretary", typeof(string));
dt.Columns.Add("secre_mail", typeof(string));
dt.Columns.Add("attn", typeof(string));
dt.Columns.Add("mvpn", typeof(string));
dt.Rows.Add("lllllllll", llllllllllllll", "lllllllllllll", "llllllllll", "lllllllllllllllllll", "-", "lllllllllllll", "llllllllllll", "2223", "lllllllllllll");
dt.Rows.Add("lllllllll", llllllllllllll", "lllllllllllll", "llllllllll", "lllllllllllllllllll", "-", "lllllllllllll", "llllllllllll", "2223", "lllllllllllll");
dt.Rows.Add("lllllllll", llllllllllllll", "lllllllllllll", "llllllllll", "lllllllllllllllllll", "-", "lllllllllllll", "llllllllllll", "2223", "lllllllllllll");
dt.Rows.Add("lllllllll", llllllllllllll", "lllllllllllll", "llllllllll", "lllllllllllllllllll", "-", "lllllllllllll", "llllllllllll", "2223", "lllllllllllll");
dt.Rows.Add("lllllllll", llllllllllllll", "lllllllllllll", "llllllllll", "lllllllllllllllllll", "-", "lllllllllllll", "llllllllllll", "2223", "lllllllllllll");
GridView_Boss.DataSource = dt;
GridView_Boss.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
protected void btnPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlEntirePanel.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
}