python下的pdf操作,pypdf2为不二之选,除了...这个:
用这段代码(网上一把把的)来填充pdf中的form field,表单域
# -*- coding: UTF-8 -*- from PyPDF2 import PdfFileWriter, PdfFileReader infile = "mb2.pdf" outfile = "c.pdf" pdf = PdfFileReader(open(infile, "rb"), strict=False) pdf2 = PdfFileWriter() field_dictionary = {'idnumber':'11ddas111','now_year':'2018-2-1','name':'好的'} pdf2.addPage(pdf.getPage(0)) pdf2.updatePageFormFieldValues(pdf2.getPage(0), field_dictionary) outputStream = open(outfile, "wb") pdf2.write(outputStream)
结果产生的pdf,用acrobat reader打开时不显示表单域的内容,只有在鼠标点击后才能显示,失去焦点后再次消失,只有重新copy paste之后才能显示:
当鼠标点击后会显示:
网上遇到类似情况的也不少:
https://stackoverflow.com/questions/47369740/pypdf2-appends-the-same-file-over-and-over 修改fields 提及,pypdf2 表单域不能在acrobat reader中显示,,不能填充cb和rb:
PyPdf2 seems to be the best option despite all the bugs python packages have for pdfs such as fields not showing in acroreader and being unable to fill checkboxes or radio buttons.
There does appear to be a bug (with pdfs generally? maybe) where the pdf file is not redrawn. If one clicks on the field one can see the new text that PyPDF2 entered however one then has to manually copy and paste in order to see that change permanently.
最终在这个issue里找到了解决方案:
https://github.com/mstamy2/PyPDF2/issues/355
and thanks this great guy (https://github.com/ademidun) 给出了参考:
Okay, I think I have figured it out. If you read section 12.7.2 (page 431) of the PDF 1.7 specification, you will see that you need to set the NeedAppearances flag of the Acroform.
ok,我们不生产代码,只做代码的搬运工:D
如下是解决方案:
# -*- coding: UTF-8 -*- from PyPDF2 import PdfFileWriter, PdfFileReader from PyPDF2.generic import BooleanObject, NameObject, IndirectObject def set_need_appearances_writer(writer): # 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) return writer except Exception as e: print('set_need_appearances_writer() catch : ', repr(e)) return writer infile = "mb2.pdf" outfile = "c.pdf" pdf = PdfFileReader(open(infile, "rb"), strict=False) if "/AcroForm" in pdf.trailer["/Root"]: pdf.trailer["/Root"]["/AcroForm"].update( {NameObject("/NeedAppearances"): BooleanObject(True)}) pdf2 = PdfFileWriter() set_need_appearances_writer(pdf2) if "/AcroForm" in pdf2._root_object: pdf2._root_object["/AcroForm"].update( {NameObject("/NeedAppearances"): BooleanObject(True)}) field_dictionary = {'idnumber':'11ddas111','now_year':'2018-2-1','name':'好的'} pdf2.addPage(pdf.getPage(0)) pdf2.updatePageFormFieldValues(pdf2.getPage(0), field_dictionary) outputStream = open(outfile, "wb") pdf2.write(outputStream)
相关推荐
在本篇文章中小编给大家分享了关于Java创建和填充PDF表单域方法和步骤,有需要的朋友们学习下。
jq json 填充 form,根据名字匹配
itextpdf 再创将pdf格式文件的时候中文不显示或者乱码问题的解决 其实目前最新版本的itexpdf即使加了asian的辅助包也不能解决中文不显示问题 因为自己试过 可能方式不对 这个绝对显示 显示不了的 分不要
内含联系方式,不懂用可以问我!多次修改后已无瑕疵,用它实现当页面刷新的时候自动提交表单数据,目前我把它运用在领取某论坛每日登录礼包这种繁琐的小事中,你还可以运用到更有价值的地方去。
解决办法是在form上或input上添加autoComplete=”off”这个属性。 form表单的属性如下所示: 但是这个解决方案在谷歌和火狐上均有bug,下面来一个一个解决。 1.’autocomplete=”off”‘在Chrome中不起作用解决方案 ...
html,表单自动填充,避免重复性工作,一个一个填写,复制粘贴到文本域,自动识别对应表单属性,复制之后自动识别,点击提交就行了。超级简单,有什么不懂的可以直接私信问我。
封装好的工具类,静态方法直接调用,支持pdf转图片,可设置清晰度。支持模板pdf域的填充。传入pdf文件路径和map,调用方法即可将map内容填充到pdf,并重新生成新的pdf文件
例如有些标签并没有放在表单内我们的fillform也可以处理,而autofill forms是无法处理表单之外的标签的。 * 可以使用历史数据对未保存过信息的网址进行填写。有时一个相同的网页有着不同的网址,fillform只要保存一...
借助ITextSharp,动态填充表单内容并输出,支持显示中文.资源中包含一些中文字体.可直接使用
CAD填充比例不一致的问题CAD填充比例不一致的问题CAD填充比例不一致的问题CAD填充比例不一致的问题
jquery.formautofill 表单填充控件,在平时使用页面表单或者弹出窗口需要赋值的时候通常情况要一个一个的赋值,有了这个控件可以实现一句话就轻松搞定。
多边形的区域填充 学时:2 实验类型:设计性 实验类别:专业实验 实验目的 1. 通过实验,进一步理解和掌握几种常用多边形填充算法的基本原理 2. 掌握多边形区域填充算法的基本...4. 实验总结(含问题分析及解决方法)
AutoCAD_填充图案_钢筋砼(2),AutoCAD_填充图案_钢筋砼(2),AutoCAD_填充图案_钢筋砼(2),AutoCAD_填充图案_钢筋砼(2),AutoCAD_填充图案_钢筋砼(2),AutoCAD_填充图案_钢筋砼(2),AutoCAD_填充图案_...
CSS制作表单技术 css 网页技术
代码实现用excel表格中数据自动填充 网页表单
用的是ajax control toolkit里的AutoCompleteExtender来实现textbox的自动填充表单,其中自动填充表单的信息来自sqlserver数据库
本代码基于CAD VBA进行开发,用于提取填充图案("AcDbHatch")和面域("AcDbRegion")的坐标,当填充图案没有关联环的情况下,能自动创建关联环。 网上这类功能的例子很少,但在数据建库、图形分析以及CAD与ARCMAP...
分享卓贝网络整理的 解决图片在图片框中填充显示 的jQuery插件,绝对好用。
画不规则(闭合)图形、填充不规则图形、右键填充 填充方式有:种子填充、扫描填充、系统填充 默认为种子填充
题目:用种子填充算法(或扫描线填充算法)填充任一多边形域 基本要求: (1)数据输入项为:多边形的顶点数、各顶点x,y坐标。 对于种子填充算法要输入种子象素的x,y坐标。 对于扫描线填充算法要输入扫描线间距。 ...