2017-11-18 6 views
-1

iTextSharp를 사용하여 여러 개의 gridviews를 하나의 pdf로 내보내려고합니다. gridviews 통해 루핑 및 gridview 행을 반복합니다. 루핑은 괜찮아 질거야. 그러나 pdf 다운로드 후에 마지막 gridview 만 볼 수 있습니다. gridviews가 서로 겹쳐 쓰고 마지막 하나만 남아있는 것 같습니다. 여기 내 코드가있다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?iTextsharp를 사용하여 단일 PDF로 다중 gridviews

protected void btnExportToPDF_Click(object sender, EventArgs e) 
     { 
      GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 }; 
      Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f); 


      for (int i = 0; i < gvExcel.Length; i++) 
      { 
       if (gvExcel[i].Visible) 
       { 

        PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count); 

        foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells) 
        { 
         Font font = new Font(); 
         font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor); 
         PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text)); 
         pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor); 
         pdfTbl.AddCell(pdfCell); 
        } 


        foreach (GridViewRow gvRow in gvExcel[i].Rows) 
        { 
         foreach (TableCell tblCell in gvRow.Cells) 
         { 
          Font font = new Font(); 
          font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor); 
          PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text)); 
          pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor); 
          pdfTbl.AddCell(pdfCell); 
         } 
        } 

        //Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f); 
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
        pdfDoc.Open(); 
        pdfDoc.Add(pdfTbl); 
       } 
      } 

      pdfDoc.Close(); 

      //Response.Clear(); 
      Response.ContentType = "application/pdf"; 
      Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf"); 
      Response.Write(pdfDoc); 
      Response.Flush(); 
      Response.End(); 
     } 

답변

1

오류 중 하나가 iText 오류가 아닙니다. 이것은 상식으로 해결할 수있는 간단한 논리적 오류입니다. 다른 오류는 이상합니다. Response을 올바르게 사용하지 않았습니다.

protected void btnExportToPDF_Click(object sender, EventArgs e) 
    { 
     GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 }; 
     Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f); 
     MemoryStream ms = new MemoryStream(); 
     PdfWriter.GetInstance(pdfDoc, ms); 
     pdfDoc.Open(); 

     for (int i = 0; i < gvExcel.Length; i++) 
     { 
      if (gvExcel[i].Visible) 
      { 

       PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count); 
       pdfTbl.SpacingAfter = 20f; 

       foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells) 
       { 
        Font font = new Font(); 
        font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor); 
        PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text)); 
        pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor); 
        pdfTbl.AddCell(pdfCell); 
       } 


       foreach (GridViewRow gvRow in gvExcel[i].Rows) 
       { 
        foreach (TableCell tblCell in gvRow.Cells) 
        { 
         Font font = new Font(); 
         font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor); 
         PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text)); 
         pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor); 
         pdfTbl.AddCell(pdfCell); 
        } 
       } 
       pdfDoc.Add(pdfTbl); 
      } 
     } 

     pdfDoc.Close(); 


     byte[] content = ms.ToArray(); 
     Response.ContentType = "application/pdf"; 
     Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf"); 
     Response.BinaryWrite(content); 
     Response.Flush(); 
     Response.End(); 
    } 

는 다른 문제가있을 수 있습니다,하지만 난 당신이 논리적 오류를 이해 희망 :

  • 당신은 PdfWriter 당신은 루프를 입력 할 때마다 새로운 PDF 파일을 만들었습니다. 하나의 PDF 만 만들려면 PdfWriter 인스턴스를 하나만 만들어야합니다.
  • MemoryStream에 PDF를 만든 다음이 스트림의 내용을 Response 개체에 이진 스트림으로 작성하는 것이 좋습니다. 나는 네가 그곳에서 무엇을하고 있었는지 이해하지 못했다.
  • 나는 또한 20 사용자 단위의 SpacingAfter을 소개했다, 그렇지 않으면 모든 테이블이 모두 하나의 큰 테이블로 함께 붙어있는 것처럼 보일 것이다.

파일을 Response (으)로 보내는 방법에 몇 가지 오류가있을 수 있지만 이미 준비가되어 있습니다. (파일 크기를 브라우저에 보내지 않는 이유는 무엇입니까? 그 크기는 알고 있습니까? content 개체의 바이트 수입니다.)