簡體   English   中英

使用iText在PDF頁腳中的圖像上的文本

[英]Text on image in PDF footer using iText

我試圖在我的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);

現在,首先添加帶有圖像的表,並將文本寫在圖像的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM