2017-03-07 4 views
0

나는이 우스운 이야기다. 그러나 i가 어디에서 붙어 있는지에 관해 모른다. 는 스피 itextsharp을 사용하여 PDF 문서에 테이블을 만들려고하고 이미지 아래Pdfptable 중복 테이블을 얻는 것

Duplicate table

PDF를 생성하기 위해 사용하는 코드 스피가를 볼 수 성공적으로 테이블을 생성.

private static void TestPDF() 
    { 
     Document document = new Document(iTextSharp.text.PageSize.A4, 60, 60, 10, 0); 
     try 
     { 
      PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\PdfUpload\" + "testFile" + ".pdf", FileMode.Create)); 
      document.Open(); 
      PdfPTable table = new PdfPTable(new float[] { 4, 16, 4, 10, 4, 10, 4, 10, 10, 10, 4, 10, 10, 7, 6, 8 }); 
      table.TotalWidth = 580; 
      //table.WidthPercentage = 120; 

      PdfPCell cell = null; 

      modifycell(cell, "", ref table, 2, 0); 
      modifycell(cell, "Formative Asssessment", ref table, 8, 0); 
      modifycell(cell, "Summetive Assessment", ref table, 3, 0); 
      modifycell(cell, "Final Result", ref table, 3, 0); 

      modifycell(cell, "Year", ref table, 0, 2); 
      modifycell(cell, "Module Name", ref table, 0, 2); 
      modifycell(cell, "Digital Assessment", ref table, 2, 0); 
      modifycell(cell, "Group work", ref table, 2, 0); 
      modifycell(cell, "Assignment", ref table, 2, 0); 
      modifycell(cell, "Subtotal", ref table, 2, 0); 
      modifycell(cell, "Examanation", ref table, 3, 0); 
      modifycell(cell, "Final (100%)", ref table, 0, 2); 
      modifycell(cell, "Symbol", ref table, 0, 2); 
      modifycell(cell, "Status (Pass/Fail)", ref table, 0, 2); 

      modifycell(cell, "%", ref table, 0, 0); 
      modifycell(cell, "Weighted Controbution (20%)", ref table, 0, 0); 
      modifycell(cell, "%", ref table, 0, 0); 
      modifycell(cell, "Weighted Controbution (10%)", ref table, 0, 0); 
      modifycell(cell, "%", ref table, 0, 0); 
      modifycell(cell, "Weighted Controbution (30%)", ref table, 0, 0); 
      modifycell(cell, "Weighted Controbution (60%)", ref table, 0, 0); 
      modifycell(cell, "Subminimum achieved", ref table, 0, 0); 
      modifycell(cell, "%", ref table, 0, 0); 
      modifycell(cell, "Weighted Controbution (40%)", ref table, 0, 0); 
      modifycell(cell, "Subminimum achieved", ref table, 0, 0); 
      table.WriteSelectedRows(0, -1, 8, 700, writer.DirectContent); 
      document.Add(table); 
      document.Close(); 
     } 
     catch (Exception oException) { string sMessage = oException.ToString(); } 
    } 

    private static void modifycell(PdfPCell cell, string str, ref PdfPTable table, int Colspan = 0, int RowSpan = 0, 
     int Width = 0) 
    { 
     BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); 
     Font font = new Font(bf, 6); 
     cell = new PdfPCell(new Phrase(str, font)); 
     if (RowSpan > 0) 
      cell.Rowspan = RowSpan; 
     if (Colspan > 0) 
      cell.Colspan = Colspan; 
     if (Width > 0) 
      cell.Width = Width; 
     cell.HorizontalAlignment = Element.ALIGN_CENTER; 
     cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
     cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220); 
     cell.Border = 1; 
     cell.BorderColorLeft = BaseColor.BLACK; 
     cell.BorderWidthLeft = .5f; 
     cell.BorderColorRight = BaseColor.BLACK; 
     cell.BorderWidthRight = .5f; 
     cell.BorderColorBottom = BaseColor.BLACK; 
     cell.BorderWidthBottom = .5f; 
     table.AddCell(cell); 
    } 

답변

0

왜 두 번 테이블을 추가하나요? 그것은 테이블이 있음을 놀라게해서는 안

document.Add(table); 

:

table.WriteSelectedRows(0, -1, 8, 700, writer.DirectContent); 

그런 다음이 줄을 그것에게 두 번째 시간을 추가 :이 줄을 절대 위치에서 한 번에 추가

두 번 추가되었습니다. 정신을 차리고 그 중 하나를 제거하십시오.

+0

안녕 @Bruno Lowagie 내가 iTextSharp를 사용하기 시작하고 난 비트 저기 혼란스러워했지만, 빠른 응답 그게 주셔서 감사 전에 단지 일주일에 잘 작동합니다. – shiva

+0

문제가 해결되면 대답을 수락하는 것이 좋습니다. (현재 점수가 0 인 옆의 체크 표시를 참조하십시오.) –