How to display image and text next to each other.

Hey. I am trying to create an invoice where the company logo and address are displayed next to each other. The company logo appears on the left and the text below it. I tried to display text next to the FAR RIGHT logo, but it didn't appear. Please, help.

public class Test {
    /** Path to the resulting PDF */
    public static final String RESULT = "C:/ex/test.pdf";

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new ComCrunchifyTutorials().createPdf(RESULT);
    }

    /**
     * Creates a PDF with information about the movies
     * @param    filename the name of the PDF file that will be created.
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename)
        throws IOException, DocumentException {

        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(filename));

        document.open();

       Image image = Image.getInstance("C:/Users/user/Desktop/New folder (3)/shoes/shoes/web/images/abc.jpg");
                                                        image.scaleAbsolute(150f, 50f);//image width,height   


Paragraph p = new Paragraph();
Phrase pp = new Phrase(200);
p.add(new Chunk(image, 0, 0));
pp.add(" a text after the image.");

p.add(pp);
document.add(p);

        document.close();
    }

}

      

+3


source to share


2 answers


Usually people achieve this by using PdfPTable

. For example, ImageNextToText example :

enter image description here

This is the code that creates a PDF and a two-column table:

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 image cell. Note that the parameter true

will scale the image. Because we have determined that the first column is half the width of the second column, the image will be one third of the width of the full 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 a cell with 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;
}

      

+9


source


In my case, I was working on an existing functionality and had to do it in a cell. So here's 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


source







All Articles