question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing appearances of widgets

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Hopdingcommented, Nov 14, 2020

Hello @emilsedgh!

You need to use the colorToComponents(...) function. Note also that the best way to check what subtype of PDFField you’re working with is instanceof.

I’ve updated the snippet you shared below:

import { colorToComponents, PDFDocument, PDFTextField, rgb } from 'pdf-lib';

(async () => {
  const dodBytes = await fetch(
    'https://pdf-lib.js.org/assets/dod_character.pdf',
  ).then((res) => res.arrayBuffer());

  const pdfDoc = await PDFDocument.load(dodBytes);

  const form = pdfDoc.getForm();

  for (const field of form.getFields()) {
    if (field instanceof PDFTextField) {
      field.setText('one punch man');

      const [widget] = field.acroField.getWidgets();
      const ac = widget.getAppearanceCharacteristics();
      ac?.setBackgroundColor(colorToComponents(rgb(0, 1, 0)));

      widget.getBorderStyle()?.setWidth(5);
      ac?.setBorderColor(colorToComponents(rgb(1, 0, 0)));
    }
  }

  const pdfBytes = await pdfDoc.save();
})();

This script produces a document that looks like the following:

<kbd> </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!

2reactions
emilsedghcommented, Nov 14, 2020

Hey @Hopding Thank you so much for the fantastic library and great support.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found