【发布时间】:2014-06-05 07:17:06
【问题描述】:
我将 iTextSharp 用于 PDF 文档创建器。我想添加标题,它是一个文本和它下面的一行,然后是要出现的表格。页脚应包含右下角的页码。我能够创建表格,但页眉和页脚似乎是一个问题。有人可以给出一个示例代码来解决这个问题。我还想为表中的标题列着色,即只有第一行的标题列具有背景颜色。我是新手,所以请帮帮我。提前致谢。
【问题讨论】:
标签: c# pdf itextsharp
我将 iTextSharp 用于 PDF 文档创建器。我想添加标题,它是一个文本和它下面的一行,然后是要出现的表格。页脚应包含右下角的页码。我能够创建表格,但页眉和页脚似乎是一个问题。有人可以给出一个示例代码来解决这个问题。我还想为表中的标题列着色,即只有第一行的标题列具有背景颜色。我是新手,所以请帮帮我。提前致谢。
【问题讨论】:
标签: c# pdf itextsharp
请查看keywords list 列出与 iText 相关的主题。现在选择关键字headers / footers。您会发现页眉和页脚是使用页面事件添加的。
它是这样工作的:当您创建一个文档时,您可以将对象添加到文档中,而不必担心这些对象在不同页面上的分布。例如:PdfPTable 放不下一个页面时会被拆分,放不下的部分会被转发到下一页。
每次发生这种情况时,iText 都会调用一个事件。如文档所述,在 OnStartPage() 方法中添加内容是危险的,因此如果要添加页眉和页脚,则需要在 OnEndPage() 事件中进行。
让我们从我的书中的Chapter 5 中选择一个 C# 示例。在MovieHistory2 中,我们创建了一个这样的页面事件:
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() 方法中找到。每次页面结束时,我们都会在绝对位置添加内容。
您希望内容是表格吗?没问题,这正是 MovieCountries1 示例中所做的。
一旦您创建了页面事件,实现OnEndPage() 方法,您所要做的就是向PdfWriter 声明事件:
TableHeader tevent = new TableHeader();
writer.PageEvent = tevent;
为表格中的单元格背景着色是一件轻而易举的事。这在chapter 4 中有解释。
PdfPCell cell = new PdfPCell(new Phrase("red cell"));
cell.BackgroundColor = BaseColor.RED;
我假设您只是为我们的信息添加了该注释并且这不是一个实际问题,但无论如何我还是回答了它,以防设置单元格的背景颜色对您来说不是微不足道的。当然,您可以毫无问题地找到创建像 this one 这样的 PDF 的示例,这是一个带有重复页眉和页脚行的 PDF,其中页眉和页脚单元格的颜色不同。
【讨论】: