Java 使用iText的PDF页脚中图像上的文本
我正在尝试在我的PDF页脚中添加图像和页码。我的问题是我无法在图像上显示页码。下面是我的代码Java 使用iText的PDF页脚中图像上的文本,java,pdf,itext,footer,Java,Pdf,Itext,Footer,我正在尝试在我的PDF页脚中添加图像和页码。我的问题是我无法在图像上显示页码。下面是我的代码 public void onEndPage(PdfWriter writer, Document document) { int pageNo=writer.getPageNumber()-this.pageNumber+1; Integer dummy=pageNo; String pageNoString=dummy.toString(); PdfContentByt
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.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();
这将首先添加文本,然后使用包含图像的表覆盖文本,因此图像覆盖文本
正如我在评论中所说,您应该使用基本逻辑并尝试以下方法:
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();
ColumnText.showTextAligned(cbb, Element.ALIGN_LEFT, new Phrase(pageNoString,FontFactory.getFont(FontFactory.COURIER,12,new BaseColor(0xFF, 0x00, 0x00))), 50, 95, 0);
现在,首先添加带有图像的表格,并将文本写在图像的顶部。您为什么要以艰难的方式添加文本?为什么不使用
ColumnText.showAligned()
,这样更容易定位文本(无需计算正弦和余弦)。此外,如果不使用go()
方法,我看不出使用column
有什么意义。在复合模式下使用ColumnText
时,还可以定义对齐方式(而这仅在文本模式下有效)。最后:为什么您认为使用元素.ALIGN_BOTTOM
可以与setSimpleColumn()配合使用。很抱歉,您的代码无法修复。请从头开始。我忘了添加列。go()代码>在此代码中。我已经更新了我的代码。我需要帮助在图片上方显示页码no。我的其他评论呢?另外,想想福尔摩斯和沃森:基本逻辑可以帮助你。如果先添加文本,然后添加图像,图像覆盖文本不是很正常吗?为什么不切换添加文本和图像,使文本覆盖图像。我真的希望你考虑扔掉你的代码,重新开始写一个体面的代码。setTextMatrix()
方法不应该被不理解参数含义的人使用。使用ColumnText.showAligned()
阅读您的代码的人会感谢您!我使用ColumnText.showTextAligned()
更新了代码,但页码仍在图像后面。请查看我的代码