PDF.js Express Plusplay_arrow

Professional PDF.js Viewing & Annotations - Try for free

side menu

Get Started

play_arrow

Learn more

play_arrow

Common use cases

play_arrow

Open a document

play_arrow

Save a document

play_arrow

Viewer

play_arrow

UI Customization

play_arrow

Annotations

play_arrow

Collaboration

play_arrow

Forms

play_arrow

Signature

play_arrow

Searching

play_arrow

Measurement

play_arrow

Compare

play_arrow

Advanced Capabilities

play_arrow

PDF.js Express REST API

play_arrow

Migration Guides

play_arrow

Customize PDF Form Field Styles with PDF.js Express

The following features are available in:

check

PDF.js Express Viewer

help_outline

PDF.js Express Viewer is a free viewer with limited capabilities compared to PDF.js Express Plus

check

PDF.js Express Plus

help_outline

PDF.js Express Plus is a commercial PDF SDK for viewing, annotating, signing, form filling and more

Change the color and opacity of PDF Form Fields using this PDF.js Express sample (no servers or other external dependencies required). This is commonly used to highlight required fields in a form (signature fields) or to make important fields easier to find on a page. View demo

WebViewer({
    path: '/static/WebViewer/lib/',
    pdftronServer: 'https://demo.pdftron.com/', // comment this out to do client-side only
    initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/form1.pdf',
  }, document.getElementById('viewer'))
    .then((instance) => {
      const { docViewer, annotManager, Annotations } = instance;

      docViewer.on('documentLoaded', () => {
        const pageCount = docViewer.getPageCount();
        const defaultStyles = Annotations.WidgetAnnotation.getCustomStyles;
        const customStyles =  (widget) => {
          if (widget instanceof Annotations.TextWidgetAnnotation) {
            if (widget.fieldName === 'f1-1') {
              return {
                'background-color': 'lightgreen',
              };
            }
            return {
              'background-color': 'lightblue',
              color: 'brown',
            };
          } if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
            return {
              'background-color': 'red',
              color: 'white',
            };
          }
        };

        document.getElementById('form').onchange =  (e) => {
          if (e.target.id === 'custom') {
          // Change styles for widget annotations
            Annotations.WidgetAnnotation.getCustomStyles = customStyles;
          } else {
            Annotations.WidgetAnnotation.getCustomStyles = defaultStyles;
          }
          for (let i = 0; i < pageCount; i++) {
          // Redraw canvas
            annotManager.drawAnnotations(i + 1, null, true);
          }
        };
      });
    });
WebViewer({
    path: '/static/WebViewer/lib/',
    pdftronServer: 'https://demo.pdftron.com/', // comment this out to do client-side only
    initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/form1.pdf',
  }, document.getElementById('viewer'))
    .then((instance) => {
      const { 
        documentViewer, 
        annotationManager, 
        Annotations 
      } = instance.Core;

      documentViewer.addEventListener('documentLoaded', () => {
        const pageCount = documentViewer.getPageCount();
        const defaultStyles = Annotations.WidgetAnnotation.getCustomStyles;
        const customStyles = (widget) => {
          if (widget instanceof Annotations.TextWidgetAnnotation) {
            if (widget.fieldName === 'f1-1') {
              return {
                'background-color': 'lightgreen',
              };
            }
            return {
              'background-color': 'lightblue',
              color: 'brown',
            };
          } if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
            return {
              'background-color': 'red',
              color: 'white',
            };
          }
        };

        document.getElementById('form').onchange =  (e) => {
          if (e.target.id === 'custom') {
          // Change styles for widget annotations
            Annotations.WidgetAnnotation.getCustomStyles = customStyles;
          } else {
            Annotations.WidgetAnnotation.getCustomStyles = defaultStyles;
          }
          for (let i = 0; i < pageCount; i++) {
          // Redraw canvas
            annotationManager.drawAnnotations(i + 1, null, true);
          }
        };
      });
    });
<!DOCTYPE html>
<html>
  <head>
    <meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
    <link rel='stylesheet' href='../../style.css'>
    <script src="/static/WebViewer/lib/webviewer.min.js"></script>
    <script src='../../old-browser-checker.js'></script>
  </head>
  <body>
    <header>
      <div className="title sample">Form fields customization sample</div>
    </header>
    <aside>
      <h1>Controls</h1>
      <h2>Form field styles</h2>
      <form id='form'>
        <input type='radio' name='form' id='default' checked />
        <label for='default'>Default</label>
        <br />
        <input type='radio' name='form' id='custom' />
        <label for='custom'>Custom</label>
      </form>
      <hr />
      <h1>Instructions</h1>
      <p>Use the buttons to change form field styles.</p>
    </aside>
    <div id='viewer'></div>
    <script src="../../menu-button.js"></script>
    <script src='form-fields.js'></script>

  </body>
</html>