【问题标题】:Generate a PDF file from two gridviews using iTextSharp使用 iTextSharp 从两个网格视图生成 PDF 文件
【发布时间】:2015-07-15 12:43:12
【问题描述】:

我使用 iTextSharp 从 gridview 生成 PDF 文件。

我需要帮助,因为我不熟悉 iTextSharp。

我在我的 aspx 页面中使用了两个网格视图:gvProductsgvUsers

使用 iTextSharp 从单个 (gvProducts) 网格视图生成 PDF 文件正常工作。

我无法在同一个 PDF 中打印第一个和第二个 GridView。

这就是我想要的:

  1. 打开 PDF 文档;
  2. 我的第一个 GridView 的打印结果;
  3. 我的第二个 GridView 的打印结果;
  4. 在客户端中关闭、保存和下载 PDF 文档。

有人知道我该怎么做吗?

提前谢谢你。

下面是我的代码。

protected void ExportToPDFWithFormatting()
{
    //link button column is excluded from the list
    int colCount = gvProducts.Columns.Count - 1;

    //Create a table
    PdfPTable table = new PdfPTable(colCount);

    table.HorizontalAlignment = 1;
    table.WidthPercentage = 100;

    //create an array to store column widths
    int[] colWidths = new int[gvProducts.Columns.Count];

    PdfPCell cell;
    string cellText;

    //create the header row
    for (int colIndex = 0; colIndex < colCount; colIndex++)
    {

        table.SetWidths(new int[] { 0, 40, 120, 110, 60, 60, 100, 90, 100, 50, 50, 50, 40, 30, 260, 200, 0 });

        //fetch the header text
        cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text);

        //create a new cell with header text
        BaseFont bf = BaseFont.CreateFont(
                                BaseFont.HELVETICA,
                                BaseFont.CP1252,
                                BaseFont.EMBEDDED,
                                false);

        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE);
        cell = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font));
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.VerticalAlignment = Element.ALIGN_MIDDLE;
        cell.FixedHeight = 45f;

        //set the background color for the header cell
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));

        //add the cell to the table. we dont need to create a row and add cells to the row
        //since we set the column count of the table to 4, it will automatically create row for
        //every 4 cells
        table.AddCell(cell);
    }


    //export rows from GridView to table
    for (int rowIndex = 0; rowIndex < gvProducts.Rows.Count; rowIndex++)
    {
        if (gvProducts.Rows[rowIndex].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < gvProducts.Columns.Count - 1; j++)
            {
                //fetch the column value of the current row
                cellText = Server.HtmlDecode(gvProducts.Rows[rowIndex].Cells[j].Text);

                //create a new cell with column value
                cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8)));                   
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.FixedHeight = 25f;

                //Set Color of Alternating row
                if (rowIndex % 2 != 0)
                {
                    cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#f5f5dc"));
                }
                else
                {
                    cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#fcfcfc"));
                }


                //add the cell to the table
                table.AddCell(cell);
            }
        }
    }

    //Create the PDF Document
    Document pdfDoc = new Document(PageSize.A3.Rotate(), 30f, 30f, 30f, 0f);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    //open the stream
    pdfDoc.Open();
    table.HeaderRows = 1;
    iTextSharp.text.Font fdefault = FontFactory.GetFont("Verdana", 18, iTextSharp.text.Font.BOLD, BaseColor.BLUE);
    string s = "Report";
    pdfDoc.Add(new Paragraph(s, fdefault));

    //add the table to the document
    pdfDoc.Add(table);

    //close the document stream
    pdfDoc.Close();

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;" + DateTime.Now + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}

编辑#1

这是我在第二个 GridView (gvUsers) 上生成 pdf 的代码。

代码正常,但在标题列中我有 GridView 第一的名称 (gvProducts)...为什么?

    //start pdf gridview number two
    int colCountUsers = gvUsers.Columns.Count - 1;
    PdfPTable tableUsers = new PdfPTable(colCountUsers);
    tableUsers.HorizontalAlignment = 1;
    tableUsers.WidthPercentage = 100;
    int[] colWidthsUsers = new int[gvUsers.Columns.Count];
    PdfPCell cellUsers;
    string cellTextUsers;
    for (int colIndexUsers = 0; colIndexUsers < colCountUsers; colIndexUsers++)
    {
        tableUsers.SetWidths(new int[] { 0, 40, 120, 110, 60, 60, 100 });
        cellTextUsers = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndexUsers].Text);
        BaseFont bfUsers = BaseFont.CreateFont(
                                BaseFont.HELVETICA,
                                BaseFont.CP1252,
                                BaseFont.EMBEDDED,
                                false);

        iTextSharp.text.Font fontUsers = new iTextSharp.text.Font(bfUsers, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE);
        cellUsers = new PdfPCell(new Phrase(cellTextUsers.Replace("<br />", Environment.NewLine), fontUsers));
        cellUsers.HorizontalAlignment = Element.ALIGN_CENTER;
        cellUsers.VerticalAlignment = Element.ALIGN_MIDDLE;
        cellUsers.FixedHeight = 45f;
        cellUsers.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));
        tableUsers.AddCell(cellUsers);
    }


    for (int rowIndexUsers = 0; rowIndexUsers < gvUsers.Rows.Count; rowIndexUsers++)
    {
        if (gvUsers.Rows[rowIndexUsers].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < gvUsers.Columns.Count - 1; j++)
            {
                cellTextUsers = Server.HtmlDecode(gvUsers.Rows[rowIndexUsers].Cells[j].Text);
                cellUsers = new PdfPCell(new Phrase(cellTextUsers, FontFactory.GetFont("PrepareForExport", 8)));
                cellUsers.HorizontalAlignment = Element.ALIGN_CENTER;
                cellUsers.VerticalAlignment = Element.ALIGN_MIDDLE;
                cellUsers.FixedHeight = 25f;
                tableUsers.AddCell(cellUsers);
            }
        }
    }

    Document pdfDocUsers = new Document(PageSize.A3.Rotate(), 30f, 30f, 30f, 0f);
    PdfWriter.GetInstance(pdfDocUsers, Response.OutputStream);
    pdfDocUsers.Open();
    tableUsers.HeaderRows = 1;
    iTextSharp.text.Font fdefaultUsers = FontFactory.GetFont("Verdana", 18, iTextSharp.text.Font.BOLD, BaseColor.BLUE);
    string sUsers = "Users";
    pdfDocUsers.Add(new Paragraph(sUsers, fdefaultUsers));
    pdfDocUsers.Add(tableUsers);
    pdfDocUsers.Close();
    //end pdf gridview number two



    //in first pdf gridview
    //add the table to the document
    pdfDoc.Add(table);
    pdfDoc.Add(tableUsers);

【问题讨论】:

  • 你能不能只创建另一个PdfPTable tableUsers 并用它和gvUsers 做类似的操作,然后你将第一个表添加到pdf 文档pdfDoc.Add(table); 也调用pdfDoc.Add(tableUsers);
  • 我和@myroman 在一起,只要做两次。你说它不起作用,但你没有第二个代码块,所以我们不能告诉你为什么。但是,我可以告诉你,摆脱Response.Write(pdfDoc)。那行代码并没有像你想象的那样做。
  • 我很困惑,你有两个 Document 类实例,pdfDocUserspdfDoc,所以你正在制作两个独立且不相关的 PDF。对吗?这就是你的意图吗?
  • 不是我只需要一个带有两个gridviews日期的pdf文件-

标签: c# asp.net pdf gridview itextsharp


【解决方案1】:
int colCount1 = gvProducts1.Columns.Count - 1;

//Create a table
PdfPTable table1 = new PdfPTable(colCount1);

table1.HorizontalAlignment = 1;
table1.WidthPercentage = 100;

//create an array to store column widths
int[] colWidths1 = new int[gvProducts1.Columns.Count];

PdfPCell cell1;
string cellText1;

//create the header row
for (int colIndex1 = 0; colIndex1 < colCount1; colIndex1++)
{

    table1.SetWidths(new int[] { 0, 40, 120, 110, 60, 60, 100, 90, 100, 50, 50, 50, 40, 30, 260, 200, 0 });

    //fetch the header text
    cellText1 = Server.HtmlDecode(gvProducts1.HeaderRow.Cells[colIndex1].Text);

    //create a new cell with header text
    BaseFont bf1 = BaseFont.CreateFont(
                            BaseFont.HELVETICA,
                            BaseFont.CP1252,
                            BaseFont.EMBEDDED,
                            false);

    iTextSharp.text.Font font1 = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE);
    cell1 = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font));
    cell1.HorizontalAlignment = Element.ALIGN_CENTER;
    cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
    cell1.FixedHeight = 45f;

    //set the background color for the header cell
    cell1.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));

    //add the cell to the table. we dont need to create a row and add cells to the row
    //since we set the column count of the table to 4, it will automatically create row for
    //every 4 cells
    table1.AddCell(cell1);
}


//export rows from GridView to table
for (int rowIndex1 = 0; rowIndex1 < gvProducts1.Rows.Count; rowIndex1++)
{
    if (gvProducts.Rows[rowIndex1].RowType == DataControlRowType.DataRow)
    {
        for (int j1 = 0; j1 < gvProducts1.Columns.Count - 1; j1++)
        {
            //fetch the column value of the current row
            cellText1 = Server.HtmlDecode(gvProducts1.Rows[rowIndex1].Cells[j].Text);

            //create a new cell with column value
            cell1 = new PdfPCell(new Phrase(cellText1, FontFactory.GetFont("PrepareForExport", 8)));                   
            cell1.HorizontalAlignment = Element.ALIGN_CENTER;
            cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell1.FixedHeight = 25f;

            //Set Color of Alternating row
            if (rowIndex1 % 2 != 0)
            {
                cell1.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#f5f5dc"));
            }
            else
            {
                cell1.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#fcfcfc"));
            }


            //add the cell to the table
            table1.AddCell(cell1);
        }
    }
}

//Create the PDF Document

//open the stream


//add the table to the document
pdfDoc.Add(table1);

//close the document stream
pdfDoc.Close();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" + DateTime.Now + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();

【讨论】:

  • 对不起,我不明白你的答案......你的代码在 pdf 文件中导出两个网格视图?
  • @AntonioMailtraq 1) 我使用了两个表格来生成 pdf。 2)然后使用gridview获取列的标题。 3)然后使用 [row][column] 表数填充列。 4)不仅仅是用doc.add(filltablename)添加它......你会在pdf中找到gridview结果
  • @AntonioMailtraq 请解释您是否为第二个gridview编写了任何代码以在您的代码中列出我无法在任何地方找到它....在安排上述代码后,我认为您的问题会解决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2015-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多