import com.aspose.words.*;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* word书签填充工具
* @author lang.zhou
* @since 2019/9/11
*/
@Slf4j
public class AsposeDocument extends Document{
@Getter
DocumentBuilder builder;
public AsposeDocument(String pathName) throws Exception {
this(new File(pathName));
}
public AsposeDocument(File file) throws Exception {
this(FileUtils.readFileToByteArray(file));
}
public AsposeDocument(byte[] b) throws Exception {
this(new ByteArrayInputStream(b));
}
public AsposeDocument(InputStream b) throws Exception {
super(b);
builder = new DocumentBuilder(this);
}
public AsposeDocument() throws Exception {
super();
builder = new DocumentBuilder(this);
}
public static void main(String[] args) throws Exception {
new AsposeLicense().validate();
AsposeDocument util = new AsposeDocument("C:\\Users\\Administrator\\Desktop\\test.docx");
List<Bookmark> rangeLabel = new ArrayList<>(5);
BookmarkCollection bookmarks = util.getRange().getBookmarks();
rangeLabel.add(bookmarks.get("LABEL_1"));
rangeLabel.add(bookmarks.get("LABEL_2"));
rangeLabel.add(bookmarks.get("LABEL_3"));
AsposeDocument nodes = util.cutDocument(rangeLabel);
nodes.save("C:\\Users\\Administrator\\Desktop\\cut.docx");
System.out.println();
}
@SneakyThrows
public AsposeDocument cutDocument(List<Bookmark> rangeLabel){
AsposeDocument doc = new AsposeDocument();
Body body = doc.getFirstSection().getBody();
body.removeAllChildren();
NodeImporter importer = new NodeImporter(this, doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
//读取文档的所有节点
NodeCollection<?> allNodeList = this.getChildNodes(NodeType.ANY, true);
List<Node> nodeList = new ArrayList<>();
for (Bookmark s : rangeLabel) {
findBookmarkNodeList(nodeList,s,allNodeList);
}
if(nodeList.size() > 0){
try{
PageSetup pageSetup = doc.getFirstSection().getPageSetup();
pageSetup.setTopMargin(doc.getFirstSection().getPageSetup().getTopMargin());
pageSetup.setBottomMargin(doc.getFirstSection().getPageSetup().getBottomMargin());
pageSetup.setRightMargin(doc.getFirstSection().getPageSetup().getRightMargin());
pageSetup.setLeftMargin(doc.getFirstSection().getPageSetup().getLeftMargin());
pageSetup.setPageWidth(doc.getFirstSection().getPageSetup().getPageWidth());
}catch (Exception e) {
log.error("",e);
}
Node lastNode = null;
CompositeNode<Node> parent = body;
for (Node n : nodeList) {
//段落和表格是可以直接插入的节点,若不是,则嵌套一个段落
if (n.getNodeType() != NodeType.TABLE && n.getNodeType() != NodeType.PARAGRAPH) {
if (parent == body) {
CompositeNode<Node> p = new Paragraph(doc);
body.appendChild(p);
parent = p;
}
}
if (n.getNodeType() == NodeType.BOOKMARK_START) {
BookmarkStart start = (BookmarkStart) n;
parent.appendChild(new BookmarkStart(doc, start.getName()));
continue;
} else if (n.getNodeType() == NodeType.BOOKMARK_END) {
BookmarkEnd end = (BookmarkEnd) n;
parent.appendChild(new BookmarkEnd(doc, end.getName()));
continue;
}
Node importNode = importer.importNode(n, true);
if(importNode.getNodeType() == NodeType.TABLE || importNode.getNodeType() == NodeType.PARAGRAPH){
body.appendChild(importNode);
}else{
parent.appendChild(importNode);
}
if (importNode instanceof Table) {
Table table = (Table) importNode;
//如果是设置的百分比宽度,关闭自适应属性
if (table.getPreferredWidth() != null && table.getPreferredWidth().getType() == PreferredWidthType.PERCENT) {
table.setAllowAutoFit(false);
}
}
//两个想领的table节点写入文档会变成一个table
if (lastNode instanceof Table && n instanceof Table) {
//插入一个换行隔断
doc.builder.moveToDocumentEnd();
doc.builder.writeln();
}
lastNode = n;
}
}
return doc;
}
private Node getPrevNodeBookmark(Node node){
Node n = node;
Node prev = node;
do {
n = n.getPreviousSibling();
if(n instanceof BookmarkStart){
prev = n;
}
}while (n != null);
return prev;
}
@SneakyThrows
public void findBookmarkNodeList(List<Node> nodeList, Bookmark bk, NodeCollection<?> allNodeList){
//处理书签位置重叠情况
Node startNode = getPrevNodeBookmark(bk.getBookmarkStart());
Node endNode = getNextNodeBookmark(bk.getBookmarkEnd());
if(startNode != null && endNode != null){
int startIndex = allNodeList.indexOf(startNode);
int endIndex = allNodeList.indexOf(endNode);
if(startIndex > -1 && endIndex > -1){
List<CompositeNode<?>> parentNodes = new ArrayList<>(20);
for (int i = startIndex ; i <= endIndex ; i++) {
boolean ret = false;
Node node = allNodeList.get(i);
for (CompositeNode<?> parentNode : parentNodes) {
NodeCollection<?> childNodes = parentNode.getChildNodes(node.getNodeType(), true);
if(childNodes.contains(node)){
ret = true;
break;
}
}
for (Node parentNode : nodeList) {
if(parentNode == node){
ret = true;
break;
}else if(parentNode.isComposite()){
CompositeNode<?> cn = (CompositeNode<?>) parentNode;
NodeCollection<?> childNodes = cn.getChildNodes(node.getNodeType(), true);
if(childNodes.contains(node)){
ret = true;
break;
}
}
}
if(ret){
continue;
}
Node insertNode = findInsertParent(node);
if(insertNode.isComposite()){
parentNodes.add((CompositeNode<?>) insertNode);
}
nodeList.add(insertNode);
}
}
}
}
private Node findInsertParent(Node node){
if(node instanceof Cell){
return ((Cell) node).getParentRow().getParentTable();
} else if(node instanceof Run){
return findInsertParent(node.getParentNode());
} else if(node instanceof Paragraph){
if(node.getParentNode() instanceof Cell){
return findInsertParent(node.getParentNode());
}else{
return node;
}
} else if(node instanceof BookmarkStart || node instanceof BookmarkEnd){
return node;
}else{
return node;
}
}
}
aspose.words提取所有word书签内容另存为新文件
于 2024-06-20 18:28:00 首次发布