从 word 转换的 pdf 中读取复选框值

Reading checkbox value from pdf which converted from word

document-checkbox我有从 word(另存为 pdf)转换为 pdf 的 PDF 文件,在 word 中我们有几个选中 selected/not 的复选框,转换后的 pdf 显示为复选框,但是,它们是不是复选框,也不是图像。

我需要读取这些复选框值(selected/not 选中),但我无法读取这些值。 我正在尝试使用 PDFBOX。我以为这些复选框是图像 – 试图提取 pdf 中的所有图像,但这些(显示为)复选框不是图像。

我想知道如何将这些复选框保存为 PDF,请告诉我如何读取这些复选框值?

提出任何 API 的建议 – 我会尝试相同的。

谢谢 大雅

当您将包含 word 表单字段的 word 文档转换为 PDF(使用另存为 *.pdf)时,不幸的是没有从中创建 pdf 表单字段。 (这会很整洁)。复选框存储为 MS Gothic 字体的字符。所以如果你想提取它们,你需要提取 PDF 的文本。复选框可以有两个状态,因此有两个字符:

☐ - Unicode 2610

☒ - Unicode 2612

一些示例代码:

public static void main(String args[]) throws IOException {
    InputStream pdfIs = //load your PDF
    RandomAccessBufferedFileInputStream rbfi = new RandomAccessBufferedFileInputStream(pdfIs);

    PDFParser parser = new PDFParser(rbfi);
    parser.parse();
    try (COSDocument cosDoc = parser.getDocument()) {
        PDFTextStripper pdfStripper = new PDFTextStripper();
        PDDocument pdDoc = new PDDocument(cosDoc);
        String parsedText = pdfStripper.getText(pdDoc);
        //System.out.println("Full text"+parsedText);

        for (int i = 0; i < parsedText.length(); i++) {
            if('☒'==parsedText.charAt(i)) {
                System.out.println("Found a checked box at index "+i);
                System.out.println("\u" + Integer.toHexString(parsedText.charAt(i) | 0x10000).substring(1));
            }
            else if('☐'==parsedText.charAt(i)) {
                System.out.println("Found an unchecked box at index "+i);
                System.out.println("\u" + Integer.toHexString(parsedText.charAt(i) | 0x10000).substring(1));
            }
            //else {//skip}
        }            
    }
}

更新:

您提供了示例 PDF。复选框以 "drawing" 的形式存储为 xobject 流。如果您查看页面对象,内容条目会为您指明正确的方向: 3 0 obj << /Type /Page /Contents 4 0 R ... 您会在 4 0 obj 中找到以以下内容开头的内容:

4 0 obj
<<
/Length 807
>>
stream
 /P <</MCID 0>> BDC q
0.00000912 0 612 792 re
W* n
BT
/F1 9.96 Tf
1 0 0 1 72.024 710.62 Tm
/GS7 gs
0 g
/GS8 gs
0 G
[( )] TJ
ET
Q
 EMC q
0.000018243 0 612 792 re
W* n
 /P <</MCID 1>> BDC 0.72 w
0 G
 1 j 
73.104 696.34 9.24 9.24 re
S
0.48 w

72.984 705.7 m
82.464 696.22 l
S

82.464 705.7 m
72.984 696.22 l
S
Q
 EMC  /P <</MCID 2>> BDC q
0.00000912 0 612 792 re
W* n
BT
/F1 9.96 Tf
1 0 0 1 83.544 697.3 Tm
0 g
0 G
[( )] TJ
ET

这基本上就是复选框的绘制方式。您现在可以使用 pdfbox 阅读此内容,但您必须自己解释/识别它。查看 PDF 规范如何解释这些绘图说明...