For the complete documentation index, see llms.txt. This page is also available as Markdown.

Customize Salesforce with WebViewer and Lightning Web Component (LWC)

Our Salesforce implementation integrates Apryse WebViewer inside a Lightning Web Component (LWC).

The integration allows for bi‑directional communication:

  • LWC to WebViewer via postMessage to the iframe.

  • WebViewer to LWC via postMessage back to the parent window.

This allows Salesforce data and WebViewer actions to stay in sync (e.g., loading documents, updating fields, tracking annotations).

This article highlights entry point of editing when you want to manipulate the data being sent to WebViewer and the information being sent back. This guide also includes how Salesforce and WebViewer communicate.

Architecture

  1. Lightning Web Component (LWC)

  • Hosts an <iframe> that loads WebViewer.

  • Uses postMessage to send commands and Salesforce payloads into WebViewer.

  • Listens for messages coming back from WebViewer.

  1. WebViewer Config File

  • Runs inside the iframe.

  • Registers window.addEventListener('message', ...) to receive messages from LWC.

  • Uses a switch statement to map incoming message types to WebViewer APIs.

  • Uses parent.postMessage(...) to send information back to the LWC.

Message Flow

Example for Load Document Flow

  1. LWC sends LOAD_DOCUMENT.

  2. WebViewer receives and loads document.

  3. WebViewer sends back DOCUMENT_LOADED.

  4. LWC receives the DOCUMENT_LOADED message.

LWC to WebViewer, Send Messages

In the LWC, you’ll typically:

  1. Reference the iframe.

  2. Call postMessage on its contentWindow.

Example for Send a Command from LWC

Typical fields include:

  • Type: String that identifies the action (used in WebViewer’s switch).

  • Payload: Object containing Salesforce-specific data (record IDs, URLs, metadata, etc.).

WebViewer, Listen for Messages from LWC

In the WebViewer config file (e.g., config_apex.js), you’ll set up a message listener.

Basic Listener Structure

Example for Call WebViewer APIs

You can map any message type to WebViewer APIs. For example:

  • Load a document.

  • Jump to a page.

  • Toggle read-only.

  • Import/export annotations.

  • Trigger save events.

WebViewer to LWC, Send Messages Back

From within the WebViewer iframe, parent gives you access to the LWC’s window. You can send information back using parent.postMessage.

Common Use Cases

  • Notify LWC that:

    • Document finished loading.

    • User created/edited/deleted annotations.

    • User saved or signed a document.

  • Send structured payloads (e.g., XFDF, page numbers, flags).

Example, Send Data to LWC

You might call this from WebViewer event handlers:

LWC, Receive Messages from WebViewer

On the Salesforce side, you’ll register a message event listener on window in the LWC.

Example, Handle Messages from WebViewer in LWC

Message Contract Design

To keep things maintainable:

  • Use clear type names (e.g., LOAD_DOCUMENT, SAVE_ANNOTATIONS, SET_READONLY).

  • Structure payload as a stable contract:

    • Document info (Blob, URL).

    • Salesforce identifiers (recordId, related object Ids).

    • Flags and options (read-only, mode).

  • Document all supported message types in your guide, including:

    • Direction (LWC to WebViewer, WebViewer to LWC).

    • Expected payload fields.

    • Behavior triggered.

Last updated

Was this helpful?