使用iTextSharp导出到PDF文档时隐藏/删除列 [英] Hide / Remove columns when exporting to PDF document using iTextSharp

查看:107
本文介绍了使用iTextSharp导出到PDF文档时隐藏/删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据的GridView. Gridview中的某些列被隐藏visible=false.

I have a GridView with data. Some of the columns in my Gridview are hidden visible=false .

我正在尝试使用iTextSharp将GridView导出为PDF文档,但是设置为visible=false的列也显示在我的PDF文档中,我不希望这样做.

I am trying to export the GridView to a PDF document using iTextSharp but the columns that are set to visible=false are also showing in my PDF document and I don't want that.

如何在没有隐藏列的情况下将GridView数据导出到PDF文档?

How can I export my GridView data to a PDF document without the hidden columns?

这是我到目前为止所做的:

This is what I've done so far:

protected void Button1_Click(object sender, EventArgs e)
{

    PdfPTable pdfTable = new PdfPTable(gvSchedule.HeaderRow.Cells.Count);
    foreach (GridViewRow gridViewRow in gvSchedule.Rows)
    {
        gvSchedule.Columns[0].Visible = false;
        gvSchedule.Columns[1].Visible = false;
        foreach (TableCell tableCell in gridViewRow.Cells)
        {
            gvSchedule.Columns[0].Visible = false;
            gvSchedule.Columns[1].Visible = false;

            PdfPCell pdfCell = new PdfPCell(new Phrase(tableCell.Text));
            pdfTable.AddCell(pdfCell);
        }
    }
    Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    PdfWriter.GetInstance(pdfDocument, Response.OutputStream);

    pdfDocument.Open();
    pdfDocument.Add(pdfTable);
    pdfDocument.Close();

    Response.ContentType = "application/pdf";
    Response.AppendHeader("content-disposition", "attachment;filename=mySchedule.pdf");
    Response.Write(pdfDocument);
    Response.Flush();
    Response.End();
}

我不想在我的PDF文档上显示的两列是:

The two columns that I DO NOT want to display on my PDF document are:

 <asp:BoundField DataField="Address" visible="false" HeaderText="Address" SortExpression="Address"/>

 <asp:BoundField DataField="Area" visible="false" HeaderText="Area" SortExpression="Area"/>

推荐答案

我不熟悉iTextSharp

Am not familiar with iTextSharp

但是看看您的代码

//enter condition here to not add the row 
         foreach (TableCell tableCell in gridViewRow.Cells)
        {
            gvSchedule.Columns[0].Visible = false;
            gvSchedule.Columns[1].Visible = false;
          if(not visiblity false)
          {
            PdfPCell pdfCell = new PdfPCell(new Phrase(tableCell.Text));
            pdfTable.AddCell(pdfCell);
           } 
        }

通过这种方式,您可以避免为所有行创建该列

That way based on visiblity you can avoid creating that column for all the rows

希望这会有所帮助!

这篇关于使用iTextSharp导出到PDF文档时隐藏/删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆