Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using a iTextsharp to create a table.I want a table at particular position in the page and I am using a table.WriteSelectedRows(0, -1, 300, 300, pcb); method but its not working. Here is my code.

           using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create))
           {
            Document doc = new Document(PageSize.A4);
            PdfWriter writer =  PdfWriter.GetInstance(doc, fs);
            doc.Open();
            PdfContentByte pcb = writer.DirectContent;
            PdfPTable table = new PdfPTable(9);
            table.TotalWidth = 595f;

            // Hdeaer row.
            table.AddCell(GetCell("Header 1", 1, 2));
            table.AddCell(GetCell("Header 2", 1, 2));
            table.AddCell(GetCell("Header 3", 5, 1));
            table.AddCell(GetCell("Header 4", 1, 2));
            table.AddCell(GetCell("Header 5", 1, 2));

            // Inner middle row.
            table.AddCell(GetCell("H1"));
            table.AddCell(GetCell("H2"));
            table.AddCell(GetCell("H3"));
            table.AddCell(GetCell("H4"));
            table.AddCell(GetCell("H5"));

            // Bottom row.
            table.AddCell(GetCell("D1"));
            table.AddCell(GetCell("D2"));
            table.AddCell(GetCell("D3"));
            table.AddCell(GetCell("D4"));
            table.AddCell(GetCell("D5"));
            table.AddCell(GetCell("D6"));
            table.AddCell(GetCell("D7"));
            table.AddCell(GetCell("D8"));
            table.AddCell(GetCell("D9"));
            table.WriteSelectedRows(0, -1, 300, 300, pcb);
            doc.Close();
   }
   private PdfPCell GetCell(string text)
    {
        return GetCell(text, 1, 1);
    }
    private PdfPCell GetCell(string text, int colSpan, int rowSpan)
    {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.HorizontalAlignment = 1;
        cell.Rowspan = rowSpan;
        cell.Colspan = colSpan;

        return cell;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
420 views
Welcome To Ask or Share your Answers For Others

1 Answer

You try to write the table before you've added any content to the table. It is normal that nothing is shown on the page. Move writeSelectedRows() down in your code:

PdfPTable table = new PdfPTable(9);
table.TotalWidth = 595;

// there isn't any content in the table: there's nothing to write yet

// Header row.
table.AddCell(GetCell("Header 1", 1, 2));
table.AddCell(GetCell("Header 2", 1, 2));
table.AddCell(GetCell("Header 3", 5, 1));
table.AddCell(GetCell("Header 4", 1, 2));
table.AddCell(GetCell("Header 5", 1, 2));

// Inner middle row.
table.AddCell(GetCell("H1"));
table.AddCell(GetCell("H2"));
table.AddCell(GetCell("H3"));
table.AddCell(GetCell("H4"));
table.AddCell(GetCell("H5"));

// Now we've added 2 rows: two rows will be shown:

table.WriteSelectedRows(0, -1, 300, 300, pcb);

Note that I've changed the number of columns in the table, because you're only adding 5 rows for the first row, and five rows for the second row. Note that incomplete rows aren't rendered, so that may be another reason why your code doesn't do what you expect.

Finally you should remove:

doc.Add(table);

This adds the table at the current position of the cursor in the page. That's probably not where you want it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...