Changing appearances of widgets
See original GitHub issueThis is an amazing project! Clean code, nice documentation, extremely supportive on issues, etc. Impressive work.
I have a question regarding form widget annotations. The API to add new annotations is pretty straightforward.
My use case however is to manipulate existing ones.
I didn’t find much available on the documentation, but digging through the code, I started doing this:
const PDFLib = require('pdf-lib')
const fs = require('fs').promises
const { rgb } = PDFLib
const run = async () => {
const buffer = await fs.readFile(process.argv[2])
const doc = await PDFLib.PDFDocument.load(buffer)
const form = doc.getForm()
for(field of form.getFields()) {
if (field.constructor.name !== 'PDFTextField')
continue
field.setText('one punch man')
const [ widget ] = field.acroField.getWidgets()
const ac = widget.getAppearanceCharacteristics()
ac.setBackgroundColor(rgb(0,1,0))
widget.getBorderStyle().setWidth(10)
ac.setBorderColor(rgb(1,1,0))
}
await fs.writeFile(process.argv[3], await doc.save())
}
run()
The purpose of this script is to iterate through all forms and change the background color and border color of text fields.
The problem im facing is that the pdf saves fine, but none of the changes i made to widgets are effective. The input pdf and output pdf’s widget background colors and border colors are the same.
Any ideas?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Appearance Widgets Screen – WordPress.org Forums
You can add, rearrange and remove Widgets just like in Appearance Widgets Screen, but you also get a live preview of your changes....
Read more >How to add and edit widgets on your iPhone - Apple Support
Touch and hold a widget to open the quick actions menu. Tap Edit Widget Edit Widget icon . Make your changes, then tap...
Read more >How to change appearance of Widgets - YouTube
Here is how you can change the styling of the template or individual widgets.Content of this video0:00 - Intro0:17 - Apply Styling to...
Read more >Edit the Appearance/Layout of My Site with Menus and Widgets
To Edit Appearance, Widgets, and Themes: On the dashboard sidebar at the left-hand side of your screen, you will see a section named ......
Read more >Edit widget appearance - Product Documentation | ServiceNow
Edit widget appearance · Move the pointer to the upper right corner of the widget to show the editing controls. · Click the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hello @emilsedgh!
You need to use the
colorToComponents(...)function. Note also that the best way to check what subtype ofPDFFieldyou’re working with isinstanceof.I’ve updated the snippet you shared below:
This script produces a document that looks like the following:
<kbd>As you observed, pdf-lib does not provide high-level APIs for modifying the widget properties of existing form fields. So it is necessary to use low-level APIs that are not well documented. But I would very much like to add high-level APIs for this. If you’re interested in submitting a PR, I’d be happy to provide guidance!
Hey @Hopding Thank you so much for the fantastic library and great support.