阅读Adobe LiveCycle Designer创建的PDF中的表单字段

问题描述 投票:1回答:2

如何从this PDF file获取字段?它是由Adobe LiveCycle Designer创建的动态PDF。如果在Web浏览器中打开链接,则可能会看到一个从“请稍候...”开始的页面。如果下载文件并通过Adobe Reader(5.0或更高版本)打开,则应该看到全部8页。

因此,当通过PyPDF2进行读取时,您会得到一个空字典,因为它像通过Web浏览器看到的那样将文件呈现为单个页面。

def print_fields(path):
    from PyPDF2 import PdfFileReader
    reader = PdfFileReader(str(path))
    fields = reader.getFields()
    print(fields)

您可以使用依赖Java的库tika来读取所有8页的内容。但是结果是混乱的,我避免使用Java依赖性。

def read_via_tika(path):
    from tika import parser
    raw = parser.from_file(str(path))
    content = raw['content']
    print(content)

因此,基本上,我可以在Adobe Actobat DC中手动Edit -> Form Options -> Export Data…以获得漂亮的XML。同样,我需要通过Python获取漂亮的表单字段及其值。

python pdf
2个回答
2
投票

由于this awesome answer,我设法使用pdfminer.six来检索字段。

通过目录导航> AcroForm> XFA,然后在列表中pdfminer.pdftypes.resolve1元素之后的b'datasets'对象。


0
投票

就我而言,以下代码有效(来源:ankur garg

import PyPDF2 as pypdf
def findInDict(needle, haystack):
    for key in haystack.keys():
        try:
            value=haystack[key]
        except:
            continue
        if key==needle:
            return value
        if isinstance(value,dict):            
            x=findInDict(needle,value)            
            if x is not None:
                return x
pdfobject=open('CTRX_filled.pdf','rb')
pdf=pypdf.PdfFileReader(pdfobject)
xfa=findInDict('/XFA',pdf.resolvedObjects)
xml=xfa[7].getObject().getData()
© www.soinside.com 2019 - 2024. All rights reserved.