Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

c# - PdfPTable as a header in iTextSharp

I need a table with about 12 cells to display as a header. The following code fails to do this. I am aware table2 does not have 12 cells. On the second page, only "testing" is displayed. What am I missing?

Thanks in advance!

Document document = new Document();

        try
        {
            PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create));
            document.Open();

            PdfPTable table = new PdfPTable(1);
            table.WidthPercentage = 100;
            PdfPTable table2 = new PdfPTable(2);

            //logo
            PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:pathofile.gif"));
            cell2.Colspan = 2;
            table2.AddCell(cell2);

            //title
            cell2 = new PdfPCell(new Phrase("
TITLE TEXT", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE)));
            cell2.HorizontalAlignment = Element.ALIGN_CENTER;
            cell2.Colspan = 2;
            table2.AddCell(cell2);

            PdfPCell cell = new PdfPCell(table2);
            table.HeaderRows = 1;
            table.AddCell(cell);
            table.AddCell(new PdfPCell(new Phrase("")));

            document.Add(table);

            document.Add(new Phrase("






































testing"));
        }
        catch (DocumentException de)
        {
            Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Console.Error.WriteLine(ioe.Message);
        }

        document.Close();
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Bingo! PdfPageEventHandler class worked for me.

I used the PdfPageEventHelper overriding the OnEndPage method:

class _events : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        PdfPTable table = new PdfPTable(1);
        //table.WidthPercentage = 100; //PdfPTable.writeselectedrows below didn't like this
        table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; //this centers [table]
        PdfPTable table2 = new PdfPTable(2);

        //logo
        PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:pathofile.gif"));
        cell2.Colspan = 2;
        table2.AddCell(cell2);

        //title
        cell2 = new PdfPCell(new Phrase("
TITLE", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE)));
        cell2.HorizontalAlignment = Element.ALIGN_CENTER;
        cell2.Colspan = 2;
        table2.AddCell(cell2);

        PdfPCell cell = new PdfPCell(table2);
        table.AddCell(cell);

        table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);
    }
}

then, build pdf

Document document = new Document(PageSize.A4, 36, 36, 36 + <height of table>, 36); // note height should be set here
_events e = new _events();
PdfWriter pw = PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create));
pw.PageEvent = e;
document.Open();

for(int i=0;i<100;i++)
{
    document.Add(new Phrase("TESTING
"));
}

document.Close();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...