iText-使用块添加外部图像 [英] iText - Adding external image using Chunk

查看:152
本文介绍了iText-使用块添加外部图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iText的新手,面对一个有关在段落中添加外部图像的有趣案例.这是东西:

I am new to iText and faced with a real interesting case about adding external images to a paragraph. Here is the thing:

Document document = new Document();  
PdfWriter.getInstance(document, new FileOutputStream("out2.pdf"));  
document.open();  
Paragraph p = new Paragraph();  
Image img = Image.getInstance("blablabla.jpg");  
img.setAlignment(Image.LEFT| Image.TEXTWRAP);  
// Notice the image added to the Paragraph through a Chunk
p.add(new Chunk(img2, 0, 0, true));  
document.add(p);  
Paragraph p2 = new Paragraph("Hello Worlddd!");  
document.add(p2);

给我照片和"Hello Worlddd!"下面的字符串.但是,

gives me the picture and "Hello Worlddd!" string below. However,

Document document = new Document();  
PdfWriter.getInstance(document, new FileOutputStream("out2.pdf"));  
document.open();  
Paragraph p = new Paragraph();  
Image img = Image.getInstance("blablabla.jpg");  
img.setAlignment(Image.LEFT| Image.TEXTWRAP);  
// Notice the image added directly to the Paragraph
p.add(img);
document.add(p);  
Paragraph p2 = new Paragraph("Hello Worlddd!");  
document.add(p2);

给我图片和字符串"Hello worlddd!".位于图片的右侧,图片的上方一行.

gives me the picture and string "Hello worlddd!" located on the right hand side of the picture and one line above it.

这种差异背后的逻辑是什么?

What is the logic behind that difference?

推荐答案

您描述的行为是因为在第二个代码段中,段落不会调整其前导,而是会调整其宽度.如果您在第二个代码段中添加了行

The behaviour you described is because in the second code snippet the Paragraph doesn't adjust its leading, but adjust its width. If in the second snippet you add the line

p.add("Hello world 1")

就在

p.add(img)

您会在左侧看到字符串"Hello world 1",在字符串"Hello Worlddd!"的上方一点点.如果输出p(System.out.println(p.getLeading())的前导,则可以看到它的数字很低(通常为16),而不是图像的高度.

you'll see the string "Hello world 1" on the left and a little bit above the string "Hello Worlddd!". If you output the leading of p (System.out.println(p.getLeading()) you can see it's a low number (typically 16) and not the height of the image.

在第一个示例中,您使用带有4个参数的块构造器

In the first example you use the chunk constructor with 4 arguments

new Chunk(img, 0, 0, true)

最后一个(true)用来调整行距,因此可以按预期打印.

with the last (true) saying to adjust the leading, so it print as you expected.

这篇关于iText-使用块添加外部图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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