在Apache POI(docx)中换行吗? [英] Wrap Text in Apache POI(docx)?

查看:72
本文介绍了在Apache POI(docx)中换行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像添加到docx中,但是我发现了简单的Apache poi代码:

I want to add an image into docx , but I just found simple apache poi code:

 XWPFDocument document = new XWPFDocument()
        XWPFParagraph paragraph = document.createParagraph()
        XWPFRun run = paragraph.createRun()
        run.addPicture(new FileInputStream(img), format, img, Units.toEMU(width), Units.toEMU(height))
        FileOutputStream outputStream = new FileOutputStream(doc)
        document.write(outputStream)

现在我想将图片设置在文本前面,但是找不到任何参考.

Now I want to set the picture in front of text , but I can not found any reference.

推荐答案

Office Open XML 格式以及 *.docx 都是 ZIP 存档,其中包含 XML 文件和目录结构中的其他文件.因此,如果我们很好奇,我们可以简单地将它们解压缩并进行查看.

The Office Open XML formats and also *.docx as such are ZIP archives containing XML files and other files in a directory structure. So if we are curious we can simple unzip them and taking a look into.

使用默认代码在/word/document.xml 中运行的文本中添加图片,我们会发现类似

Using the default code for adding a picture to a text run in /word/document.xml we find something like:

<w:r>
 <w:t>Picture inline with text:</w:t>
 <w:drawing>
  <wp:inline distT="0" distR="0" distB="0" distL="0">
   <wp:extent cx="1905000" cy="254000"/>
   <wp:docPr id="0" name="Drawing 0" descr="samplePict.jpeg"/>
   <a:graphic>
    <a:graphicData ...

如果我们打开此usig Word 并将图片的文本换行更改为文本后面,则在/word/document.xml 中,我们会找到类似以下内容的内容:

If we are opening this usig Word and changing the text wrap of the picture to behind text, then in /word/document.xml we find something like:

<w:r>
 <w:drawing>
  <wp:anchor allowOverlap="1" behindDoc="1" layoutInCell="1" locked="0" relativeHeight="0" simplePos="0">
   <wp:simplePos x="0" y="0"/>
   <wp:positionH relativeFrom="column"><wp:posOffset>0</wp:posOffset></wp:positionH>
   <wp:positionV relativeFrom="paragraph"><wp:posOffset>0</wp:posOffset></wp:positionV>
   <wp:extent cx="1905000" cy="508000"/>
   <wp:effectExtent b="0" l="0" r="0" t="0"/><wp:wrapNone/>
   <wp:docPr descr="samplePict.jpeg" id="1" name="Drawing 0"/><wp:cNvGraphicFramePr/>
   <a:graphic>
    <a:graphicData ...

如您所见,第一个位于 wp:inline 元素内,而第二个位于 wp:anchor 元素内.

As you see, the first is within an wp:inline element while the second is within an wp:anchor element.

不幸的是,到目前为止,不能使用 apache poi 来应用 wp:anchor 元素.因此,我们需要知道在哪里找到 apache poi 所基于的低级对象.我发现

Unfortunately the wp:anchor element cannot be applied using apache poi until now. So we need to know where to find the low level objects, apache poi bases on. I found http://grepcode.com a good reference.

现在,我们可以基于这些低级对象对所需的事物进行编码:

Now we can coding the needed things based on those low level objects:

import java.io.*;

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

 private static CTAnchor getAnchorWithGraphic(CTDrawing drawing /*inline drawing*/ , 
  String drawingDescr, boolean behind) throws Exception {

  CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
  long width = drawing.getInlineArray(0).getExtent().getCx();
  long height = drawing.getInlineArray(0).getExtent().getCy();

  String anchorXML = 
   "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
  +"simplePos=\"0\" relativeHeight=\"0\" behindDoc=\""+((behind)?1:0)+"\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
  +"<wp:simplePos x=\"0\" y=\"0\"/>"
  +"<wp:positionH relativeFrom=\"column\"><wp:posOffset>0</wp:posOffset></wp:positionH>"
  +"<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>0</wp:posOffset></wp:positionV>"
  +"<wp:extent cx=\""+width+"\" cy=\""+height+"\"/>"
  +"<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>";

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

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

  XWPFDocument doc= new XWPFDocument();
  XWPFParagraph paragraph;
  XWPFRun run; 
  InputStream in;
  CTDrawing drawing;
  CTAnchor anchor;

  //default
  paragraph = doc.createParagraph();
  run = paragraph.createRun();
  run.setText("Picture inline with text:");
  in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(150), Units.toEMU(40));
  in.close();  

  paragraph = doc.createParagraph();

  //behind text
  paragraph = doc.createParagraph();
  run = paragraph.createRun();
  in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(150), Units.toEMU(40));
  in.close();  
  drawing = run.getCTR().getDrawingArray(0);

  anchor = getAnchorWithGraphic(drawing, "samplePict.jpeg", true /*behind text*/);

  drawing.setAnchorArray(new CTAnchor[]{anchor});
  drawing.removeInline(0);
  run = paragraph.createRun();
  run.setText("The above picture is behind the text. ");

  paragraph = doc.createParagraph();

  //in front of text
  paragraph = doc.createParagraph();
  run = paragraph.createRun();
  in = new FileInputStream("samplePict.jpeg");
  run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(150), Units.toEMU(40));
  in.close();  
  drawing = run.getCTR().getDrawingArray(0);

  anchor = getAnchorWithGraphic(drawing, "samplePict.jpeg", false /*not behind text*/);

  drawing.setAnchorArray(new CTAnchor[]{anchor});
  drawing.removeInline(0);
  run = paragraph.createRun();
  run.setText("The above picture is in front of the text. ");

  paragraph = doc.createParagraph();

  doc.write(new FileOutputStream("CreateWordImagesBehindAndInFrontText.docx"));
  doc.close();

 }
}

这篇关于在Apache POI(docx)中换行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆