How to display image and text beside each other Itext

前端 未结 2 1516
予麋鹿
予麋鹿 2021-01-19 06:24

Hi I am trying to create an Invoice where the company logo and the address are displayed next to each other. The company logo is displayed on the left and the text beneath i

2条回答
  •  粉色の甜心
    2021-01-19 07:06

    In my case, I was working in existing functionality and had to do so with in a cell. So here is what I did

    public PdfPCell buildDesiredCell() throws Exception {
    
    PdfPCell cell = new PdfPCell();
    
    PdfPTable headerTable = new PdfPTable(2);
    headerTable.setWidthPercentage(100f);
    
    PdfPCell leftCell = new PdfPCell();
    String imagePath; 
    imagePath = D:\....;//specify any path
    
    Image img = Image.getInstance(imagePath); 
    
    img.scaleAbsoluteHeight(120f);
    img.scaleAbsoluteWidth(120f);
    img.setAlignment(Element.ALIGN_LEFT);
    leftCell.addElement(img);
    leftCell.setBorder(Rectangle.NO_BORDER);
    
    headerTable.addCell(leftCell);
    
    PdfPCell rightCell = new PdfPCell();
    Paragraph addText= new Paragraph("  This is required text", smallStandardFont);
    rightCell .addElement(addText);
    
    rightCell .setBorder(Rectangle.NO_BORDER);
    rightCell .setPaddingLeft(5);
    headerTable.addCell(rightCell);
    
    cell.setBorder(Rectangle.NO_BORDER);
    cell.addElement(headerTable);
    
    return cell;
    
    
    }
    

提交回复
热议问题