Header, footer and large tables with iTextSharp

后端 未结 1 908
滥情空心
滥情空心 2020-12-19 08:09

I\'ve added an header and a footer on my document by PdfPageEventHelper.

The document has several \"large\" tables populated at runtime.

I\'ve tried to add t

相关标签:
1条回答
  • 2020-12-19 08:41

    In your OnEndPage method you have this line:

    head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight - 30, writer.DirectContent);
    

    That code correctly calculates where to put content based on the page's height and top margin but also includes a magical 30 in there which is causing the header to be drawn on top of the table. Change it to this and your header will be fine.

    head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight, writer.DirectContent);
    

    I'm guessing that that 30 is trying to include some padding between your header and the table itself. What I would recommend is actually changing the document's margins themselves in the main code:

    document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin);
    

    And then accounting for that in the OnEndPage method:

    float cellHeight = document.TopMargin - 30;
    

    Your footer code doesn't actually account for the bottom margin and just draws it at 50 so this will always overlap. The quick fix would be to change it to:

    footer.WriteSelectedRows(0, -1, 10, footer.TotalHeight, writer.DirectContent);
    

    This will at least get the footer bottom-aligned. If you want some more padding like above just adjust the document margins again:

    document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin + 30);
    
    0 讨论(0)
提交回复
热议问题