java 读取word 文章标题

用POI读取docx doc文档样式时,当文档用标题(一级标题.....),其全篇字体都会变成-1,请教大神原因。

问题描述:用POI读取docx doc文档样式时,当文档用标题(一级标题.....),其全篇字体都会变成-1,请教大神原因。

预想效果:能获取含标题文档的样式(字体 字号 颜色等)

代码:

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class XwpfTest{
    public static void main(String[] args)throws Exception {
        InputStream is = new FileInputStream("D:\\test.docx");
        @SuppressWarnings("resource")
        XWPFDocument doc = new XWPFDocument(is);
        List<XWPFParagraph> paras = doc.getParagraphs();//将得到包含段落列表
        System.out.println("all data :" + paras.size());
        for(XWPFParagraph para : paras) {
            //当前段落的属性  
            //CTPPr pr = para.getCTP().getPPr(); 
            //System.out.println(para.getText());
            List<XWPFRun> runsLists = para.getRuns();//获取段楼中的句列表

            for(XWPFRun runsList : runsLists ){
                String c = runsList.getColor();//获取句的字体颜色
                float f = runsList.getFontSize();//获取句中字的大小
                String s = runsList.getText(0);//获取文本内容

                if(s != null) // 如果读取为非空,则对其进行判断
                {
                    if(s.contains("摘要"))// 识别摘要
                    {
                        System.out.println("right!");
                        runsList.setBold(true);
                    }
                    if(s.equals("摘要:")){
                        System.out.println("ddddddddddddddddddd");
                    }

                    if(s.contains("第一章")){
                        if(f != 16){
                            System.out.println("一级标题格式不是三号字体!");
                        }
                        System.out.println("一级标题!!!!!");
                    }
                }

                System.out.println("color:" + c);
                System.out.println("size:" + f);
                System.out.print("text:" + s);
                if(s != null){
                    System.out.print(",the length of string is " + s.length());
                }
                System.out.println("-----");
            }
        }
}
}

test.docx文档图片
test.docx文档样式

运行图片:
运行图片

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑点燃希望

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值