为什么没有添加这些表格单元格?

发布于 2024-11-08 05:22:25 字数 6210 浏览 0 评论 0原文

我正在尝试使用 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);
            }
        }
    }

这是它如何结束的图片: 因此它正确添加了最后一列 com,还添加了空白填充单元格,然后它不添加接下来的内容。它只是不显示。有什么建议吗?

在此处输入图像描述

I'm trying to create a pdf report using iTextSharp and I'm stumped as to why it's not correctly going to the next row to add the cells.

Here's the code:

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

Here's a picture of how it ends up: So it add the last column com correctly, also adds the blank padding cell, then it doesn't add what comes next. It just doesn't display. Any suggestions?

enter image description here

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

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

发布评论

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

评论(2

晚风撩人 2024-11-15 05:22:25

您需要为每行添加确切的列数才能显示它。

请参阅我对您的其他问题的回答PdfTable 未添加到我的文档中

You need to add exact number of columns for each row to have it shown.

See my answer for your other question PdfTable isn't added to my document

无所的.畏惧 2024-11-15 05:22:25

试试这个

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

try this

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