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

标签 c# pdf-generation itext itext7

我有一个带表格的 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();

实际输出是这样的:

enter image description here

我想要实现的是:

enter image description here

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

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

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

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

最佳答案

float 元素功能已在 7.0.3-SNAPSHOT 中实现,并且很可能会进入 7.0.3 版本。

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

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

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

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

输出如下所示:

enter image description here

关于c# - 使用 iText 7 将文本环绕在单元格中的图像周围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960676/

相关文章:

c# - Xamarin 使用 xaml 创建具有多个详细 View 的 masterdetailpage

c# - automapper 继承 Profile 返回 json 中的额外属性

pdf-generation - 创建PDF iOS7

java - 尝试渲染 pdf 时获取此 "org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBox"

c# - MVC 漂亮的 url 和正则表达式中的特殊字符

c++ - 如何在 Qt C++ 中生成带数字签名的 PDF

java - Itext 5.5.9 - 设置加密时出现问题

java - iText 限制文本字段中的文本大小

java - 如何使用 iText Java 在新的浏览器选项卡中打开新的 pdf 文件

c# - 字符串包含另外两个字符串