PdfTable 未添加到我的文档中

本文关键字:文档 我的 添加 PdfTable | 更新日期: 2023-09-27 17:57:10

这是代码:

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文件时,根本没有向文档添加任何表格。只有段落是。有什么想法吗?

PdfTable 未添加到我的文档中

您创建了一个包含 14 列的表,但只添加一列。在PdfPTable中,您只需添加单元格即可创建行。如果添加的单元格数等于预期的列号,则创建一行。因此,如果只添加一个单元格,则不会创建任何行。

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

我将 RowSpan 更改为 1,因为当所有单元格都跨行时,它在这里效果不佳,无论如何跨越它都没有意义,所以......

这是我创建单行的修改:

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);
}