Skip to content

BUG: Fix field's appearance problem#434

Closed
liuzhuoling2011 wants to merge 6 commits into
py-pdf:mainfrom
liuzhuoling2011:master
Closed

BUG: Fix field's appearance problem#434
liuzhuoling2011 wants to merge 6 commits into
py-pdf:mainfrom
liuzhuoling2011:master

Conversation

@liuzhuoling2011

@liuzhuoling2011 liuzhuoling2011 commented Jun 1, 2018

Copy link
Copy Markdown

I have tried many python pdf-related tools, none of them work correctly for me.
Finally I found this, it meet all the needs but have some small problem such as:

  1. data filled in can't be seen until you click the field (appearance problem)
  2. check box can't be selected

to make better user experience, I made some change~
now, use this library is more easier, just need a little customer code!

from PyPDF2 import PdfFileWriter, PdfFileReader

def fill_pdf_with_dict(input_file, output_file, dict_data):
    pdf_reader = PdfFileReader(input_file, strict=False)
    pdf_writer = PdfFileWriter()

    for i in range(pdf_reader.getNumPages()):
        page = pdf_reader.getPage(i)
        pdf_writer.updatePageFormFieldValues(page, dict_data[i])
        pdf_writer.addPage(page)

    with open(output_file, 'wb') as outputStream:
        pdf_writer.write(outputStream)

@mwhit74

mwhit74 commented Jul 25, 2018

Copy link
Copy Markdown
Contributor

A fix for the appearance was started a while ago (#412) , just never got incorporated into the code base. I don't think it is significantly than the fix given here.

I am confused why the /NeedAppearances is updated in the PdfFileReader class in this solution.

@MartinThoma MartinThoma added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label Apr 6, 2022
@MartinThoma MartinThoma changed the title fix field's appearance problem && fix check box can't be selected pro… Fix field's appearance problem Apr 16, 2022
Comment thread PyPDF2/pdf.py Outdated
@MartinThoma

Copy link
Copy Markdown
Member

It seems as if this PR breaks something

@MartinThoma MartinThoma added needs-change The PR/issue cannot be handled as issue and needs to be improved workflow-forms From a users perspective, forms is the affected feature/workflow labels Apr 17, 2022
Comment thread PyPDF2/pdf.py Outdated
@MartinThoma MartinThoma changed the title Fix field's appearance problem BUG: Fix field's appearance problem Jun 25, 2022
@MartinThoma MartinThoma added the needs-rebase This PR cannot be merged as the main branch is too different. You need to rebase or merge main. label Jun 25, 2022
Comment thread PyPDF2/pdf.py
Comment on lines +1203 to +1207
if "/AcroForm" in self.trailer["/Root"]:
self.trailer["/Root"]["/AcroForm"].update({
NameObject("/NeedAppearances"): BooleanObject(True)
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if "/AcroForm" in self.trailer["/Root"]:
self.trailer["/Root"]["/AcroForm"].update({
NameObject("/NeedAppearances"): BooleanObject(True)
})

We now have writer.set_need_appearances_writer() which is used in writer.update_page_form_field_values

@MartinThoma

Copy link
Copy Markdown
Member

Note for myself:

AS: The annotation’s appearance state, which selects the applicable
appearance stream from an appearance subdictionary

MartinThoma added a commit that referenced this pull request Jul 24, 2022
MartinThoma added a commit that referenced this pull request Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF needs-change The PR/issue cannot be handled as issue and needs to be improved needs-rebase This PR cannot be merged as the main branch is too different. You need to rebase or merge main. workflow-forms From a users perspective, forms is the affected feature/workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants