iTextSharp: Page header out of alignment with large tables

痞子三分冷 提交于 2019-12-02 09:41:44

In OnStartPage and OnEndPage you are not supposed to add anything to the given Document instance. Instead headers and footers shall be added to the direct content of the given PdfWriter. Cf. The iText(Sharp) samples, e.g.

  /**
   * Adds the header and the footer.
   * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
   *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
   */
  public override void OnEndPage(PdfWriter writer, Document document) {
    Rectangle rect = writer.GetBoxSize("art");
    switch(writer.PageNumber % 2) {
    case 0:
      ColumnText.ShowTextAligned(writer.DirectContent,
        Element.ALIGN_RIGHT, 
        header[0],
        rect.Right, rect.Top, 0
      );
      break;
    case 1:
      ColumnText.ShowTextAligned(
        writer.DirectContent,
        Element.ALIGN_LEFT,
        header[1],
        rect.Left, rect.Top, 0
      );
      break;
    }
    ColumnText.ShowTextAligned(
      writer.DirectContent,
      Element.ALIGN_CENTER, 
      new Phrase(String.Format("page {0}", pagenumber)),
      (rect.Left + rect.Right) / 2, 
      rect.Bottom - 18, 0
    );
  }

(from MovieHistory2.cs, a C#'ified sample from chapter 5 of iText in Action — 2nd Edition)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!