java读取word内容中的控件,[简单]Docx4j读取word 2007内容控件

word 2007内容控件位置:

8027d1c1ba50c5f3cbd4e8159a5a98dd.png

代码如下:

import java.io.File;

import java.io.StringWriter;

import java.util.ArrayList;

import java.util.List;

import javax.xml.bind.JAXBElement;

import org.docx4j.TextUtils;

import org.docx4j.TraversalUtil;

import org.docx4j.TraversalUtil.CallbackImpl;

import org.docx4j.XmlUtils;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import org.docx4j.wml.Body;

import org.docx4j.wml.CTLock;

import org.docx4j.wml.CTSdtCell;

import org.docx4j.wml.ContentAccessor;

import org.docx4j.wml.Document;

import org.docx4j.wml.Id;

import org.docx4j.wml.SdtBlock;

import org.docx4j.wml.SdtPr;

import org.docx4j.wml.SdtPr.Alias;

import org.docx4j.wml.SdtRun;

import org.docx4j.wml.Tag;

import org.jvnet.jaxb2_commons.ppp.Child;

public class Docx4j_读取内容控件_S3_Test {

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

Docx4j_读取内容控件_S3_Test t = new Docx4j_读取内容控件_S3_Test();

t.printSdtContent("f:/saveFile/temp/kkk - 副本.docx");

}

public void printSdtContent(String filePath) throws Exception {

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage

.load(new File(filePath));

MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

Document wmlDocumentEl = (Document) mdp.getContents();

Body body = wmlDocumentEl.getBody();

SdtFinder sdtFinder = new SdtFinder();

new TraversalUtil(body, sdtFinder);

for (Child sdtChild : sdtFinder.sdtList) {

if (sdtChild instanceof SdtRun) {

SdtRun sdtRun = (SdtRun) sdtChild;

SdtPr sdtPr = sdtRun.getSdtPr();

printSdtPrContent(sdtPr);

// System.out.println(XmlUtils.marshaltoString(sdtPr, true,

// true));

ContentAccessor sdtContent = sdtRun.getSdtContent();

System.out.println("-----------p content="

+ getContentAccessorContent(sdtContent));

} else if (sdtChild instanceof CTSdtCell) {

CTSdtCell sdtCell = (CTSdtCell) sdtChild;

SdtPr sdtPr = sdtCell.getSdtPr();

printSdtPrContent(sdtPr);

ContentAccessor sdtContent = sdtCell.getSdtContent();

System.out.println("-----------table content="

+ getContentAccessorContent(sdtContent));

} else if (sdtChild instanceof SdtBlock) {

SdtBlock sdtBlock = (SdtBlock) sdtChild;

SdtPr sdtPr = sdtBlock.getSdtPr();

printSdtPrContent(sdtPr);

ContentAccessor sdtContent = sdtBlock.getSdtContent();

System.out.println("-----------sdtblock content="

+ getContentAccessorContent(sdtContent));

}

}

}

// 没有解析样式

public void printSdtPrContent(SdtPr sdtPr) {

StringBuffer sb = new StringBuffer();

List rprList = sdtPr.getRPrOrAliasOrLock();

for (Object obj : rprList) {

if (obj instanceof JAXBElement) {

String eName = ((JAXBElement) obj).getName().getLocalPart();

// 布尔类型特殊处理

if ("temporary".equals(eName)) {

sb.append(" 替换后是否删除内容控件:").append("是");

}

obj = XmlUtils.unwrap(obj);

// System.out.println("------after="+ obj.getClass().getName());

if (obj instanceof CTLock) {

CTLock lock = (CTLock) obj;

if (lock != null) {

sb.append(" 锁定方式:").append(lock.getVal());

}

} else if (obj instanceof Alias) {

Alias alias = (Alias) obj;

if (alias != null) {

sb.append(" 标题:").append(alias.getVal());

}

}

} else if (obj instanceof Tag) {

Tag tag = (Tag) obj;

if (tag != null) {

sb.append(" tag标记:").append(tag.getVal());

}

} else if (obj instanceof Id) {

Id id = (Id) obj;

if (id != null) {

sb.append(" id:").append(id.getVal());

}

}

}

System.out.println(sb.toString());

}

public String getContentAccessorContent(ContentAccessor contentAcc)

throws Exception {

StringWriter stringWriter = new StringWriter();

TextUtils.extractText(contentAcc, stringWriter);

return stringWriter.toString();

}

}

class SdtFinder extends CallbackImpl {

List sdtList = new ArrayList();

public List apply(Object o) {

if (o instanceof javax.xml.bind.JAXBElement

&& (((JAXBElement) o).getName().getLocalPart().equals("sdt"))) {

sdtList.add((Child) XmlUtils.unwrap(o));

} else if (o instanceof SdtBlock) {

sdtList.add((Child) o);

}

return null;

}

// to setParent

public void walkJAXBElements(Object parent) {

List children = getChildren(parent);

if (children != null) {

for (Object o : children) {

if (o instanceof javax.xml.bind.JAXBElement

&& (((JAXBElement) o).getName().getLocalPart()

.equals("sdt"))) {

((Child) ((JAXBElement) o).getValue()).setParent(XmlUtils

.unwrap(parent));

} else {

o = XmlUtils.unwrap(o);

if (o instanceof Child) {

((Child) o).setParent(XmlUtils.unwrap(parent));

}

}

this.apply(o);

if (this.shouldTraverse(o)) {

walkJAXBElements(o);

}

}

}

}

}

全文完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值