Using pdfbox to get form field values

前端 未结 2 1885
隐瞒了意图╮
隐瞒了意图╮ 2021-02-11 02:11

I\'m using pdfbox for the first time. Now I\'m reading something on the website Pdf

Summarizing I have a pdf like this:

2条回答
  •  耶瑟儿~
    2021-02-11 02:41

    The field can be a top-level field. So you need to loop until it is no longer a top-level field, then you can get the value. Code snippet below loops through all the fields and outputs the field names and values.

    {
        //from your original code
        PDDocument pdDoc = PDDocument.loadNonSeq( myFile, null );
        PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
        PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
    
    
        //get all fields in form
        List fields = acroForm.getFields();
        System.out.println(fields.size() + " top-level fields were found on the form");
    
        //inspect field values
        for (PDField field : fields)
        {
                processField(field, "|--", field.getPartialName());
        }
    
        ...
    }
    
    
    private void processField(PDField field, String sLevel, String sParent) throws IOException
    {
            String partialName = field.getPartialName();
    
            if (field instanceof PDNonTerminalField)
            {
                    if (!sParent.equals(field.getPartialName()))
                    {
                            if (partialName != null)
                            {
                                    sParent = sParent + "." + partialName;
                            }
                    }
                    System.out.println(sLevel + sParent);
    
                    for (PDField child : ((PDNonTerminalField)field).getChildren())
                    {
                            processField(child, "|  " + sLevel, sParent);
                    }
            }
            else
            {
                //field has no child. output the value
                    String fieldValue = field.getValueAsString();
                    StringBuilder outputString = new StringBuilder(sLevel);
                    outputString.append(sParent);
                    if (partialName != null)
                    {
                            outputString.append(".").append(partialName);
                    }
                    outputString.append(" = ").append(fieldValue);
                    outputString.append(",  type=").append(field.getClass().getName());
                    System.out.println(outputString);
            }
    }
    

提交回复
热议问题