【发布时间】:2014-09-21 15:42:44
【问题描述】:
我在android应用程序中使用itext创建了pdf页面,需要为pdf页面添加页眉和页脚,并且需要更改每个页面的页眉。
我确实喜欢添加页眉和页脚
公共类 HeaderAddFooter 扩展 PdfPageEventHelper {
private Phrase footer;
private Phrase header;
private String patientName;
private String patientID;
Font headerFont = new Font(FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
Font footerFont = new Font(FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
Font TitleFont = new Font(FontFamily.COURIER, 14, Font.NORMAL);
/*
* constructor
*/
public HeaderAddFooter(String name,String id) {
super();
header = new Phrase("***** Header *****");
footer = new Phrase("**** Footer ****");
patientName=name;
patientID=id;
}
@Override
public void onStartPage(PdfWriter writer, Document document) {
super.onStartPage(writer, document);
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Date date = new Date();
Calendar cal = Calendar.getInstance();
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("welcome",headerFont),
document.leftMargin()-1, document.top()+10, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("Name",headerFont),
document.right(), document.top()+15, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("Designation",headerFont),
document.right(), document.top()+1, 0);
/*
* Foooter
*/
ColumnText.showTextAligned(cb, Element.ALIGN_MIDDLE, new Phrase("Page:"+String.format(" %d ",
writer.getPageNumber()),footerFont),
document.right() - 280 , document.bottom() - 10, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_MIDDLE, new Phrase(dateFormat.format(cal.getTime()),footerFont),
document.right() - 350 , document.bottom() - 25, 0);
}
}
现在当向标题添加新值时,它会像合并文本一样显示以前的文本和新的标题文本。如何为每个页面动态更改标题
【问题讨论】:
-
当向标题添加新值时,它显示为合并先前文本和新标题文本的文本 - 如何添加新值?我在您的代码中没有看到它。
-
对于第一页它显示正确然后对于第二页需要在前一个标题上显示新标题时再次发送值
-
如何添加新值?我在您的代码中没有看到它。您说您的做法以某种方式混合了以前的和当前的标头。使用您显示的无法复制的代码。因此,我们无法告诉您您做错了什么。
-
向每个页面的构造函数发送值