java itext columntext,在文本模式下使用ColumnText时如何在iText中添加嵌入式图像

private void processImage(Phrase phrase, Picture picture, ColumnText column)

{

// TODO Auto-generated method stub

byte[] pictureData = picture.getContent();

float ls = 12.0f;

float multiline = 1.0f;

column.setLeading(ls, multiline);

pictureData = WordToPdfUtils.getMetaFileAsImage(pictureData);

if (pictureData != null) {

try {

Image pic = Image.getInstance(pictureData);

float[] scwh = scaleInlinePicture(picture);

pic.scaleAbsolute(scwh[0], scwh[1]);

phrase.add(new Chunk(pic, 0, 0, true));

column.addText(phrase);

} catch (BadElementException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

When i add phrase containing inline image to the ColumnText, the image appears to overlap on the above paragraphs in the generated pdf, how to add inline images while working with ColumnText.

A link to sample pdf file generated with the issue is as below

解决方案

Please take a look at the ColumnTextChunkImage example. It adds a text containing images wrapped in a Chunk. The leading is adapted automatically based on the height of the images:

zNEyp.png

The code to achieve this is very similar to yours:

Image dog = Image.getInstance(DOG);

Image fox = Image.getInstance(FOX);

Phrase p = new Phrase("quick brown fox jumps over the lazy dog.");

p.add("Or, to say it in a more colorful way: quick brown ");

p.add(new Chunk(fox, 0, 0, true));

p.add(" jumps over the lazy ");

p.add(new Chunk(dog, 0, 0, true));

p.add(".");

ColumnText ct = new ColumnText(writer.getDirectContent());

ct.setSimpleColumn(new Rectangle(50, 600, 400, 800));

ct.addText(p);

ct.go();

The only apparent difference I see, is that you use:

column.add(phrase);

This method doesn't exist in the official version of iText. You either have addText() (text mode) or addElement() (composite mode). My guess is that you're using an unofficial version of iText. Unfortunately some individuals have created forks of iText using the FFF principle: Fork, F***, Forget. If you are using such a fork and you don't want to be f***ed or forgotten, please switch to using an official version of iText.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值