【发布时间】:2014-05-12 15:43:57
【问题描述】:
我正在尝试在我的 PDF 页脚中添加图像和页码。我的问题是我无法在图像上显示页码。下面是我的代码。
public void onEndPage(PdfWriter writer, Document document) {
int pageNo=writer.getPageNumber()-this.pageNumber+1;
Integer dummy=pageNo;
String pageNoString=dummy.toString();
PdfContentByte cbb = writer.getDirectContent();
try {
ColumnText column = new ColumnText(cbb);
PdfPTable newtable = new PdfPTable(1);
newtable.setTotalWidth(530);
newtable.setLockedWidth(true);
Image img = Image.getInstance("C:/Users/sathesh/Desktop/Warfiles/PDFFiles/Footer.png");
PdfPCell imageCell=new PdfPCell();
PdfPCell textCell=new PdfPCell(new Paragraph("Disclaimer text",new Font(Font.FontFamily.COURIER, 6, Font.NORMAL)));
imageCell.setBorder(Rectangle.NO_BORDER);
textCell.setBorder(Rectangle.NO_BORDER);
imageCell.setImage(img);
ColumnText.showTextAligned(cbb,
Element.ALIGN_LEFT, new Phrase(pageNoString,FontFactory.getFont(FontFactory.COURIER,12,new BaseColor(0xFF, 0x00, 0x00))), 50, 95, 0);
newtable.addCell(imageCell);
newtable.addCell(textCell);
column.addElement(newtable);
column.setSimpleColumn(30, 130, 570, 45,5f, Element.ALIGN_RIGHT | Element.ALIGN_BOTTOM |Element.ALIGN_JUSTIFIED_ALL);
}
column.go();
catch(Exception e)
{
e.printStackTrace();
}
}
【问题讨论】:
-
你为什么要添加文本?为什么不使用
ColumnText.showAligned(),这样可以更轻松地定位文本(无需计算正弦和余弦)。此外,如果不使用go()方法,我看不到使用column的意义。您还可以在复合模式下使用ColumnText时定义对齐方式(而这仅适用于文本模式)。最后:您为什么认为使用Element.ALIGN_BOTTOM可以与setSimpleColumn()一起使用。抱歉,您的代码无法修复。请从头开始。 -
我忘记在此代码中添加
column.go();。我已经更新了我的代码。我需要帮助才能在图片上方显示页面。 -
我的所有其他 cmets 呢?另外,想想福尔摩斯和华生:基本逻辑可以帮助你。如果先加文字再加图片,图片覆盖文字不正常吗?为什么不切换添加文本和图像,以便文本覆盖图像。我真的希望你考虑扔掉你的代码并重新开始编写像样的代码。
setTextMatrix()方法不应该被不理解参数含义的人使用。使用ColumnText.showAligned(),必须阅读您的代码的人会感谢您! -
我已经使用
ColumnText.showTextAligned()更新了代码,但页码仍然在图像后面。请查看我的代码