Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image iText-使用块添加外部图像_Image_Pdf_Itext_Paragraph - Fatal编程技术网

Image iText-使用块添加外部图像

Image iText-使用块添加外部图像,image,pdf,itext,paragraph,Image,Pdf,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");

我不熟悉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);
给我下面的图片和“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);
给我图片和字符串“Hello worldd!”,位于图片右侧和上方一行


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

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

p.add("Hello world 1")
就在之前

p.add(img)
您将在左侧看到字符串“Hello world 1”,在字符串“Hello Worlddd!”上方稍高一点。如果输出p的前导(System.out.println(p.getLeading()),可以看到它是一个较低的数字(通常为16),而不是图像的高度

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

new Chunk(img, 0, 0, true)

最后一个(true)表示调整前导,使其按预期打印。

如果直接添加图像,则其对齐属性(设置为 setAlignment()被考虑在内。因此图像位于左侧(image.left),文本被环绕(image.TEXTWRAP)

如果将图像包装在一个块中,它的处理方式就好像它是图像的一个块一样 文本。因此图像特有的对齐属性丢失。这会导致文本位于图像下方


如果您尝试Image.RIGHT,这会变得更明显。在第一个示例中没有任何变化:图像仍然在左侧。在第二个示例中,图像与右侧对齐,文本被包装在其左侧。

这可能只是一个输入错误,但在您的第一个代码段中,您包含了
img2
,而不是
img
。我正在使用sa我的技术,以显示旁边的文字图像,但我没有得到图像的输出。