Steve Staple
Steve Staple

Reputation: 3279

iTextSharp PdfPtable coming out blank

I have been racking my brains on this one all day.

This is the code:

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

I have commented out the actual values and substituted "z" to eliminate the possibility of bad input data. Every other table on the pdf is coming out fine. Just this one is missing.

I can't see any problem with the code. Maybe it is a bug in iTextSharp or VS. Any ideas?

Upvotes: 0

Views: 770

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

When I copy/paste your code, and I add cell numbers in comment, I get this:

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

You are creating a table with 10 columns:

PdfPTable table = new PdfPTable(10);

Yet you are only adding 9 cells. As a result, the table is not complete. As documented, incomplete rows are never shown.

Either change the table into a table with 9 columns, or add an extra cell (there's a method to do this automatically, if you don' want to add it yourself).

Upvotes: 1

Related Questions