【发布时间】:2017-12-08 22:45:04
【问题描述】:
我以编程方式将图像粘贴到 docx 中。但结果布局不适合我。面临缺乏文件。 我需要更改图像换行(布局)。例如现在我有这个:
但是想要这个:
UPD1:我所做的:遍历段落,然后遍历运行并找到带有特殊书签的特定运行。在这次运行中,我添加了图片:
XWPFPicture pic = run.addPicture(
new ByteArrayInputStream(picSource),
Document.PICTURE_TYPE_PNG,
"pic",
Units.toEMU(100),
Units.toEMU(30));
UPD2:在这个类中调查了一些有趣的东西:
org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor
方法setWrapTight(CTWrapTight var1)。可能是它。仍然不知道如何将它应用到我的代码中。
UPD3:最后我想到了这个(currentRun - 用我们的图片运行):
CTWrapTight ctWrapTight = currentRun.getCTR().getDrawingList().get(0).addNewAnchor().addNewWrapTight();
CTWrapPath ctWrapPath = ctWrapTight.addNewWrapPolygon();
CTPoint2D ctStart = ctWrapPath.addNewStart();
ctStart.setX(0L);
ctStart.setY(0L);
CTPoint2D ctLineTo1 = ctWrapPath.addNewLineTo();
CTPoint2D ctLineTo2 = ctWrapPath.addNewLineTo();
CTPoint2D ctLineTo3 = ctWrapPath.addNewLineTo();
ctLineTo1.setX(21384L);
ctLineTo1.setY(20520L);
ctLineTo2.setX(21384L);
ctLineTo2.setY(0L);
ctLineTo3.setX(0L);
ctLineTo3.setY(0L);
ctWrapTight.setWrapText(STWrapText.BOTH_SIDES);
但是当我尝试打开它时它会分解文档:
我们很抱歉。我们无法打开文档,因为我们发现了一个问题 它的内容。
依赖是:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
【问题讨论】:
-
“但我仍然不知道如何将它应用到我的代码中。”:在那一行代码中?这是不可能的。那时你将需要更多的代码。展示一个完整的示例,说明如何添加与文本一致的图片。然后我将向您展示如何添加带有文字换行的图片。
-
@AxelRichter 我更新了问题
标签: java apache-poi docx