Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向iTextSharp中的pdf单元格添加页眉、页脚和背景色_C#_Pdf_Itextsharp - Fatal编程技术网

C# 向iTextSharp中的pdf单元格添加页眉、页脚和背景色

C# 向iTextSharp中的pdf单元格添加页眉、页脚和背景色,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我正在使用iTextSharp的PDF文档创建器。我想添加标题,它是一个文本和它下面的一行,然后是要显示的表格。页脚应在右下角包含页码。我可以创建表,但页眉和页脚似乎是个问题。有人能给出一个示例代码来解决这个问题吗。我还想给表中的标题列上色,即只给第一行的标题列上色。我是新来的,所以请帮帮我。提前感谢。请查看与iText相关的列表主题。现在选择关键字。您将发现页眉和页脚是使用页面事件添加的 这就是它的工作原理:创建文档时,可以向文档中添加对象,而不必担心这些对象在不同页面上的分布。例如:PdfP

我正在使用iTextSharp的PDF文档创建器。我想添加标题,它是一个文本和它下面的一行,然后是要显示的表格。页脚应在右下角包含页码。我可以创建表,但页眉和页脚似乎是个问题。有人能给出一个示例代码来解决这个问题吗。我还想给表中的标题列上色,即只给第一行的标题列上色。我是新来的,所以请帮帮我。提前感谢。

请查看与iText相关的列表主题。现在选择关键字。您将发现页眉和页脚是使用页面事件添加的

这就是它的工作原理:创建文档时,可以向文档中添加对象,而不必担心这些对象在不同页面上的分布。例如:PdfPTable在不适合某一页时被拆分,不适合的部分被转发到下一页

每次发生这种情况时,iText都会调用一个事件。如前所述,在OnStartPage方法中添加内容是危险的,因此如果要添加页眉和页脚,则需要在OnEndPage事件中添加

让我们从我的书中选取一个C示例。在中,我们创建如下页面事件:

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方法可能与您无关:它在事件中存储章节的标题,并将该标题用作标题。您应该感兴趣的部分可以在OneDPage方法中找到。每次页面结束时,我们都会在绝对位置添加内容

您希望内容是一个表吗?没问题,这正是示例中所做的

创建页面事件后,实现OnEndPage方法,只需向PdfWriter声明事件:

给表格中的细胞背景上色是一件很容易的事。这一点在本文中进行了解释


我假设你只是为了我们的信息添加了这句话,这不是一个实际的问题,但我还是回答了它,以防设置单元格的背景颜色对你来说不是一件小事。当然,您在查找创建PDF的示例时没有遇到任何问题,例如PDF中重复的页眉和页脚行,其中页眉和页脚单元格具有不同的颜色。

我无法使用BaseColor。它说在当前上下文中不存在。似乎您使用的是过时版本的iText。请升级。您当前可能正在使用不受支持的iTextSharp分叉。基色是在ITEXT5中引入的:
TableHeader tevent = new TableHeader();
writer.PageEvent = tevent;
PdfPCell cell = new PdfPCell(new Phrase("red cell"));
cell.BackgroundColor = BaseColor.RED;