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 it. I tried to display the text next to the logo to the FAR RIGHT but it didnt come.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();
}
}
People usually achieve this by using a PdfPTable
. See for instance the ImageNextToText example:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With