我正在尝试将html div标签内的内容转换为pdf,但出现了以下错误:
输入字符串格式不正确
这是我使用c#尝试的代码:
public string getWhileLoopData()
{
string htmlStr = "";
SqlConnection thisConnection = new SqlConnection("Data Source=VELU-PC\\SQLEXPRESS;Initial Catalog=EEP;Trusted_Connection=True;");
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "select * from Product_category";
thisConnection.Open();
SqlDataReader reader = thisCommand.ExecuteReader();
while (reader.Read())
{
string id = reader.GetString(6);
string Name = reader.GetString(3);
string Pass = reader.GetString(5);
htmlStr += "<tr><td><table width='200px'><tr><td align='center'><img src=" + id + " /></td></tr><tr><td align='center'>" + Name + "</td></tr></table></td><td><table width='600px'><tr><td align='left' style='border:1px solid blue;border-radius:7px;box-shadow: 10px 0 10px #888888; padding: 8px 6px 0 7px;'>Features: <br/><p style='margin-top: 10px;'>" + Pass + "</p></td></tr></table></td></tr>";
}
thisConnection.Close();
return htmlStr;
}
void generatetable()
{
divexcel.Visible = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
divexcel.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 80f, 80f, -2f, 35f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{ }
protected void button_Click(object sender, EventArgs e)
{
generatetable();
}
这是我的HTML代码:
以下是需要翻译的内容:
<div id="divexcel" runat="server">
<table><tr><td><asp:Button ID="button" runat="server" Text="Submit"
onclick="button_Click" /></td></tr></table>
<table align="center" width="100%">
<tr>
<td>
<table>
<tr><td align="center">ID</td></tr>
<tr><td align="center">Name </td></tr>
</table>
</td>
<td>
<table>
<tr><td align='left'>Features: <br/><p>Pass</p></td></tr>
</table>
</td>
</tr>
<%=getWhileLoopData()%>
</table>
</div>