java生成pdf如何让文字和图片显示在同一行

我用itext生成pdf,在pdf中写入了一个表格,在表格中有图片和文字,怎么让图片和文字在同一行显示

可以用表格布局
BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
Font fontChinese5 = new Font(bf,8);
PdfPTable table1 = new PdfPTable(2); //表格两列
table1.setHorizontalAlignment(Element.ALIGN_CENTER); //垂直居中
table1.setWidthPercentage(100);//表格的宽度为100%
float[] wid1 ={0.75f,0.25f}; //两列宽度的比例
table1.setWidths(wid1);
table1.getDefaultCell().setBorderWidth(0); //不显示边框

PdfPCell cell11 = new PdfPCell(new Paragraph("SilkRoad24 GmbH",fontChinese5)); table1.addCell(cell11);

String imagepath = "D:\\wl\\logo.png";
Image image = Image.getInstance(imagepath);
table1.addCell(image);
document.add(table1);//增加到文档中
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-12-04

今天刚好做了这个,也是困扰了很久。百度上基本找不到答案,还是bing了一下。

设置图片的时候,可以用new Chunk添加

Image img = Image.getInstance("/images/pdf/checked_12.png");

Paragraph paragraph = new Paragraph();
paragraph.add(new Chunk("this is text ..."));
paragraph.add(new Chunk(img, 0, 0, true)); // 图片和文案就会处在一行