Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing properties of a field #2559

Open
ReedGraff opened this issue Mar 30, 2024 · 4 comments
Open

Editing properties of a field #2559

ReedGraff opened this issue Mar 30, 2024 · 4 comments
Labels
workflow-forms From a users perspective, forms is the affected feature/workflow

Comments

@ReedGraff
Copy link

Explanation

I'd love to be able to change field properties. Specifically, in Adobe, it is possible to change the background color of a signature field to yellow, red, or another color for example. If someone could point me in the right direction I would happily make this myself.

image
image

Code Example

I assume this could be done two ways, by extending or appending functionality to the update_ function, or by creating a new function in its entirety. The latter of which is probably easier.

from pypdf import PdfReader, PdfWriter

reader = PdfReader("SOMEPATH")
writer = PdfWriter()
writer.append(reader)

for page in range(len(reader.pages)):
    writer.update_page_form_field_properties(
    writer.pages[page],
    {
          'field1': {"background": "#ffffff", "font": "Helvetica"}, 
          'field2': {"fontSize": "14pt", "color": "#000000"},
    }
)
@pubpub-zz
Copy link
Collaborator

pubpub-zz commented Mar 30, 2024

@ReedGraff
you can change font size and color adjusting /DA field before filling up (using PDF or any viewers) before. a method to change it has been drafted in #2064. Color could be introduced too
Changing font was also planned there however this is more tricky and is currently blocking the PR I've started.
for background color I have no find a way to do that.

Do you have any example of such documents ?

@stefan6419846 stefan6419846 added the workflow-forms From a users perspective, forms is the affected feature/workflow label Mar 30, 2024
@ReedGraff
Copy link
Author

Here's a demo in Adobe illustrating what I'd like to be able to do in pypdf.
BgFillDemo

Here's the example PDF.
HighlightingDemo.pdf

@stefan6419846
Copy link
Collaborator

stefan6419846 commented Mar 30, 2024

Looking at the provided PDF file (in this case the uncompressed content stream version from pdftk, but should be similar in pypdf itself), we have (original xobj is 6):

113 0 obj

<<
/TU (Contact:)
/P 7 0 R
/Subtype /Widget
/T (Contact)
/F 4
/Type /Annot
/DA (/Helv  0 Tf 0 g)
/DR 
<<
/Font 
<<
/Helv 134 0 R
>>
/Encoding 
<<
/PDFDocEncoding 135 0 R
>>
>>
/FT /Tx
/MK 
<<
/BG [0.988235 0.956863 0.521576]
>>
/AP 
<<
/N 86 0 R
>>
/Rect [363.96 678.24 568.68 695.04]
>>
endobj

The most important difference to a regular field is

/MK 
<<
/BG [0.988235 0.956863 0.521576]
>>

in this case: /MK defines the "appearance characteristics dictionary" of the annotation (see table 188 of the PDF 1.7 specification) and the corresponding /BG key of this dictionary the "colour of the widget" (see table 189 of the PDF 1.7 specification).

For comparison: This is how the not highlighted phone field is defined (the original xobj is 12):

114 0 obj

<<
/TU (Phone:)
/P 7 0 R
/Subtype /Widget
/T (Phone)
/F 4
/Type /Annot
/DA (/Helv  0 Tf 0 g)
/DR 
<<
/Font 
<<
/Helv 134 0 R
>>
/Encoding 
<<
/PDFDocEncoding 135 0 R
>>
>>
/FT /Tx
/AP 
<<
/N 87 0 R
>>
/Rect [358.08 660 568.68 676.8]
>>
endobj

@ReedGraff
Copy link
Author

Interesting, so how might I be able to create a function for this on top of the existing library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
workflow-forms From a users perspective, forms is the affected feature/workflow
Projects
None yet
Development

No branches or pull requests

3 participants