iTextSharp 导出 asp 网络面板
iTextSharp export asp net Panel
我下载了最新版本的 iTextSharp dll。
我在 PDF
这个简单的标签 Panel
中使用它在 c# 中导出。
<asp:Panel ID="views" runat="server">
<fieldset style="margin-left: 50px">
<legend style="font-weight: bold; color: Red; margin-left: 10px;">Testing<br />
<br />
1. testing tble</legend>
<br />
<table>
<tr>
<td>
<label for="Zone">
Zone<br />
</label>
<asp:Label ID="Zone" runat="server"></asp:Label></td>
<td>
<label for="cp">
cp<br />
</label>
<asp:Label ID="cp" runat="server"></asp:Label></td>
<td>
<label for="LinesName">
LinesName<br />
</label>
<asp:Label ID="LinesName" runat="server"></asp:Label></td>
<td>
<label for="LinesCode">
LinesCode<br />
</label>
<asp:Label ID="LinesCode" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>
</table>
</fieldset>
</asp:Panel>
并使用此 代码隐藏 来填充 Panel
:
using (OdbcCommand command =
new OdbcCommand(sql, cn))
{
try
{
command.Parameters.AddWithValue("param1", Decrypt(Request.QueryString["id"].ToString()));
command.Connection.Open();
using (OdbcDataAdapter da =
new OdbcDataAdapter(command))
{
using (OdbcDataReader sdr = command.ExecuteReader())
{
while (sdr.Read())
{
Zone.Text = sdr["Zone"].ToString();
cp.Text = sdr["cp"].ToString();
LinesName.Text = sdr["LinesName"].ToString();
LinesCode.Text = sdr["LinesCode"].ToString();
cl.Text = sdr["cl"].ToString();
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
导出代码 PDF
:
private void PdfFiles1()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Decrypt(Request.QueryString["id"].ToString()) + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
views.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 30f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
我在 PDF
return 中没有错误,但我只有第一行 table
:
<tr>
<td>
<label for="Zone">
Zone<br />
</label>
<asp:Label ID="Zone" runat="server"></asp:Label></td>
<td>
<label for="cp">
cp<br />
</label>
<asp:Label ID="cp" runat="server"></asp:Label></td>
<td>
<label for="LinesName">
LinesName<br />
</label>
<asp:Label ID="LinesName" runat="server"></asp:Label></td>
<td>
<label for="LinesCode">
LinesCode<br />
</label>
<asp:Label ID="LinesCode" runat="server"></asp:Label></td>
</tr>
第二行没有打印:
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>
请帮助我,在此先感谢您。
您的 HTML table 不平衡,iText 期望 table 中的每个单元格都被考虑在内。要修复它,只需为第二行提供适当数量的单元格:
<tr>
<td colspan="4">
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>
如果您不想跨越它们,您只需要包括空单元格:
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
<td></td>
<td></td>
<td></td>
</tr>
或者:
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
<td colspan="3"></td>
</tr>
我下载了最新版本的 iTextSharp dll。
我在 PDF
这个简单的标签 Panel
中使用它在 c# 中导出。
<asp:Panel ID="views" runat="server">
<fieldset style="margin-left: 50px">
<legend style="font-weight: bold; color: Red; margin-left: 10px;">Testing<br />
<br />
1. testing tble</legend>
<br />
<table>
<tr>
<td>
<label for="Zone">
Zone<br />
</label>
<asp:Label ID="Zone" runat="server"></asp:Label></td>
<td>
<label for="cp">
cp<br />
</label>
<asp:Label ID="cp" runat="server"></asp:Label></td>
<td>
<label for="LinesName">
LinesName<br />
</label>
<asp:Label ID="LinesName" runat="server"></asp:Label></td>
<td>
<label for="LinesCode">
LinesCode<br />
</label>
<asp:Label ID="LinesCode" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>
</table>
</fieldset>
</asp:Panel>
并使用此 代码隐藏 来填充 Panel
:
using (OdbcCommand command =
new OdbcCommand(sql, cn))
{
try
{
command.Parameters.AddWithValue("param1", Decrypt(Request.QueryString["id"].ToString()));
command.Connection.Open();
using (OdbcDataAdapter da =
new OdbcDataAdapter(command))
{
using (OdbcDataReader sdr = command.ExecuteReader())
{
while (sdr.Read())
{
Zone.Text = sdr["Zone"].ToString();
cp.Text = sdr["cp"].ToString();
LinesName.Text = sdr["LinesName"].ToString();
LinesCode.Text = sdr["LinesCode"].ToString();
cl.Text = sdr["cl"].ToString();
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
导出代码 PDF
:
private void PdfFiles1()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Decrypt(Request.QueryString["id"].ToString()) + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
views.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 30f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
我在 PDF
return 中没有错误,但我只有第一行 table
:
<tr>
<td>
<label for="Zone">
Zone<br />
</label>
<asp:Label ID="Zone" runat="server"></asp:Label></td>
<td>
<label for="cp">
cp<br />
</label>
<asp:Label ID="cp" runat="server"></asp:Label></td>
<td>
<label for="LinesName">
LinesName<br />
</label>
<asp:Label ID="LinesName" runat="server"></asp:Label></td>
<td>
<label for="LinesCode">
LinesCode<br />
</label>
<asp:Label ID="LinesCode" runat="server"></asp:Label></td>
</tr>
第二行没有打印:
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>
请帮助我,在此先感谢您。
您的 HTML table 不平衡,iText 期望 table 中的每个单元格都被考虑在内。要修复它,只需为第二行提供适当数量的单元格:
<tr>
<td colspan="4">
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>
如果您不想跨越它们,您只需要包括空单元格:
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
<td></td>
<td></td>
<td></td>
</tr>
或者:
<tr>
<td>
<label for="cl">
cl<br />
</label>
<asp:Label ID="cl" runat="server"></asp:Label></td>
<td colspan="3"></td>
</tr>