使用C#和asp.net转换HTML内容为PDF

编程入门 行业动态 更新时间:2024-10-26 16:34:19
本文介绍了使用C#和asp转换HTML内容为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面我试图将内容转换内部的 HTML div标签的PDF 我发现了以下错误:

输入字符串是不是在正确的格式是发生

下面是我尝试使用C#代码:

公共字符串getWhileLoopData() {串htmlStr =; 的SqlConnection thisConnection =新的SqlConnection(数据源= VELU-PC\\SQLEXPRESS;初始目录= EEP; Trusted_Connection = TRUE;); 的SqlCommand thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText =选择PRODUCT_CATEGORY *; thisConnection.Open(); SqlDataReader的读卡器= thisCommand.ExecuteReader(); 而(reader.Read()) {字符串ID = reader.GetString(6); 字符串名称= reader.GetString(3); 字符串传递= reader.GetString(5); htmlStr + =&所述; TR>&下; TD>&下;表宽度='200像素'>&下; TR>&下; TD对齐=中心>&下; IMG SRC =+ ID + />< / TD>< / TR>< TR>< TD ALIGN =中心>中+姓名+< / TD>< / TR>< /表>< / TD>< TD><表格的宽度=600px的'>< TR>< TD ALIGN =左风格=边界:1px的固体蓝色;边界半径:7px的框阴影:10px的0 10px的#888888;填充:8像素6像素0 7px的;'>产品特点:< BR />< p风格=保证金机顶:10px的;>中+通行证+< / P>< / TD>< / TR>< /表>< / TD>< / TR>中; } thisConnection.Close(); 返回htmlStr; } 无效generatetable() { divexcel.Visible = TRUE; Response.ContentType =应用程序/ PDF Response.AddHeader(内容处置,附件;文件名= TestPage.pdf); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter的SW =新的StringWriter(); 的HtmlTextWriter HW =新的HtmlTextWriter(SW); divexcel.RenderControl(HW); StringReader SR =新StringReader(sw.ToString()); 文档pdfDoc =新的文件(PageSize.A4,80F,80F,-2F,35F); HTMLWorker的HTMLParser =新HTMLWorker(pdfDoc); PdfWriter作家= PdfWriter.GetInstance(pdfDoc,Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(SR); pdfDoc.Close(); 的Response.Write(pdfDoc); 到Response.End(); } 公共覆盖无效VerifyRenderingInServerForm(System.Web.UI.Control控制) {} 保护无效button_Click(对象发件人,EventArgs五) { generatetable(); }

下面是我的html代码

< DIV ID =divexcel=服务器> <表>< TR>< TD>< ASP:按钮的ID =按钮=服务器文本=提交的onclick =button_Click/>< / TD>< / TR>< /表> <表ALIGN =中心宽度=100%> < TR> < TD> <表> < TR>< TD ALIGN =中心> ID< / TD>< / TR> < TR>< TD ALIGN =中心>名称和LT; / TD>< / TR> < /表> < / TD> < TD> <表> < TR>< TD ALIGN ='左'>产品特点:< BR />< P>通行证< / P>< / TD>< / TR> < /表> < / TD> < / TR> <%= getWhileLoopData()%GT; < /表> < / DIV>

解决方案

尝试与宽度,以取代宽度='200像素= 200它应该工作。从宽度每个地方都删除像素。

Here i am trying to convert the content inside html div tag to pdf i found the following error:

Input string was not in a correct format is occur

Here is the code i tried using 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(); }

Here is my html code:

<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>

解决方案

Try to replace width='200px' with width='200' it should work. Remove px from every places with width.

更多推荐

使用C#和asp.net转换HTML内容为PDF

本文发布于:2023-06-10 16:27:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/615012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   net   asp   PDF   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!