• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jiangxincode / ApkToolBoxGUI / #1427

20 Apr 2026 03:04PM UTC coverage: 3.452% (-0.1%) from 3.572%
#1427

push

jiangxincode
add word pages stat

0 of 243 new or added lines in 6 files covered. (0.0%)

1 existing line in 1 file now uncovered.

250 of 7242 relevant lines covered (3.45%)

0.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/jiangxin/apktoolbox/word/WordUtils.java
1
package edu.jiangxin.apktoolbox.word;
2

3
import org.apache.logging.log4j.LogManager;
4
import org.apache.logging.log4j.Logger;
5
import org.apache.poi.hpsf.SummaryInformation;
6
import org.apache.poi.hwpf.HWPFDocument;
7
import org.apache.poi.xwpf.usermodel.XWPFDocument;
8

9
import java.io.File;
10
import java.io.FileInputStream;
11
import java.io.IOException;
12

13
public class WordUtils {
14

NEW
15
    private static final Logger LOGGER = LogManager.getLogger(WordUtils.class.getSimpleName());
×
16

17
    private WordUtils() {
18
        // utility class
19
    }
20

21
    /**
22
     * 读取 Word 文档的页数。
23
     * - .doc  使用 HWPFDocument(poi-scratchpad)
24
     * - .docx 使用 XWPFDocument(poi-ooxml)
25
     * 若读取失败或页数 <= 0,返回 0。
26
     *
27
     * @param file Word 文档文件
28
     * @return 文档页数,若无法读取则返回 0
29
     */
30
    public static int getPageCount(File file) {
NEW
31
        if (file == null || !file.exists() || !file.isFile()) {
×
NEW
32
            LOGGER.warn("Invalid file: {}", file);
×
NEW
33
            return 0;
×
34
        }
35

NEW
36
        String name = file.getName().toLowerCase();
×
NEW
37
        if (name.endsWith(".doc")) {
×
NEW
38
            return getDocPageCount(file);
×
NEW
39
        } else if (name.endsWith(".docx")) {
×
NEW
40
            return getDocxPageCount(file);
×
41
        } else {
NEW
42
            LOGGER.warn("Unsupported file type: {}", file.getPath());
×
NEW
43
            return 0;
×
44
        }
45
    }
46

47
    private static int getDocPageCount(File file) {
NEW
48
        try (FileInputStream fis = new FileInputStream(file);
×
NEW
49
             HWPFDocument document = new HWPFDocument(fis)) {
×
NEW
50
            SummaryInformation si = document.getSummaryInformation();
×
NEW
51
            int pageCount = (si != null) ? si.getPageCount() : 0;
×
NEW
52
            if (pageCount <= 0) {
×
NEW
53
                LOGGER.info("Page count <= 0 for .doc file: {}, returning 0", file.getPath());
×
NEW
54
                return 0;
×
55
            }
NEW
56
            LOGGER.info("Processing .doc file: {}, page count: {}", file.getPath(), pageCount);
×
NEW
57
            return pageCount;
×
NEW
58
        } catch (IOException e) {
×
NEW
59
            LOGGER.error("Error reading .doc file: {}, message: {}", file.getPath(), e.getMessage());
×
NEW
60
            return 0;
×
61
        }
62
    }
63

64
    private static int getDocxPageCount(File file) {
NEW
65
        try (FileInputStream fis = new FileInputStream(file);
×
NEW
66
             XWPFDocument document = new XWPFDocument(fis)) {
×
67
            // 优先从 ExtendedProperties 读取页数
NEW
68
            int pageCount = 0;
×
NEW
69
            if (document.getProperties() != null
×
NEW
70
                    && document.getProperties().getExtendedProperties() != null
×
NEW
71
                    && document.getProperties().getExtendedProperties().getUnderlyingProperties() != null) {
×
NEW
72
                pageCount = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
×
73
            }
74
            // 若 ExtendedProperties 无效,尝试从 CoreProperties 读取 revision 作为备选
NEW
75
            if (pageCount <= 0) {
×
NEW
76
                String revision = null;
×
NEW
77
                if (document.getProperties() != null
×
NEW
78
                        && document.getProperties().getCoreProperties() != null) {
×
NEW
79
                    revision = document.getProperties().getCoreProperties().getRevision();
×
80
                }
NEW
81
                if (revision != null && !revision.isEmpty()) {
×
82
                    try {
NEW
83
                        pageCount = Integer.parseInt(revision);
×
NEW
84
                    } catch (NumberFormatException e) {
×
NEW
85
                        LOGGER.warn("Cannot parse revision as page count for .docx file: {}", file.getPath());
×
NEW
86
                        pageCount = 0;
×
NEW
87
                    }
×
88
                }
89
            }
NEW
90
            if (pageCount <= 0) {
×
NEW
91
                LOGGER.info("Page count <= 0 for .docx file: {}, returning 0", file.getPath());
×
NEW
92
                return 0;
×
93
            }
NEW
94
            LOGGER.info("Processing .docx file: {}, page count: {}", file.getPath(), pageCount);
×
NEW
95
            return pageCount;
×
NEW
96
        } catch (IOException e) {
×
NEW
97
            LOGGER.error("Error reading .docx file: {}, message: {}", file.getPath(), e.getMessage());
×
NEW
98
            return 0;
×
99
        }
100
    }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc