cac*_*aca 4 pdf image itext paragraph
我是iText的新手,面对一个关于在段落中添加外部图像的真实有趣案例.这是事情:
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);
Run Code Online (Sandbox Code Playgroud)
给我的照片和"Hello Worlddd!" 以下字符串.然而,
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);
Run Code Online (Sandbox Code Playgroud)
给我一张图片和字符串"Hello worlddd!" 位于图片的右侧,上面有一行.
这种差异背后的逻辑是什么?
您描述的行为是因为在第二个代码段中,Paragraph 不会调整其前导,而是调整其宽度.如果在第二个片段中添加了该行
p.add("Hello world 1")
Run Code Online (Sandbox Code Playgroud)
就在此之前
p.add(img)
Run Code Online (Sandbox Code Playgroud)
你会在左边看到字符串"Hello world 1",并在字符串"Hello Worlddd!"上方稍微看一下.如果输出p的前导(System.out.println(p.getLeading()),您可以看到它的数字较小(通常为16),而不是图像的高度.
在第一个示例中,您使用具有4个参数的块构造函数
new Chunk(img, 0, 0, true)
Run Code Online (Sandbox Code Playgroud)
用最后(真实)的说法来调整前导,所以它按照你的预期打印.