iTextSharp PdfP表格出现空白

本文关键字:空白 表格 PdfP iTextSharp | 更新日期: 2023-09-27 18:29:11

我一整天都在为这个绞尽脑汁。

这是代码:

    private void addFinalCheckdata()
    {
        PdfPTable table = new PdfPTable(10);
        float[] widths = new float[] { 60f, 60f, 60f, 60f, 60f, 60f, 60f, 60f, 260f, 60f };
        table.TotalWidth = 800f;
        table.SetWidths(widths);
        table.LockedWidth = true;
        table.HorizontalAlignment = 0;
        // Add Answers 
        //string value = _LGSRobj.finalCheck.SatisfactoryVisualInspection;
        PdfPCell cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.EmergencyControl;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.EarthBondingSatisfactory;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.NumberofAppliancesTested;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.serviceTimerSet;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.StartPressure;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.FinishPressure;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.Pass;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.InstallationCaped;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        table.AddCell(cell);
        _document.Add(table);
    }

我已经注释掉了实际值,并用"z"代替,以消除输入数据不正确的可能性。pdf上的其他表格都很好。只有这一个不见了。

我看不出代码有任何问题。也许这是iTextSharp或VS中的一个错误。有什么想法吗?

iTextSharp PdfP表格出现空白

当我复制/粘贴你的代码,并在注释中添加单元格编号时,我得到的是:

    private void addFinalCheckdata()
    {
        PdfPTable table = new PdfPTable(10);
        float[] widths = new float[] { 60f, 60f, 60f, 60f, 60f, 60f, 60f, 60f, 260f, 60f };
        table.TotalWidth = 800f;
        table.SetWidths(widths);
        table.LockedWidth = true;
        table.HorizontalAlignment = 0;
        // Add Answers 
        //string value = _LGSRobj.finalCheck.SatisfactoryVisualInspection;
        PdfPCell cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 1
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.EmergencyControl;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 2
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.EarthBondingSatisfactory;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 3
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.NumberofAppliancesTested;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 4
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.serviceTimerSet;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 5
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.StartPressure;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 6
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.FinishPressure;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 7
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.Pass;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 8
        table.AddCell(cell);
        //value = _LGSRobj.finalCheck.InstallationCaped;
        cell = new PdfPCell(new Phrase("z", _fntNormal7));
        // 9
        table.AddCell(cell);
        _document.Add(table);
    }

您正在创建一个包含10列的表:

PdfPTable table = new PdfPTable(10);

然而,您只添加了9个单元格。因此,该表并不完整。如文档所示,从不显示不完整的行。

要么将表更改为具有9列的表,要么添加一个额外的单元格(如果您不想自己添加,有一种方法可以自动执行此操作)。