使用 apache poi 在文档 .docx 中创建文本框 [英] create text box in document .docx using apache poi

查看:64
本文介绍了使用 apache poi 在文档 .docx 中创建文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文档 .docx 中创建一个文本框,但我没有找到方法帮助我做到这一点和任何例子.有人知道我该怎么做吗?

I want create a text box in a document .docx but I didn´t find a method that help me do it and any example. Someone know how can i do it?

推荐答案

插入真正的 Word 文本框直到现在用 apache-poi 还不是完全可能的.一个真正的Word 文本框包含在来自schemasMicrosoftComVmlcom.microsoft.schemas.vmlCTShape 中.XML 看起来像:

Inserting a real Word Text-box is not fully possible with apache-poi until now. A real Word Text-box is contained in a CTShape from schemasMicrosoftComVml or com.microsoft.schemas.vml. The XML looks like:

<w:r>
 <w:pict>
  <v:shape style="width:100pt;height:24pt">
   <v:textbox>
    <w:txbxContent>
     <w:p>
      <w:r>
       <w:t>The TextBox text...</w:t>
      </w:r>
     </w:p>
    </w:txbxContent>
   </v:textbox>
  </v:shape>
 </w:pict>
</w:r>

如您所见,v:shapev:textbox 中的命名空间与其他命名空间不同.

As you see the namespaces in v:shape and v:textbox are different from the rest.

所以如果我们知道并尊重这一点,我们目前可以插入这样的CTShape.但仅与文本内联.据我所知,目前无法为此创建 CTWrap,因为 schemasMicrosoftComOfficeWord 中的 CTWrap 未在 poi- 中提供ooxml-schemas-3.13-*.jar 和 ooxml-schemas-1.3.jar 中的 com.microsoft.schemas.vmlcom.microsoft.schemas.office 类需要3.13 版未附带的 org.apache.poi.POIXMLTypeLoader.

So if we know and respect this, we can currently insert such CTShape. But only inline with the text. Creating a CTWrap for this is currently not possible, as far as I know, because the CTWrap from the schemasMicrosoftComOfficeWord is not shipped within the poi-ooxml-schemas-3.13-*.jar and the com.microsoft.schemas.vml and com.microsoft.schemas.office classes from ooxml-schemas-1.3.jar need a org.apache.poi.POIXMLTypeLoader which is not shipped with version 3.13.

内联文本框示例:

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;

import schemasMicrosoftComVml.CTGroup;
import schemasMicrosoftComVml.CTShape;

import org.w3c.dom.Node;

public class CreateWordTextBox {

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

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body text: ");

  CTGroup ctGroup = CTGroup.Factory.newInstance();

  CTShape ctShape = ctGroup.addNewShape();
  ctShape.setStyle("width:100pt;height:24pt");
  CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
  ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text...");

  Node ctGroupNode = ctGroup.getDomNode(); 
  CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
  run=paragraph.createRun();  
  CTR cTR = run.getCTR();
  cTR.addNewPict();
  cTR.setPictArray(0, ctPicture);
    
  doc.write(new FileOutputStream("WordTextBox.docx"));

 }
}

但是定位文本框架是可能的:

But a positioned text-frame is possible:

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentBlock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPBdr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFramePr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STXAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STWrap;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
/*
To
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFramePr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STXAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STWrap;
the fully ooxml-schemas-1.3.jar is needed as mentioned in https://poi.apache.org/faq.html#faq-N10025
*/

import java.math.BigInteger;

public class CreateWordTextFrame {

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

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The body text:");

  CTSdtContentBlock ctSdtContentBlock = doc.getDocument().getBody().addNewSdt().addNewSdtContent();

  CTP ctP = ctSdtContentBlock.addNewP();
  ctP.addNewR().addNewT().setStringValue("The TextFrame text...");

  CTPPr ctPPr = ctP.addNewPPr();
  CTFramePr ctFramePr = ctPPr.addNewFramePr();
  ctFramePr.setHAnchor(STHAnchor.TEXT);
  ctFramePr.setVAnchor(STVAnchor.TEXT);
  ctFramePr.setXAlign(STXAlign.CENTER);
  ctFramePr.setWrap(STWrap.AROUND);
  ctFramePr.setW(BigInteger.valueOf(4000));
  ctFramePr.setHSpace(BigInteger.valueOf(400));

  CTPBdr ctPBdr = ctPPr.addNewPBdr();
  CTBorder ctBorder = ctPBdr.addNewLeft(); ctBorder.setColor("000000"); ctBorder.setVal(STBorder.SINGLE); ctBorder.setSz(BigInteger.valueOf(4)); ctBorder.setSpace(BigInteger.valueOf(7));
  ctBorder = ctPBdr.addNewRight(); ctBorder.setColor("000000"); ctBorder.setVal(STBorder.SINGLE); ctBorder.setSz(BigInteger.valueOf(4)); ctBorder.setSpace(BigInteger.valueOf(7));
  ctBorder = ctPBdr.addNewTop(); ctBorder.setColor("000000"); ctBorder.setVal(STBorder.SINGLE); ctBorder.setSz(BigInteger.valueOf(4)); ctBorder.setSpace(BigInteger.valueOf(7));
  ctBorder = ctPBdr.addNewBottom(); ctBorder.setColor("000000"); ctBorder.setVal(STBorder.SINGLE); ctBorder.setSz(BigInteger.valueOf(4)); ctBorder.setSpace(BigInteger.valueOf(7));

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... ");
     
  doc.write(new FileOutputStream("WordTextFrame.docx"));

 }
}

这需要 https:/中提到的完全 ooxml-schemas-1.3.jar/poi.apache.org/faq.html#faq-N10025.

编辑

如果我们降级到 ooxml-schemas-1.1.jar - 可从 http://search.maven.org/#artifactdetails|org.apache.poi|ooxml-schemas|1.1|jar - 那么一个免费的可定位文本框也可以使用apache-poi 3.13 版.

If we downgrade to ooxml-schemas-1.1.jar - available from http://search.maven.org/#artifactdetails|org.apache.poi|ooxml-schemas|1.1|jar - then a free positionable Text-box is also possible with apache-poi version 3.13.

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;

import schemasMicrosoftComVml.CTGroup;
import schemasMicrosoftComVml.CTShape;
import schemasMicrosoftComOfficeWord.CTWrap;
import schemasMicrosoftComOfficeWord.STWrapType;
/*
To 
import schemasMicrosoftComOfficeWord.CTWrap;
import schemasMicrosoftComOfficeWord.STWrapType;
ooxml-schemas-1.1.jar is needed - available from http://search.maven.org/#artifactdetails|org.apache.poi|ooxml-schemas|1.1|jar
*/

import org.w3c.dom.Node;

public class CreateWordTextBox {

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

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body text: ");

  CTGroup ctGroup = CTGroup.Factory.newInstance();

  CTShape ctShape = ctGroup.addNewShape();
  ctShape.addNewWrap().setType(STWrapType.SQUARE);
  ctShape.setStyle("position:absolute;mso-position-horizontal:center;margin-top:40pt;width:100pt;height:24pt");
  CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
  ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text...");

  Node ctGroupNode = ctGroup.getDomNode(); 
  CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
  run=paragraph.createRun();  
  CTR cTR = run.getCTR();
  cTR.addNewPict();
  cTR.setPictArray(0, ctPicture);

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... ");
     
    
  doc.write(new FileOutputStream("WordTextBox.docx"));

 }
}


编辑

使用当前的 apache poi 5.0.0 以下是可能的:

Using current apache poi 5.0.0 following is possible:

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;

import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import com.microsoft.schemas.office.word.CTWrap;
import com.microsoft.schemas.office.word.STWrapType;

import org.w3c.dom.Node;

public class CreateWordTextBox {

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

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body text: ");

  CTGroup ctGroup = CTGroup.Factory.newInstance();

  CTShape ctShape = ctGroup.addNewShape();
  ctShape.addNewWrap().setType(STWrapType.SQUARE);
  ctShape.setStyle("position:absolute;mso-position-horizontal:center;margin-top:40pt;width:100pt;height:24pt");
  CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
  ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text...");

  Node ctGroupNode = ctGroup.getDomNode(); 
  CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
  run=paragraph.createRun();  
  CTR cTR = run.getCTR();
  cTR.addNewPict();
  cTR.setPictArray(0, ctPicture);

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... ");

  FileOutputStream out = new FileOutputStream("WordTextBox.docx");
  doc.write(out);
  out.close();
  doc.close();

 }
}

这篇关于使用 apache poi 在文档 .docx 中创建文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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