使用飞碟(xHTMLRenderer)和iText生成页眉/页脚

我意识到之前已经问过这个问题(我查看了所有的解决方案并尝试了所有这些)但我仍然在尝试生成一个带有页眉和页脚的pdf文档,并在每个页面上重复。

我正在使用flying saucer R8和iText2.0.8我已经尝试了许多不同的方法来使它工作,但到目前为止无济于事。 我测试的一些方法是https://gist.github.com/626264 ,使用运行元素和边距框http://pigeonholdings.com/projects/flyingsaucer/R8/doc/guide/users-guide-R8.html# xil_40 (css3function),飞碟r7指南,不适用于r8 http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer -and-itext.html #page-specific-features ,很多其他方法对我不起作用。

我的标题div包含2个带图像的div和我的页脚仅用于页面编号。 html被放入一个名为buf的StringBuffer

 buf.append(""); buf.append("blabla "); buf.append(" "); buf.append("@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } } "); buf.append("#footer { font-size: 90%; font-style: italic; position: running(footer); top: 0; left: 0; }"); buf.append("#pagenumber:before { content: counter(page); } "); buf.append("#pagecount:before { content: counter(pages); } "); buf.append(""); buf.append(""); buf.append("
"); buf.append(""); buf.append(""); buf.append("

alt text

"); buf.append("
"); buf.append("Some texy text"); buf.append("
");); buf.append("
"); buf.append("


"); buf.append(""); buf.append(""); buf.append("");

我的pdf生成正常,除了标题只出现在第一页,页脚只出现在最后一页的底部。 当我把html通过w3cvalidation器时它出来很好,但当我使用他们的CSSvalidation器时,它说它们是@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } }行中的解析错误@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } } @page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } }

据我所知,我读到的所有指南都很好。 我还听说W3C CSSvalidation器对于CSS3规范是不完整的,所以我认为它是validation器错了。

如果有人能给我一些关于在哪里寻找或想法的提示,那将使我的一周:)

ps必须使用飞碟R8和/或iText 2.0.8

这是一个工作示例:

 package com.sg2net.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.DocumentException; public class XHTMLRenderer8 { /** * @author Giovanni Cuccu */ public static void main(String[] args) throws FileNotFoundException, DocumentException { ITextRenderer renderer = new ITextRenderer(); String content="\n" + "
Header
Page1
Page2
"; renderer.setDocumentFromString(content); renderer.layout(); renderer.createPDF(new FileOutputStream("test.pdf")); } }

这是使用以下XHTML文档

      
Header
Page1
Page2

经过大量的研究和测试,我想出了一个真正有效的解决方案。

你可以控制:

– 编辑margin-top的标题高度;

– 通过编辑边距底部的页脚高度;

– 通过编辑div.content宽度的内容宽度。

页码显示在页脚。

见下面的代码:

      
This is the header that will repeat on every page at top

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

This is the content

希望能帮助到你!!!

对于使用java checkout的itext

 public class HeaderAndFooter extends PdfPageEventHelper { public void onEndPage (PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); switch(writer.getPageNumber() % 2) { case 0: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("even header"), rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0); break; case 1: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())), 300f, 62f, 0); break; } ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())), (2f + 4f) / 2, 2f - 18, 0); } } 

在你的pdfwriter中使用下面的一个

 HeaderAndFooter event = new HeaderAndFooter(); writer.setPageEvent(event);