Nirose Basnyat
Nirose Basnyat

Reputation: 21

iTextSharp doesn't show all columns

I recently started using itextsharp and have been using it to create a PDF report using web service in asp.net. My code in the web service is shown below. My problem is, it does not show the first 3 columns data.

I think the problem is in the dt.Rows.

        string[] strFile = Directory.GetFiles(strUploadPath);

        Array.ForEach(Directory.GetFiles(strUploadPath), File.Delete);

        Document document = new Document();
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strUploadPath + "/" + strFilename, FileMode.Create));
        document.Open();
        Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);
        PdfPTable table = new PdfPTable(dt.Columns.Count);
        float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 200;
        PdfPCell cell = new PdfPCell(new Phrase());
        int j = 1;
        foreach (DataColumn c in dt.Columns)
        {
            if (j <= (dt.Columns.Count))
            {

                //table.AddCell(new Phrase(c.ToString(),font5));
                //table.AddCell(new Phrase(j.ToString(),font5));
                table.AddCell(new Phrase(c.ColumnName, font5));
            }
            j++;
        }

        int k = 1;
        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(k.ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
                table.AddCell(new Phrase(r[3].ToString(), font5));
                table.AddCell(new Phrase(r[4].ToString(), font5));
                table.AddCell(new Phrase(r[5].ToString(), font5));
               // table.AddCell(new Phrase(r[6].ToString(), font5));
                //                table.AddCell(new Phrase(r[7].ToString(), font5));

            }
            k++;
        }
        document.Add(table);
        document.CloseDocument();
        document.Close();
        return strFilename;
    }
    else
    {
        return null;
    }

Upvotes: 2

Views: 317

Answers (1)

mkl
mkl

Reputation: 95908

The code of the OP contains this line:

table.WidthPercentage = 200;

This causes the table to be twice (200%) as wide as the page minus margins. Thus, it is partially hidden.

When setting the table width using the WidthPercentage property, one usually shouldn't set it above 100.

As the OP meanwhile confirmed, the appropriate change causes the code to work properly.

Upvotes: 1

Related Questions