PdfTable 未添加到我的文档中

发布于 2024-11-08 19:59:06 字数 1872 浏览 0 评论 0原文

代码如下:

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

Here's the code:

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

When I open the generated PDF file, no table is added to the document at all. Only the paragraphs are. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-11-15 19:59:06

您创建一个包含 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);
}

You create a table with 14 columns, but only add one. In PdfPTable you create rows by just adding cells. If number of cells added is equal to the expected column number, a row is created. So if you only add one cell, no row is created.

Furthermore, you use ColSpan = 2, so you only need to add 7 columns per row.

I changed RowSpan to 1 as it didn't work well here when all the cells were rowspanned, it didn't make sense to have it spanned anyway, so...

Here's my modification which creates a single row:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文