how to get field page in PDFBox API 2?

前端 未结 1 1977
[愿得一人]
[愿得一人] 2020-12-21 11:02

i\'m trying to get the field page in my project, and i dont know how to get the page number for each field and field. i have this code:

    String formTempla         


        
相关标签:
1条回答
  • 2020-12-21 11:46

    This will show you on what page(s) (0-based) the field appears:

    PDField field = acroForm.getField( "date" );
    for (PDAnnotationWidget widget : field.getWidgets())
    {
        PDPage page = widget.getPage();
        if (page == null)
        {
            // incorrect PDF. Plan B: try all pages to check the annotations.
            for (int p = 0; p < doc.getNumberOfPages(); ++p)
            {
                List<PDAnnotation> annotations = doc.getPage(p).getAnnotations();
                for (PDAnnotation ann : annotations)
                {
                    if (ann.getCOSObject() == widget.getCOSObject())
                    {
                        System.out.println("found at page: " + p);
                        break;
                    }
                }
            }
            continue;
        }
        int pageNum = pdfDocument.getPages().indexOf(page);
        System.out.println("found at page: " + pageNum);
    }
    
    0 讨论(0)
提交回复
热议问题