2011-05-19 40 views
1

我正在嘗試使用iTextSharp創建一個pdf報告,我很難理解爲什麼它不能正確地進入下一行添加單元格。爲什麼這些表格單元格不被添加?

下面的代碼:

public class Centralizador 
    { 
     public void PrintCentralizador(int gradeParaleloId, string gradeName, string paraleloName, string courseName) 
     { 
      var studentRepo = new StudentRepository(); 
      var students = studentRepo.FindAllStudentsFromGradeParalelo(gradeParaleloId).OrderBy(s => s.LastNameFather); 
      int rowHeight = 13; 
      string filePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Centralizador.pdf"; 

      try 
      { 
       Document document = new Document(PageSize.LETTER); 
       //Landscape the document. 
       document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate()); 
       BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1250, BaseFont.EMBEDDED); 
       Font font = new Font(baseFont, 8); 

       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)); 
       document.Open(); 

       GradeParaleloRepository paraRep = new GradeParaleloRepository(); 
       var gra = paraRep.FindGradeParalelo(gradeParaleloId); 
       Paragraph p = new Paragraph(new Phrase("Centralizador - Gestion " + DateTime.Now.Year + " \n " + courseName + " " + gra.Grade.Name + " " + gra.Name + "\n Colegio Madre Vicenta Uboldi \n " + DateTime.Now, font)); 
       document.Add(p); 

       PdfPTable table = new PdfPTable(36); //36 Column table. 
       table.TotalWidth = 800f; 
       table.LockedWidth = true; 
       float[] widths = new float[] { 0.13f, 1.4f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f, 0.13f }; 
       table.SetWidths(widths); 

       PdfPCell blankCell = new PdfPCell(new Phrase("", font)); 
       blankCell.FixedHeight = 25; 

       PdfPCell blankCellB = new PdfPCell(new Phrase("", font)); 
       blankCellB.FixedHeight = 25; 

       table.AddCell(blankCell); 
       table.AddCell(blankCellB); 

       PdfPCell mat = new PdfPCell(new Phrase("MAT", font)); 
       mat.Colspan = 3; 
       mat.HorizontalAlignment = 1; 
       table.AddCell(mat); 

       PdfPCell len = new PdfPCell(new Phrase("LEN", font)); 
       len.HorizontalAlignment = 1; 
       len.Colspan = 3; 
       table.AddCell(len); 

       PdfPCell psi = new PdfPCell(new Phrase("PSI", font)); 
       psi.Colspan = 3; 
       psi.HorizontalAlignment = 1; 
       table.AddCell(psi); 

       PdfPCell cna = new PdfPCell(new Phrase("CNA", font)); 
       cna.Colspan = 3; 
       cna.HorizontalAlignment = 1; 
       table.AddCell(cna); 

       PdfPCell soc = new PdfPCell(new Phrase("SOC", font)); 
       soc.Colspan = 3; 
       soc.HorizontalAlignment = 1; 
       table.AddCell(soc); 

       PdfPCell ing = new PdfPCell(new Phrase("ING", font)); 
       ing.Colspan = 3; 
       ing.HorizontalAlignment = 1; 
       table.AddCell(ing); 

       PdfPCell efi = new PdfPCell(new Phrase("EFI", font)); 
       efi.Colspan = 3; 
       efi.HorizontalAlignment = 1; 
       table.AddCell(efi); 

       PdfPCell mus = new PdfPCell(new Phrase("MUS", font)); 
       mus.Colspan = 3; 
       mus.HorizontalAlignment = 1; 
       table.AddCell(mus); 

       PdfPCell apl = new PdfPCell(new Phrase("APL", font)); 
       apl.Colspan = 3; 
       apl.HorizontalAlignment = 1; 
       table.AddCell(apl); 

       PdfPCell rel = new PdfPCell(new Phrase("REL", font)); 
       rel.Colspan = 3; 
       rel.HorizontalAlignment = 1; 
       table.AddCell(rel); 

       PdfPCell com = new PdfPCell(new Phrase("COM", font)); 
       com.Colspan = 3; 
       com.HorizontalAlignment = 1; 
       table.AddCell(com); 

       PdfPCell blankCellC = new PdfPCell(new Phrase("", font)); 
       blankCellC.FixedHeight = 25; 
       table.AddCell(blankCellC); 

       //This is supposed tobe on a new row. But isn't. It seems 
       //everything below this comment doesn't even get added. 
       PdfPCell numero = new PdfPCell(new Phrase("No.", font)); 
       numero.FixedHeight = rowHeight; 
       numero.HorizontalAlignment = 0; 
       table.AddCell(numero);     

       PdfPCell nombres = new PdfPCell(new Phrase("Apellidos y Nombres", font)); 
       nombres.FixedHeight = rowHeight; 
       nombres.HorizontalAlignment = 0; 
       table.AddCell(nombres); 

       for (int i = 0; i < 2; i++) 
       { 
        PdfPCell pa = new PdfPCell(new Phrase("PA.", font)); 
        table.AddCell(pa); 

        PdfPCell re = new PdfPCell(new Phrase("RE.", font)); 
        table.AddCell(re); 

        PdfPCell nf = new PdfPCell(new Phrase("NF.", font)); 
        table.AddCell(nf); 
       } 

       PdfPCell obs = new PdfPCell(new Phrase("OBS.", font)); 

       table.SpacingBefore = 20f; 
       table.SpacingAfter = 20f; 

       document.Add(table); 
       document.Close(); 
      } 
      catch (DocumentException de) 
      { 
       Debug.WriteLine(de.Message); 
      } 
      catch (IOException ioe) 
      { 
       Debug.WriteLine(ioe.Message); 
      } 
     } 
    } 

下面是它是如何結束的圖片:所以正確添加的最後一列融爲一體,還增加了填補空白單元格,然後它不添加下一步怎麼走。它只是不顯示。有什麼建議麼?

enter image description here

+0

如果我在構造函數(37列)添加宣佈PdfTable 37並添加另一個浮到寬度陣列中,「沒有。」出現。所以這個錯誤似乎是它不會下到下一行。有什麼建議麼? – 2011-05-19 18:52:31

回答

3

您需要添加列的確切數量爲每一行已顯示出它。

見我的回答對您的其他問題PdfTable isn't added to my document

+1

驚人的如何這種行爲沒有記錄。希望這可以幫助其他有類似問題的人。 – 2011-05-23 22:48:18

0

試試這個

foreach (DataGridViewRow row in dgvCalls.Rows) 
{ 
    foreach (DataGridViewCell cell in row.Cells) 
    { 
     if (cell.Visible) 
     { 
      if (cell.Value != null) 
       pdfTable.AddCell(cell.Value.ToString()); 
      else 
       pdfTable.AddCell(""); 
     } 
     // continue; 
    } 
} 
相關問題