【问题标题】:Text on image in PDF footer using iText使用 iText 在 PDF 页脚中的图像上的文本
【发布时间】: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() 更新了代码,但页码仍然在图像后面。请查看我的代码

标签: java pdf itext footer


【解决方案1】:

你有:

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);

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

【讨论】:

    猜你喜欢
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2016-06-30
    相关资源
    最近更新 更多