How to display image and text beside each other Itext

前端 未结 2 1514
予麋鹿
予麋鹿 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 06:52

    People usually achieve this by using a PdfPTable. See for instance the ImageNextToText example:

    enter image description here

    This is the code that creates the PDF and the table with two columns:

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.setWidths(new int[]{1, 2});
        table.addCell(createImageCell(IMG1));
        table.addCell(createTextCell("This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013."));
        document.add(table);
        document.close();
    }
    

    This is the code that creates the cell with the image. Note that the true parameter will cause the image to scale. As we've defined that the first column takes half the width of the second column, the width of the image will take one third of the width of the complete table.

    public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
        Image img = Image.getInstance(path);
        PdfPCell cell = new PdfPCell(img, true);
        return cell;
    }
    

    This is one way to create the cell with the text. There are many different ways to achieve the same result. Just experiment with text mode and composite mode.

    public static PdfPCell createTextCell(String text) throws DocumentException, IOException {
        PdfPCell cell = new PdfPCell();
        Paragraph p = new Paragraph(text);
        p.setAlignment(Element.ALIGN_RIGHT);
        cell.addElement(p);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setBorder(Rectangle.NO_BORDER);
        return cell;
    }
    
    0 讨论(0)
  • 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;
    
    
    }
    
    0 讨论(0)
提交回复
热议问题