pmaupin / pdfrw

pdfrw is a pure Python library that reads and writes PDFs
Other
1.86k stars 271 forks source link

Form push button widget creation does not work #243

Closed sanksara closed 7 months ago

sanksara commented 8 months ago

I am trying to create a form field button as per code below. However the line annot.Ff = 19 doesnot set the button to a push button. without the Field flag setting, it defaults to a check box.

from pdfrw import PdfName
from pdfrw import PdfDict

# Open the PDF file
reader = pdfrw.PdfReader("c:/test/button.pdf")

# Get the first page
page = reader.pages[0]

# Create a new annotation object for the push button
annot = PdfDict()
annot.Subtype = pdfrw.PdfName.Widget
annot.T = "SubmitButton"  # Set the annotation name
annot.Ff = 19
# annot.F = 4  # Set the annotation flags
annot.Rect = [100, 100, 10, 10]  # Set the annotation rectangle
annot.FT = pdfrw.PdfName.Btn  # Set the form field type to button
annot.MK = pdfrw.PdfDict(BG=[0.5, 0.5, 0.5], CA="Submit")  # Customize button appearance
annot.H = PdfName.N  # Set the highlighting mode to none
annot.V = PdfName.Yes  # Set the value to Yes
annot.V = True
annot.AS = PdfName.Yes  # Set the appearance state to Yes
sl2c commented 7 months ago

Your code does not include the part that saves the result. In any case, a debugging strategy that works for me is to try to save the file, read it again and print out the annotations dictionary of the re-read file. This will show if your changes are being saved correctly.

Regarding the specific Push Button issue: Table 8.75 in Adobe Pdf Reference v. 1.7 specifies the bit position of 17 in annot.Ff for push buttons. The value you assign (19) has the 17th bit value equal to zero.

Also, you seem to assign annot.V twice, the second of the assignments being to bool, which is incorrect (the first assignment is correct, in fact).

sanksara commented 7 months ago

Thanks Noted @sl2c