使用 iText 7 将文本环绕在单元格中的图像周围

Wrap text around image in cell using iText 7

我有一个带有 table 的 PDF 文档。代码如下:

PdfWriter _writer = new PdfWriter(@"C:\output.pdf");
PdfDocument _document = new PdfDocument(_writer);
Document MyDocument = new Document(_document, PageSize.A4);

Table MyTable = new Table(new float[] { 1, 4 });
MyTable.SetWidthPercent(100);
MyTable.AddHeaderCell(new Cell().Add(new Paragraph("ID")));
MyTable.AddHeaderCell(new Cell().Add(new Paragraph("Description")));

MyTable.AddCell(new Cell().Add(new Paragraph("1")));
Cell descCell = new Cell();
descCell.Add(IMG); // iText.Layout.Element.Image
descCell.Add(new Paragraph("This is the description."));
MyTable.AddCell(descCell);

MyDocument.Add(MyTable);
MyDocument.Close();

实际输出是这样的:

我想要实现的是:

我已经阅读了几个 iText 5 的示例,并且都指向使用这个 属性:

image.setAlignment(Image.LEFT | Image.TEXTWRAP);

问题是它似乎在 iText 7 上不可用。

如有任何帮助,我们将不胜感激。

浮动元素功能已在 7.0.3-SNAPSHOT 中实现,很可能会在 7.0.3 版本中实现。

为了让文本环绕单元格中的图像,您需要将图像指定为浮动元素,就像在 HTML 中一样。为此,请使用

Image img = ...
img.setProperty(Property.FLOAT, FloatPropertyValue.LEFT);

然后,像往常一样构造一个单元格,向其添加图像和文本:

table.addCell(new Cell().add(img).add(textStr);

输出如下所示: