选择Python(PYPDF2)的PDF中的复选框
我正在尝试使填充此PDF自动化: (用ilovepdf解密)。
我对文本没有任何问题,但是使用复选框没有办法。许多 /btn有 /孩子 /孩子是其他复选框,看起来为“ indirectobject”。 另一个复选框,我无法选择/修改,我无法在此PDF(示例)
代码
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject
from collections import OrderedDict
def set_need_appearances_writer(writer: PdfFileWriter):
# See 12.7.2 and 7.7.2 for more information: http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
try:
catalog = writer._root_object
# get the AcroForm tree
if "/AcroForm" not in catalog:
writer._root_object.update({
NameObject("/AcroForm"): IndirectObject(len(writer._objects), 0, writer)
})
need_appearances = NameObject("/NeedAppearances")
writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
# del writer._root_object["/AcroForm"]['NeedAppearances']
return writer
except Exception as e:
print('set_need_appearances_writer() catch : ', repr(e))
return writer
reader = PdfFileReader("TEMPORAL COMPLETO12 de mayo_unlocked.pdf")
writer = PdfFileWriter()
set_need_appearances_writer(writer)
page = reader.pages[0]
writer.addPage(page)
#Texto4 works, but not the checkboxes
writer.updatePageFormFieldValues(
writer.getPage(0), {'BOTON_TIPOJORNADA': '/1',
'BOTON_JORN': '/S',
'Texto4': 'Texto4'
}
)
with open("filled-out.pdf", "wb") as output_stream:
writer.write(output_stream)
reader.stream.close()
另外,如果我手动修改PDF并读取字段...:
reader.getFields()
OUTPUT (one checkbox selected):
[...]
'BOTON_JORN': {'/FT': '/Btn',
'/Kids': [IndirectObject(160, 0),
IndirectObject(162, 0),
IndirectObject(167, 0),
IndirectObject(172, 0)],
'/T': 'BOTON_JORN',
'/Ff': 49152,
'/V': '/S'},
OUTPUT (another checkbox selected):
[...]
'BOTON_JORN': {'/FT': '/Btn',
'/Kids': [IndirectObject(160, 0),
IndirectObject(162, 0),
IndirectObject(167, 0),
IndirectObject(172, 0)],
'/T': 'BOTON_JORN',
'/Ff': 49152,
'/V': '/D'},
中选择/修改普通复选框。 “ textocasilla deverificación25”当选择具有值时'/s#ed'
'TEXTOCasilla de verificación25': {'/FT': '/Btn',
'/T': 'TEXTOCasilla de verificación25',
'/V': '/S#ED'},
任何提示都很棒
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总是困惑程序员为什么需要销毁形式模板,它是一个空白的画布,没有程序只有观众/打印机,因此您可以填充FDF并打开FDF,将自动编写PDF,并用所有新的字段输入值,包括按钮,复选框或其他选择。
/v(我对PDF的输入值)
包括Python的库,包括Python的库来导出FDF,以通过服务器或命令行填充fdf (甚至通过MS Notepad进行手动编辑)或查找并替换文本值。有关一个有力的例子,请参见 https://www.pdftron.com/documentation com.com/documentation/samampels/ py/fdftest/但是,在github上,搜索
fdf pdf
应该扔掉数十个。注意以上样本将需要过度限制的导出限制。
写作或编辑文本FDF条目后,只需打开FDF即使限制了相关的空白模板。
在这里打开数据文件,我需要确认其允许的
但我们可以看到我编写的数据(在这种情况下为字段名称),已导入
: - 在导入并非每个字段都需要添加时,但是您确实需要根据Resave填充所有字段。
Always confused why programmers need to destroy form templates, it is a blank canvas to be populated without a program only a viewer/printer so you fill the FDF and opening the FDF will automatically write the PDF, with all the new field entry values, including buttons, check boxes or other selections.
/V (My Inputted value for the PDF)
There are many libraries including Python ones to export the FDF for populating via server or command line (even manual edit via MS NotePad) or Find and Replace a text value. For a powerful example see https://www.pdftron.com/documentation/samples/py/FDFTest/ but on github a search of
FDF PDF
should throw up dozens.Note the sample above will need the export restriction over-ruled.
Once you write or edit the text FDF entries, then simply opening the FDF will auto populate the associated blank template.pdf, even if it was restricted.
Here opening data file I need to confirm its allowed
But we can see the data I write (in this case the field name), has been imported
Note:- when importing not every field needs to be added, but you do need to fill all fields as required for a resave.