Itextpdf : set image in middle of text

2019-07-12 02:03发布

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 :)"

2条回答
仙女界的扛把子
2楼-- · 2019-07-12 02:47

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.

查看更多
We Are One
3楼-- · 2019-07-12 02:52

Isn't it better for you to set a HTML output and convert this HTML to a PDF. As far as I'm concerned, that is an option with iText

For more information on how to do this, please refer to their website: http://itextpdf.com/product/xml_worker

查看更多
登录 后发表回答