Itextpdf:在文本中间设置图像 [英] Itextpdf : set image in middle of text

查看:208
本文介绍了Itextpdf:在文本中间设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在段落中有一段文字我希望在文本中间设置一个图像:

  public void createPdf(String dest ,String imgSource)抛出IOException,DocumentException {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc,new FileOutputStream(dest));
doc.open();
段落p =新段落();
Image image1 = Image.getInstance(imgSource);
p.add(new Chunk(这是我的照片:));
p.add(image1);
p.add(new Chunk(so beautifull :)));
doc.add(p);
doc.close();
}

这是一张小图片(宽度= 100,身高= 50),但是我的形象坐在第二行。有可能这样设置:这是我的照片:[IMAGE]如此美丽:)

解决方案

如果你换行Chunk中的Image对象,您可以将其用作内联元素:

 段落p = new Paragraph(); 
Image image1 = Image.getInstance(imgSource);
p.add(new Chunk(这是我的照片:));
p.add(new Chunk(image1,0,0,true));
p.add(new Chunk(so beautifull :)));

该Chunk构造函数的第2和第3个参数可用于水平和垂直偏移图像。 / p>

I have a text in paragraph I want set an image in the middle of text :

public void createPdf(String dest, String imgSource) throws IOException, DocumentException {
    Document doc = new Document ();
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(dest));
    doc.open ();
    Paragraph p = new Paragraph();
    Image image1 = Image.getInstance(imgSource);
    p.add(new Chunk("This is my photo : "));
    p.add (image1);
    p.add(new Chunk(" so beautifull :)"));
    doc.add(p);
    doc.close();
}

it is a small image (width=100, height=50), but my image is sit in second line. Is it possible to set like this : "This is my photo : [IMAGE] so beautifull :)"

解决方案

If you wrap the Image object in a Chunk, you can use it as an inline element:

Paragraph p = new Paragraph();
Image image1 = Image.getInstance(imgSource);
p.add(new Chunk("This is my photo : "));
p.add (new Chunk(image1, 0, 0, true));
p.add(new Chunk(" so beautifull :)"));

The 2nd and 3rd parameter of that Chunk constructor can be used to offset the image horizontally and vertically.

这篇关于Itextpdf:在文本中间设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆