Trimack
Trimack

Reputation: 4213

itextsharp pdfpcell header

I'd like to have a table of cells of class PdfPCell each with a tiny header, main string and tiny footer. I can't find a way to insert them since HeaderandFooter is not allowed element to add to the cell, one paragraph overwrites another and so on. Any ideas?

Thanks in advance

Upvotes: 3

Views: 13725

Answers (1)

manji
manji

Reputation: 47978

You can use nested tables.
Instead of a PdfPCell, insert a 1x1 table with a tiny header & a tiny footer.

EDIT:

let's forget about table footer and header feature of iTextSharp because it's useful when a table spans over multiple pages and then you have footer & header repeated. In our case, header & footer will belong to the inner table that will contain only 3 cells, so let's use PdfPCell for all of them.

Then main function is GetHFCell that will return a PdfPTable containing a customized header cell (height, font, text,..), a customized footer cell & a middle cell containing the main text. This function is called whenever we want to add a cell to our main table (example of how to use this function in GeneratePDF).

    private static PdfPTable GetHFCell(string header, string footer, string text)
    {
        PdfPTable pdft;
        PdfPCell hc;
        PdfPCell fc;

        pdft = new PdfPTable(1);
        pdft.WidthPercentage = 100f;
        pdft.DefaultCell.Border = 0;

        hc = new PdfPCell(new Phrase(header));
        hc.Top = 0f;
        hc.FixedHeight = 7f;
        hc.HorizontalAlignment = 1;
        hc.BackgroundColor = iTextSharp.text.Color.ORANGE;
        ((Chunk)(hc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(hc.Phrase[0])).Font.Family, 5f);

        fc = new PdfPCell(new Phrase(footer));
        hc.Top = 0f;
        fc.FixedHeight = 7f;
        hc.HorizontalAlignment = 1;
        fc.BackgroundColor = iTextSharp.text.Color.YELLOW;
        ((Chunk)(fc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(fc.Phrase[0])).Font.Family, 5f);

        pdft.AddCell(hc);
        pdft.AddCell(text);
        pdft.AddCell(fc);

        return pdft;
    }

    public void GeneratePDF()
    {
        Document document = new Document();
        try
        {            
            PdfWriter.GetInstance(document, new FileStream("File1.pdf", FileMode.Create));

            document.Open();

            PdfPTable table = new PdfPTable(5);
            table.DefaultCell.Padding = 0;
            table.DefaultCell.BorderWidth = 2f;
            for (int j = 1; j < 6; j++)
            {
                for (int i = 1; i < 6; i++)
                {
                    //calling GetHFCell
                    table.AddCell(
                        GetHFCell("header " + ((int)(i + 5 * (j - 1))).ToString(), 
                                  "footer " + ((int)(i + 5 * (j - 1))).ToString(), 
                                  "z" + j.ToString() + i.ToString()));
                }
            }

            document.Add(table);
        }
        catch (DocumentException de)
        {
            //...
        }
        catch (IOException ioe)
        {
            //...
        }
        document.Close();
    }

Upvotes: 6

Related Questions