Hide / Remove columns when exporting to PDF document using iTextSharp

对着背影说爱祢 提交于 2019-12-24 10:18:26

问题


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

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.

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();
}

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

回答1:


Am not familiar with iTextSharp

But looking at your code

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

Hope this helps !



来源:https://stackoverflow.com/questions/48468423/hide-remove-columns-when-exporting-to-pdf-document-using-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!