Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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
Java 使用ApachePOI在DOCX中更改图像布局或包装_Java_Apache Poi_Docx - Fatal编程技术网

Java 使用ApachePOI在DOCX中更改图像布局或包装

Java 使用ApachePOI在DOCX中更改图像布局或包装,java,apache-poi,docx,Java,Apache Poi,Docx,我以编程方式将图像粘贴到docx中。但结果是布局不适合我。面临缺乏文件的问题。 我需要更改图像换行(布局)。例如,现在我有: 但我想要这个: UPD1:我要做的是:反复浏览段落,然后浏览跑步记录,找到带有特殊书签的特定跑步记录。在这次跑步中,我添加了图片: XWPFPicture pic = run.addPicture( new ByteArrayInputStream(picSource), Document.PICTURE_TYPE_PNG,

我以编程方式将图像粘贴到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-带着我们的图片跑步):

但当我试图打开它时,它是一个分解文档:

我们很抱歉。我们无法打开文档,因为我们发现 它的内容

依赖关系包括:

<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>

org.apache.poi
poi ooxml
3.17
org.apache.poi
poi ooxml模式
3.17
org.apache.poi
ooxml模式
1.3

虽然您已经找到了可以使用的正确类以及后续类,但是还需要考虑到。这说明需要更多的责任要素,而不仅仅是定义包装的要素。因此,使用
org.openxmlformats.schemas.drawingml.x2006
的逐类代码,您可能正在编写页面代码。对于此类问题,我首选的解决方案是提供
XML
,通过一些变量更新所有需要的元素。然后可以解析该
XML
,以获得所需的对象

例如:

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.util.Units;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;

public class WordInsertPictures {

 private static CTAnchor getAnchorWithGraphic(CTGraphicalObject graphicalobject, 
                                              String drawingDescr, int width, int height,
                                              int left, int top) throws Exception {

  String anchorXML = 
   "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
  +"simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"1\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
  +"<wp:simplePos x=\"0\" y=\"0\"/>"
  +"<wp:positionH relativeFrom=\"column\"><wp:posOffset>"+left+"</wp:posOffset></wp:positionH>"
  +"<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>"+top+"</wp:posOffset></wp:positionV>"
  +"<wp:extent cx=\""+width+"\" cy=\""+height+"\"/>"
  +"<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>"
  +"<wp:wrapTight wrapText=\"bothSides\">"
  +"<wp:wrapPolygon edited=\"0\">"
  +"<wp:start x=\"0\" y=\"0\"/>"
  +"<wp:lineTo x=\"0\" y=\"21600\"/>" //Square polygon 21600 x 21600 leads to wrap points in fully width x height
  +"<wp:lineTo x=\"21600\" y=\"21600\"/>"// Why? I don't know. Try & error ;-).
  +"<wp:lineTo x=\"21600\" y=\"0\"/>"
  +"<wp:lineTo x=\"0\" y=\"0\"/>"
  +"</wp:wrapPolygon>"
  +"</wp:wrapTight>"
  +"<wp:docPr id=\"1\" name=\"Drawing 0\" descr=\""+drawingDescr+"\"/><wp:cNvGraphicFramePr/>"
  +"</wp:anchor>";

  CTDrawing drawing = CTDrawing.Factory.parse(anchorXML);
  CTAnchor anchor = drawing.getAnchorArray(0);
  anchor.setGraphic(graphicalobject);
  return anchor;  
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();

  run.setText("The picture in line: ");

  InputStream in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(30));
  in.close();  

  run.setText(" text after the picture.");

  paragraph = document.createParagraph();

  run = paragraph.createRun();
  in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(30));
  in.close();  
  CTDrawing drawing = run.getCTR().getDrawingArray(0);
  CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
  CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "samplePict.jpeg", 
                                         Units.toEMU(100), Units.toEMU(30), 
                                         Units.toEMU(30), Units.toEMU(0));
  drawing.setAnchorArray(new CTAnchor[]{anchor});
  drawing.removeInline(0);

  run = paragraph.createRun();
  run.setText("The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight.");

  document.write(new FileOutputStream("WordInsertPictures.docx"));
  document.close();
 }
}
import java.io.FileOutputStream;
导入java.io.FileInputStream;
导入java.io.InputStream;
导入org.apache.poi.xwpf.usermodel.*;
导入org.apache.poi.util.Units;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
导入org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
导入org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
公共类WordInsertPictures{
专用静态CTAnchor getAnchorWithGraphic(CTGraphicalObject graphicalobject,
字符串绘制描述,整数宽度,整数高度,
int left,int top)引发异常{
字符串锚定XML=
""
+""
+“”+左+“”
+“”+top+“”
+""
+""
+""
+""
+""
+“”//方形多边形21600 x 21600导致全宽x全高的包裹点
+“”//为什么?我不知道。请尝试&error;-)。
+""
+""
+""
+""
+""
+"";
CTDrawing=CTDrawing.Factory.parse(anchorXML);
CTAnchor anchor=图纸。GetAnchorary(0);
anchor.setGraphic(graphicalobject);
回锚;
}
公共静态void main(字符串[]args)引发异常{
XWPFDocument document=新的XWPFDocument();
XWPFParagraph paragraph paragraph=document.createParagraph();
XWPFRun=段落.createRun();
run.setText(“行中的图片:”);
InputStream in=新文件InputStream(“samplePict.jpeg”);
run.addPicture(in,Document.PICTURE_TYPE_JPEG,“samplePict.JPEG”,Units.toEMU(100),Units.toEMU(30));
in.close();
run.setText(“图片后面的文本”);
段落=document.create段落();
run=段落.createRun();
in=新文件输入流(“samplePict.jpeg”);
run.addPicture(in,Document.PICTURE_TYPE_JPEG,“samplePict.JPEG”,Units.toEMU(100),Units.toEMU(30));
in.close();
CTDrawing drawing=run.getCTR().getDrawingArray(0);
CTGraphicalObject graphicalobject=drawing.getInlineArray(0.getGraphic();
CTAnchor anchor=getAnchorWithGraphic(graphicalobject,“samplePict.jpeg”,
单位。toEMU(100),单位。toEMU(30),
单位。toEMU(30),单位。toEMU(0);
图纸:Setanchorary(新CTAnchor[]{anchor});
图纸。移除细脉线(0);
run=段落.createRun();
run.setText(“上一张图片是锚定的wrapTight。上一张图片是锚定的wrapTight。上一张图片是锚定的wrapTight。上一张图片是锚定的wrapTight。”);
编写(新文件输出流(“WordInsertPictures.docx”);
document.close();
}
}

“但我仍然不知道如何将它应用到我的代码中。”:在那一行代码中?这是不可能的。您将需要更多的代码。显示一个完整的示例,说明如何在文本中添加图片。然后,我将向您展示如何添加具有文本换行的图片。@AxelRichter我已更新了问题谢谢您的varian,我一定会在度假后尝试此功能。但现在我找到了暂时的解决办法。我在make
.addPicture()之前添加了
中断
,图像布局就成了所需。但前面有break。@选项:您的问题是将布局选项从“与文本对齐”更改为“与文本环绕-紧密”。这绝对不能通过在图片运行之前添加换行符来实现。正如我所说的,这是临时解决方案。但是如果没有这个,图像的高度等于直线的高度。在之前添加一些breake会有所帮助。我在第一次评论时并没有说对——当然布局并没有改变,但它在视觉上产生了预期的效果。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.util.Units;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;

public class WordInsertPictures {

 private static CTAnchor getAnchorWithGraphic(CTGraphicalObject graphicalobject, 
                                              String drawingDescr, int width, int height,
                                              int left, int top) throws Exception {

  String anchorXML = 
   "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
  +"simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"1\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
  +"<wp:simplePos x=\"0\" y=\"0\"/>"
  +"<wp:positionH relativeFrom=\"column\"><wp:posOffset>"+left+"</wp:posOffset></wp:positionH>"
  +"<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>"+top+"</wp:posOffset></wp:positionV>"
  +"<wp:extent cx=\""+width+"\" cy=\""+height+"\"/>"
  +"<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>"
  +"<wp:wrapTight wrapText=\"bothSides\">"
  +"<wp:wrapPolygon edited=\"0\">"
  +"<wp:start x=\"0\" y=\"0\"/>"
  +"<wp:lineTo x=\"0\" y=\"21600\"/>" //Square polygon 21600 x 21600 leads to wrap points in fully width x height
  +"<wp:lineTo x=\"21600\" y=\"21600\"/>"// Why? I don't know. Try & error ;-).
  +"<wp:lineTo x=\"21600\" y=\"0\"/>"
  +"<wp:lineTo x=\"0\" y=\"0\"/>"
  +"</wp:wrapPolygon>"
  +"</wp:wrapTight>"
  +"<wp:docPr id=\"1\" name=\"Drawing 0\" descr=\""+drawingDescr+"\"/><wp:cNvGraphicFramePr/>"
  +"</wp:anchor>";

  CTDrawing drawing = CTDrawing.Factory.parse(anchorXML);
  CTAnchor anchor = drawing.getAnchorArray(0);
  anchor.setGraphic(graphicalobject);
  return anchor;  
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();

  run.setText("The picture in line: ");

  InputStream in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(30));
  in.close();  

  run.setText(" text after the picture.");

  paragraph = document.createParagraph();

  run = paragraph.createRun();
  in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(30));
  in.close();  
  CTDrawing drawing = run.getCTR().getDrawingArray(0);
  CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
  CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "samplePict.jpeg", 
                                         Units.toEMU(100), Units.toEMU(30), 
                                         Units.toEMU(30), Units.toEMU(0));
  drawing.setAnchorArray(new CTAnchor[]{anchor});
  drawing.removeInline(0);

  run = paragraph.createRun();
  run.setText("The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight. The previous picture is anchored wrapTight.");

  document.write(new FileOutputStream("WordInsertPictures.docx"));
  document.close();
 }
}