2011-05-20 18 views
1

下面的代碼:PdfTable不添加到我的文檔

public void PrintBoletin(int studentId, int gradeParaleloId) 
{ 
    StudentRepository studentRepo = new StudentRepository(); 
    var student = studentRepo.FindStudent(studentId); 
    int rowHeight = 20; 

    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Boletin.pdf"; 
    Document document = new Document(PageSize.LETTER); 
    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("Boletin de Notas - Gestion " + DateTime.Now.Year + " \n " + gra.Grade.Name + " " + gra.Name + "\n Colegio Madre Vicenta Uboldi \n " + DateTime.Now, font)); 
    Paragraph p2 = new Paragraph(new Phrase("Alumno: " + student.StudentId + " - " + student.LastNameFather + " " + student.LastNameMother + ", " + student.Name, font)); 

    document.Add(p); 
    document.Add(p2); 

    PdfPTable table = new PdfPTable(14); //14 Column table. 
    table.TotalWidth = 550f; 
    float[] widths = new float[] { 1.4f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f, 0.18f }; 
    table.SetWidths(widths); 

    PdfPCell materia = new PdfPCell(new Phrase("MATERIA", font)); 
    materia.Rowspan = 2; 
    materia.Colspan = 2; 
    materia.HorizontalAlignment = 1; 
    materia.VerticalAlignment = 1; 
    table.AddCell(materia); 


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

    document.Add(table); 
    document.Close(); 

    Process.Start(filePath); 
} 

當我打開生成的PDF文件時,沒有將表添加到文檔中的。只有段落是。有任何想法嗎?

回答

2

您創建一個包含14列的表格,但只能添加一個表格。在PdfPTable中,您只需添加單元格即可創建行。如果添加的單元格數量等於預期的列數量,則會創建一行。因此,如果只添加一個單元格,則不會創建任何行。

此外,您使用ColSpan = 2,所以您只需要每行添加7列。

我改變RowSpan爲1,因爲它並沒有在這裏工作很好,當所有的細胞rowspanned,它沒有任何意義有也無妨跨越,所以......

這裏是我的修正,它創建了一個單排:

for (int i = 0; i < widths.Length/2; i++) 
{ 
    PdfPCell materia = new PdfPCell(new Phrase("MATERIA", font)); 
    materia.Rowspan = 1; 
    materia.Colspan = 2; 
    materia.HorizontalAlignment = 1; 
    materia.VerticalAlignment = 1; 
    table.AddCell(materia); 
}