0

I have added following picture in word document using XWPF addPicture ,

XWPFParagraph p3 = document.createParagraph();
XWPFRun r3 = p3.createRun();
imgFile = "C:\\poi-3.9\\pictures\\Picture4.jpeg";
try {
r3.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(500), Units.toEMU(80)); // 200x200 pixels
}catch (Exception e){
  System.out.println (e.getMessage());
}

firstImage

Now I want to set it to a particular position and tilt it as well, so that it will look like this ...

enter image description here

Is there any way we can do it using XWPF run, or any other method. Thanks.

4

1 回答 1

0

我自己解决了。基本上word文档是一种zip文件。如果我们将 word 文件扩展名重命名为 .zip 并打开 document.xml 文件,我们可以看到图像实际上是如何使用 xml 格式定位的。这是我采取的步骤。

  1. 用所需的图像创建一个word文档并手动设置图像位置()
  2. 将文件扩展名从 .docx 重命名为 .zip,解压缩 zip 文件。
  3. 导航并打开解压缩的 zip 的 word 文件夹中的 document.xml。
  4. 搜索图片名称(这里我的图片名称是picture4)
  5. 从 xml 中获取水平和垂直偏移量、范围 cx 和 cy、相对高度和其他参数位置,我在下面的 xml 片段中将它们标记为粗体。
<w:body>
        <w:p>
            <w:bookmarkStart w:id="0" w:name="_GoBack"/>
            <w:bookmarkEnd w:id="0"/>
            <w:r>
                <w:drawing>
                    <wp:anchor distT="0" distB="0" distL="**114300**" distR="**114300**" simplePos="0" relativeHeight="**251658240**" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1">
                        <wp:simplePos x="0" y="0"/>
                        <wp:positionH relativeFrom="column">
                            <wp:posOffset>**-2256790**</wp:posOffset>
                        </wp:positionH>
                        <wp:positionV relativeFrom="paragraph">
                            <wp:posOffset>**-1823085**</wp:posOffset>
                        </wp:positionV>
                        <wp:extent cx=**"3448050"** cy=**"3619500"**/>
                        <wp:effectExtent l="0" t="0" r="0" b="0"/>
                        <wp:wrapNone/>
                        <wp:docPr id="4" name="Drawing 0" descr="**Picture4**"/>
                        <wp:cNvGraphicFramePr>
  1. 在锚 xml 中设置相同

    String anchorXML = "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +"distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251658240\" behindDoc=\"1\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"

       +"<wp:simplePos x=\"0\" y=\"0\"/>"
    
       +"<wp:positionH relativeFrom=\"column\"><wp:posOffset>-1800000</wp:posOffset></wp:positionH>"
       +"<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>-1800000</wp:posOffset></wp:positionV>"
    
       + "<wp:extent cx=\"3448050\" cy=\"3619500\"/>"
       +"<wp:effectExtent l=\"0\" t=\"0\" r=\"6350\" b=\"6350\"/>"
       +"  <wp:simplePos x=\"0\" y=\"0\"/><wp:positionH relativeFrom=\"column\"><wp:posOffset>-1800000</wp:posOffset></wp:positionH><wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>-1800000</wp:posOffset></wp:positionV><wp:extent cx=\"3448050\" cy=\"3619500\"/><wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/><wp:wrapNone/> "
       +"<wp:docPr id=\"1\" name=\"Drawing 0\" descr=\""+drawingDescr+"\"/><wp:cNvGraphicFramePr/>"
       +"</wp:anchor>"; 
    
  2. 这是添加图像和位置的示例代码,我们可以参数化设置偏移量和其他xml参数的方法。

     import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
     import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
     import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
     public class addImage extends XWPFDocument {
     public static void main(String[] args) throws IOException {`
    // Create a document file
                     CustomXWPFDocument document = new CustomXWPFDocument();
                     XWPFParagraph paragraphFourteenx = document.createParagraph();
                     XWPFRun paragraphFourteenRunx = paragraphFourteenx.createRun();
    
    
             // add picture four         
             //String imgFile = "C:\\pictures\\Picture4.jpeg";
             XWPFParagraph p3 = document.createParagraph();
             XWPFRun r3 = p3.createRun();
             imgFile = "C:\\Picture4.jpeg";
             try {
             r3.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(250), Units.toEMU(250)); // 200x200 pixels
             }catch (Exception e){
               System.out.println (e.getMessage());
             }
    
             try{
              CTDrawing drawing = r3.getCTR().getDrawingArray(0);
               CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
               CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "Picture4", 
                                                      Units.toEMU(250), Units.toEMU(250), 
                                                      Units.toEMU(0), Units.toEMU(0),true);
               drawing.setAnchorArray(new CTAnchor[]{anchor});
               drawing.removeInline(0);
    
               }catch (Exception e){
               System.out.println (e.getMessage());
             }
    
              String anchorXML =          
                   "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
                  +"distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251658240\" behindDoc=\"1\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
    
                   +"<wp:simplePos x=\"0\" y=\"0\"/>"
    
                   +"<wp:positionH relativeFrom=\"column\"><wp:posOffset>-1800000</wp:posOffset></wp:positionH>"
                   +"<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>-1800000</wp:posOffset></wp:positionV>"
    
                   + "<wp:extent cx=\"3448050\" cy=\"3619500\"/>"
                   +"<wp:effectExtent l=\"0\" t=\"0\" r=\"6350\" b=\"6350\"/>"
                   +"  <wp:simplePos x=\"0\" y=\"0\"/><wp:positionH relativeFrom=\"column\"><wp:posOffset>-1800000</wp:posOffset></wp:positionH><wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>-1800000</wp:posOffset></wp:positionV><wp:extent cx=\"3448050\" cy=\"3619500\"/><wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/><wp:wrapNone/> "
                   +"<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);
    
         // stream output to file            
                     FileOutputStream outStream = null;
                     try {
                         double x = Math.random();
    
                         String fileName = "C:\\poi-3.9\\generateprequal\\output\\" + args[0] + ".docx";
                         outStream = new FileOutputStream(fileName);
                     } catch (FileNotFoundException e) {
                         System.out.println("First Catch");
                         e.printStackTrace();
                     }
                     try {
                         document.write(outStream);
                         outStream.close();
                     } catch (FileNotFoundException e) {
                         System.out.println("Second Catch");
                         e.printStackTrace();
                     } catch (IOException e) {
                         System.out.println("Third Catch");
                         e.printStackTrace();
                     }
                     }}
    
于 2020-06-26T04:20:39.793 回答