iTextSharp的没有数据
问题描述:
我有一些数据的GridView和我想要将其从每篇文章转化为PDF也显示出图像和我有PDF格式的白色和绿色的每个单元,但它没有数据iTextSharp的没有数据
我是使用此代码
bool gerarpdf;
protected void btn_pdf_Click(object sender, ImageClickEventArgs e)
{
gerarpdf = true;
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
/* Necessario para gerar pdf com gridview */
}
protected override void Render(HtmlTextWriter writer)
{
if (gerarpdf == true)
{
//pdf generation code called here
int columns = GridView2.Columns.Count;
// Table and PdfTable classes removed in version 5.XXX
iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(columns);
// padding can only be set for cells, __NOT__ PdfPTable object
int padding = 5; float[] widths = new float[columns]; for (int x = 0; x < columns; x++)
{
widths[x] = (float)GridView2.Columns[x].ItemStyle.Width.Value; string cellText = Server.HtmlDecode(GridView2.HeaderRow.Cells[x].Text);
// Cell and Color classes are gone too
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new iTextSharp.text.Phrase(cellText)) { BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000")), Padding = padding }; table.AddCell(cell);
}
// next two lines set the table's __ABSOLUTE__ width
table.SetTotalWidth(widths); table.LockedWidth = true; for (int i = 0; i < columns; i++) { if (GridView2.Rows[i].RowType == DataControlRowType.DataRow) { for (int j = 0; j < columns; j++) { string cellText = Server.HtmlDecode(GridView2.Rows[i].Cells[j].Text); iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new iTextSharp.text.Phrase(cellText)) { Padding = padding }; if (i % 2 != 0) { cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#C2D69B")); } table.AddCell(cell); } } } Response.ContentType = "application/pdf"; iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 10f, 0f); iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); pdfDoc.Add(table); pdfDoc.Close(); Response.End();
}
else
{
//let page render normally
base.Render(writer);
}
}
需要与paggin选项我发现了一个demoe这里,但我不知道这里把它放到我的代码http://archive.aspsnippets.com/demos/GridView2PDF.aspx
我需要的是sothinng与此类似,但也有pagging http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx
答
一旦我不得不完成这项工作来从数据库获取数据并生成Word和PDF文档,并且在很多RND之后,我来到了一个非常棒的事物,它具有极大的帮助和易用性。您可以将表格转换为表格在PDF中,您只需要欺骗列,然后添加任意数量的行就可以了。另外,您还可以将样式添加到表格中。页面格式同样容易页眉页脚每一件事都很简单。