> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/compare-files/semantic.md).

# Compare PDF Changes in Semantic Text Using JavaScript

Effortlessly compare semantic text elements like headers and paragraphs, generate difference summaries, and add highlights for easy review. Compare and annotate documents side by side with ease. Creat

{% hint style="info" %}
**Requirements**

*These packages are required to use these features in production. Trial keys have unlimited access to all features*

<a href="https://apryse.com/capabilities#Compare" class="button primary">Package: Compare</a><a href="https://showcase.apryse.com/semantic-text-compare" class="button primary">Live demo</a>
{% endhint %}

Compare changes in semantic categories, such as headers, paragraphs, numbers, and generate a document with a summary of differences for easy review.

## Highlighting differences

Using this method you can compare documents and add highlight annotations to mark the differences in side-by-side view.

![](https://3532544125-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX9YnTSKIHvV7m0A36LbO%2Fuploads%2Fgit-blob-7ab8e97f8d72deab87b0f3847a555d7b18130fd2%2F16116525dd93f4f1443fc9487390db937e13c8ae-2410x1318.png?alt=media)

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  fullAPI: true,
  disableMultiViewerComparison: true, // Disable default compare buttons
}, document.getElementById('viewer'))
.then((instance) => {
  const { UI, Core } = instance;
  const { Annotations } = Core;
  const { Color } = Annotations;
  instance.UI.openElements(['comparePanel']);

  UI.addEventListener(UI.Events.MULTI_VIEWER_READY, () => {
    const [documentViewer1, documentViewer2] = Core.getDocumentViewers();
    const startCompare = async () => {
      const shouldCompare = documentViewer1.getDocument() && documentViewer2.getDocument();
      if (shouldCompare) { // Check if both documents loaded before comparing
        const beforeColor = new Color(21, 205, 131, 0.4);
        const afterColor = new Color(255, 73, 73, 0.4);
        const options = { beforeColor, afterColor };
        const { doc1Annotations, doc2Annotations, diffCount } = await documentViewer1.startSemanticDiff(documentViewer2, options);
      }
    }
    documentViewer1.addEventListener('documentLoaded', startCompare);
    documentViewer2.addEventListener('documentLoaded', startCompare);
    documentViewer1.loadDocument('https://www.pdftron.com/compare1.pdf');
    documentViewer2.loadDocument('https://www.pdftron.com/compare2.pdf');
  });
  UI.enableFeatures([UI.Feature.MultiViewerMode]);
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Creating a new document with differences

Using this method you can create a new document that has alternating pages from both input documents with highlighted differences

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
const wvElement = document.getElementById('viewer');
WebViewer({
  fullAPI: true,
}, document.getElementById('viewer'))
.then(async instance => {
  const { documentViewer, PDFNet } = instance.Core;

  await PDFNet.initialize();

  const newDoc = await PDFNet.PDFDoc.create();
  await newDoc.lock();

  const doc1 = await PDFNet.PDFDoc.createFromURL('./files/semantic_test_doc_1.pdf');
  const doc2 = await PDFNet.PDFDoc.createFromURL('./files/semantic_test_doc_2.pdf');
  await newDoc.appendTextDiffDoc(doc1, doc2);

  await newDoc.unlock();

  instance.UI.loadDocument(newDoc);

  // wait until the document has been loaded
  documentViewer.addEventListener('documentLoaded', () => {
    instance.UI.setLayoutMode(instance.UI.LayoutMode.FacingContinuous);
  });
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

[TextDiffOptions](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.TextDiffOptions.html)

See our [semantic text compare interactive demo](https://showcase.apryse.com/semantic-text-compare) to try it out now, or start coding now with the [semantic compare sample](/web/get-started/samples/diff.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/web/compare-files/semantic.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
