我有一个网格视图与一些数据,我想把它转换成pdf还显示图像从每篇文章,我有pdf格式的白色和绿色的每个单元格,但没有数据上它
我正在使用这段代码
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中
我需要的是类似于此,但也与包装http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx
发布于 2011-04-02 00:56:40
有一次,我不得不做的工作,从数据库中提取数据,并生成Word和PDF文件和后,我来到了一个很棒的事情,有很大的帮助和方便,到use.You可以转换你的表格,你只需要取消列,然后添加你想要的行,加上你可以添加样式到表格too.Page格式是同样容易,页眉,页脚,每件事都很容易。
这是链接PDFsharp & MigraDoc
https://stackoverflow.com/questions/5515190
复制相似问题