I paste image into docx programmatically. But in result the layout does not suit me. Faced a lack of documentation.
I need to change image wrap (layout). For example now I have this:
But want this:
UPD1: What I do: iterate through the paragraphs, then through the runs and find certain run with special bookmark. In this run I add picture:
XWPFPicture pic = run.addPicture(
new ByteArrayInputStream(picSource),
Document.PICTURE_TYPE_PNG,
"pic",
Units.toEMU(100),
Units.toEMU(30));
UPD2: Investigated something interesting inside this class:
org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor
method setWrapTight(CTWrapTight var1)
. May be is it. By still don't know how to apply it to my code.
UPD3: Finally I came to this (currentRun - run with our picture):
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);
But it's break down document when I try to open it:
We're sorry. We can't open document because we found a problem with
its contents.
Dependency are:
<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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…