我想仅使用一个单元格在同一行中添加图像和文本。我可以在一个单元格中添加图像和文本,但图像出现在新行中,而不是在文本旁边
最佳答案
引用 iText example :
public static void main(String[] args) throws Exception {
String dest = "sample.pdf";
String img1 = "sample.jpg";
File file = new File(dest);
file.getParentFile().mkdirs();
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Table table = new Table(UnitValue.createPercentArray(new float[]{1, 2}));
table.addCell(createImageCell(img1));
table.addCell(createTextCell("Some Text"));
doc.add(table);
doc.close();
}
public static Cell createImageCell(String path) throws MalformedURLException {
Image img = new Image(ImageDataFactory.create(path));
img.setWidth(UnitValue.createPercentValue(100));
Cell cell = new Cell().add(img);
cell.setBorder(null);
return cell;
}
public static Cell createTextCell(String text) {
Cell cell = new Cell();
Paragraph p = new Paragraph(text);
p.setTextAlignment(TextAlignment.RIGHT);
cell.add(p).setVerticalAlignment(VerticalAlignment.BOTTOM);
cell.setBorder(Border.NO_BORDER);
return cell;
}
关于java - 使用 itext 在一个单元格中的同一行中显示图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138325/