aspose.words提取所有word书签内容另存为新文件


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;
            for (Node n : nodeList) {
                if(n.getNodeType() != NodeType.TABLE && n.getNodeType() != NodeType.PARAGRAPH){
                    continue;
                }
                Node importNode = importer.importNode(n, true);
                body.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;
    }


    @SneakyThrows
    public void findBookmarkNodeList(List<Node> nodeList, Bookmark bk, NodeCollection<?> allNodeList){
        Node startNode = bk.getBookmarkStart();
        Node endNode = 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;
        }
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值