添加页眉,页脚和背景颜色以PDF格式iTextSharp的细胞 [英] add header, footer and Background color to pdf cells in iTextSharp

查看:364
本文介绍了添加页眉,页脚和背景颜色以PDF格式iTextSharp的细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iTextSharp的对PDF文档创世IM。我想添加页眉这是一个文本和它下面一条线,然后出现在表中。页脚应包含在右下角的页码。 IM能够创建表,但页眉和页脚似乎是一个问题。有人可以举一个例子代码来解决这个问题。还我想要的颜色的标题表格中的列即仅第一行的标题列有一个背景颜色。即时通讯新本,请帮助我。先谢谢了。

im using iTextSharp for PDF Doc creater. i want to add header which is a text and a line below it and then the table to appear. the footer should contain the page number at the bottom right. im able to create the table but the header and footer seems to be an issue. can someone give an example code to solve this issue. also i want to color the header columns in the table i.e the heading columns of only the first row to have a background color. im new to this so please help me out. thanks in advance.

推荐答案

请检查的关键字列表列出相关的iText的主题。现在选择关键字页眉/页脚。你会发现,页眉和页脚使用页面事件添加

Please check the keywords list listing topics that are related to iText. Now select the keyword headers / footers. You'll discover that headers and footers are added using page events.

这是如何工作的:当你创建一个文档,将对象添加到文档中,而不必担心这些对象在不同页的分发。例如:一个 PdfPTable 是分裂时,它不适合一个页面,不适合被转发到下一个页面的一部分

This is how it works: when you create a document, you add objects to the document without worrying about the distribution of these objects over different pages. For instance: a PdfPTable is split when it doesn't fit a page, and the part that doesn't fit is forwarded to the next page.

每一次发生这种情况,iText的调用的事件。如记录,这是危险的添加在的OnStartPage()方法内容,因此,如果您要添加页眉和页脚,你需要在<$这样做C $ C>的OnEndPage()事件。

Every time this happens, iText calls an event. As documented, it is dangerous to add content in the OnStartPage() method, so if you want to add a header and a footer, you need to do so in the OnEndPage() event.

让我们从挑选的第5章我的书。在 MovieHistory2 ,我们创建这样一个页面事件:

Let's pick a C# example from Chapter 5 of my book. In MovieHistory2, we create a page event like this:

class HeaderFooter : PdfPageEventHelper {
  /** Alternating phrase for the header. */
  Phrase[] header = new Phrase[2];
  /** Current page number (will be reset for every chapter). */
  int pagenumber;

  /**
   * Initialize one of the headers.
   * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
   *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
   */
  public override void OnOpenDocument(PdfWriter writer, Document document) {
    header[0] = new Phrase("Movie history");
  }

  /**
   * Initialize one of the headers, based on the chapter title;
   * reset the page number.
   * @see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter(
   *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float,
   *      com.itextpdf.text.Paragraph)
   */
  public override void OnChapter(
    PdfWriter writer, Document document,
    float paragraphPosition, Paragraph title) 
  {
    header[1] = new Phrase(title.Content);
    pagenumber = 1;
  }

  /**
   * Increase the page number.
   * @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(
   *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
   */
  public override void OnStartPage(PdfWriter writer, Document document) {
    pagenumber++;
  }

  /**
   * 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
    );
  }
}



请注意, OnChapter( )方法可能不相关的你:它存储在事件章的标题,并使用该标题为标题。应该感兴趣的部分,可以在的OnEndPage()方法被发现。每一个页面结束的时候,我们在绝对位置添加内容。

Note that the OnChapter() method may not be relevant for you: it stores the title of a chapter in the event and uses that title for the header. The part that should interest you, can be found in the OnEndPage() method. Every time a page ends, we add content at an absolute position.

您想要的内容必须是一个表?没问题,这正是在的 MovieCountries1 例子。

You want the content to be a table? No problem, that's exactly what is done in the MovieCountries1 example.

一旦你创建你的页面时,实施的OnEndPage()方法,你所要做的,就是宣布该事件的 PdfWriter

Once you've created your page event, implementing the OnEndPage() method, all you have to do, is to declare the event to the PdfWriter:

TableHeader tevent = new TableHeader();
writer.PageEvent = tevent;



着色表中的单元格的背景是一个没有脑子。这在第4章解释说。

PdfPCell cell = new PdfPCell(new Phrase("red cell"));
cell.BackgroundColor = BaseColor.RED;



我假设你刚刚添加这句话对我们的信息,它是不是一个实际的问题,但无论如何我回答了在情况下设置单元格的背景颜色是不平凡的你。当然,你有没有问题,发现,创造如这个例子其中是重复的页眉和页脚行,其中的页眉和页脚的细胞有不同的颜色的PDF文件。

I assume that you just added that remark for our information and that it wasn't an actual question, but I answered it anyway in case that setting the background color of a cell wasn't trivial to you. Surely you had no problem finding the examples that create PDFs like this one which is a PDF with repeating header and footer rows where the header and footer cells have a different color.

这篇关于添加页眉,页脚和背景颜色以PDF格式iTextSharp的细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆