PdfTable不会被添加到我的文档 [英] PdfTable isn't added to my document

查看:177
本文介绍了PdfTable不会被添加到我的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的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);
}

当我打开生成的PDF文件,没有表被添加到文档的。只有段落。任何想法?

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

推荐答案

您有14列上创建一个表,但只添加一个。在 PdfPTable 您只需添加单元格中创建行。如果加入的细胞数是等于预期列数,行被创建。所以,如果你只添加一个单元格,没有行创建。

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.

此外,您可以使用 COLSPAN = 2 ,所以你只需要在每行增加7列。

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

我改变了 ROWSPAN 1,因为它没有工作时,这里所有的细胞rowspanned,它没有任何意义,反正它跨越了,所以。 ..

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

这篇关于PdfTable不会被添加到我的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆