zoukankan      html  css  js  c++  java
  • Java 读取Word中的脚注、尾注

    本文介绍读取Word中的脚注及尾注的方法,添加脚注、尾注可以参考这篇文章

    :本文使用了Word类库(Free Spire.Doc for Java 免费版)来读取,获取该类库可通过官网下载,并解压文件,将lib文件夹下的Spire.Doc.jar导入java程序;或者通过maven仓库安装导入

    jar导入效果如下:

    测试文档如下,包含脚注及尾注:

    1. 读取Word脚注

    import com.spire.doc.*;
    import com.spire.doc.documents.Paragraph;
    import com.spire.doc.fields.Footnote;
    import com.spire.doc.fields.TextRange;
    
    import java.util.List;
    
    public class ExtractFootnoteAndEndnote {
        public static void main(String[] args) {
            //创建Document实例
            Document doc = new Document();
            doc.loadFromFile("test1.docx");
    
            //获取文档中的所有脚注
           List<Footnote> footNotes = doc.getFootnotes();
    
           //实例化String类型变量
           String str = "";
    
           //遍历脚注
            for (Footnote footNote :footNotes) {
                //遍历脚注中的段落
                for (int j = 0; j < footNote.getTextBody().getParagraphs().getCount(); j++) {
                    Paragraph paragraph = footNote.getTextBody().getParagraphs().get(j);
                    //遍历段落中的对象
                   for(Object object : paragraph.getChildObjects()){
                       //读取文本
                       if (object instanceof TextRange) {
                           TextRange textRange = (TextRange) object;
                           str = str + textRange.getText();
                       }
                   }
    
                }
            }
            //输出脚注文本
            System.out.println(str);
        }
    }

    脚注读取结果:

     

    2. 读取Word尾注

    import com.spire.doc.*;
    import com.spire.doc.documents.Paragraph;
    import com.spire.doc.fields.Footnote;
    import com.spire.doc.fields.TextRange;
    
    import java.util.List;
    
    public class ExtractFootnoteAndEndnote {
        public static void main(String[] args) {
            //创建Document实例
            Document doc = new Document();
            doc.loadFromFile("test1.docx");
    
           //获取所有尾注
            List<Footnote> endNotes = doc.getEndnotes();
            //实例化String类型变量
            String str = "";
    
            //遍历尾注
            for (Footnote endnote :endNotes) {
                //遍历尾注中的段落
                for (int j = 0; j < endnote.getTextBody().getParagraphs().getCount(); j++) {
                    Paragraph paragraph = endnote.getTextBody().getParagraphs().get(j);
                    //遍历段落中的对象
                    for(Object object : paragraph.getChildObjects()){
                        //读取文本
                        if (object instanceof TextRange) {
                            TextRange textRange = (TextRange) object;
                            str = str + textRange.getText();
                        }
                    }
                }
            }
            //输出尾注文本
            System.out.println(str);
        }
    }

    尾注读取结果:

  • 相关阅读:
    黑马乐优商城项目总结
    SpringDataJpa学习(3)——SpringDataJpa的多表映射和动态查询
    SpringDataJpa学习(2)——SpringDataJpa的单表使用
    SpringDataJpa学习(1)——Jpa学习
    个人博客06
    个人博客05
    个人博客04
    典型用户和用户场景描述
    个人博客03
    个人博客02
  • 原文地址:https://www.cnblogs.com/Yesi/p/12448959.html
Copyright © 2011-2022 走看看