java - 使用 iText 在 PDF 页脚中的图像上添加文本

标签 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();
    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);

现在首先添加带有图像的表格,并将文本写在图像顶部。

关于java - 使用 iText 在 PDF 页脚中的图像上添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793143/

相关文章:

javascript - 使用 Node(在 Windows 上)查找 PDF 页数

java - 如何从网络应用程序中预选特定打印机

java - 使用java sevlets和itext向pdf添加页码

java - 在 JavaSparkStreamingContext 中执行查询

Java 多线程 - 识别代码中的错误

java - 如何在 Eclipse 中创建 servlet 过滤器?

java - 修改当前代码以在 > 21 时拒绝用户输入

pdf - 如何从 ColdFusion 结构对象中获取值

java - 是否可以在没有数据库连接的情况下使用 iText 在 pdf 文件中进行页面转换(类似于 PowerPoint 演示文稿)?

java - 使用 iText,在内存中生成一个在磁盘上生成的 PDF