# Merge Rest API - how to flatten PDF?

**URL:** <https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328>\
**Category:** Technical Support\
**Created:** [October 12, 2020, 9:09am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328 "2020-10-12T09:09:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![appdevsa](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@appdevsa](https://pdfjs.community/u/appdevsa)\
**Post date:** [October 12, 2020, 9:09am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/1 "2020-10-12T09:09:32Z")

</div>

Hi, is there a way to “flatten” a PDF. After the merge all annotations are editable in the output PDF. I would like it to be readonly and almost form part of the PDF and not appear as annotations…hope this makes sense.

---

<div class="post-metadata">

**Author:** ![kiran.patil](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kiran.patil](https://pdfjs.community/u/kiran.patil)\
**Post date:** [October 13, 2020, 10:43am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/2 "2020-10-13T10:43:01Z")

</div>

Hi appdevsa,

Can you tell me how you are outputting the pdf?

---

<div class="post-metadata">

**Author:** ![appdevsa](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@appdevsa](https://pdfjs.community/u/appdevsa)\
**Post date:** [October 13, 2020, 11:19am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/3 "2020-10-13T11:19:26Z")

</div>

Hi,

I am using the PDF.JS Express API with merge: [https://api.pdfjs.express/xfdf/merge](https://api.pdfjs.express/xfdf/merge)

It creates the PDF for me with the annotations saved – but they are editable. I tried to set annotations to read only individually but it does not work. So the idea is that I want a “flat” PDF as it was initially before annotations were added so that users cannot edit/move them around after the merge.

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [October 14, 2020, 5:27pm UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/4 "2020-10-14T17:27:34Z")

</div>

Hey there!

How are you setting ‘readonly’ on the annotations? I just tried setting them all to read only and merging them and it worked the way you are expecting. Here is my code:

```javascript
  const { annotManager, docViewer } = instance;

  const list = annotManager.getAnnotationsList();

  list.forEach(item => {
    item.ReadOnly = true
  })

  const xfdf = await annotManager.exportAnnotations();

  // ...rest of merge code

```

Thanks,  
Logan

---

<div class="post-metadata">

**Author:** ![appdevsa](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@appdevsa](https://pdfjs.community/u/appdevsa)\
**Post date:** [October 15, 2020, 7:52am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/5 "2020-10-15T07:52:14Z")

</div>

Hi Logan,

I’ve done it exactly like you explained it but it does not work. If you open the saved PDF the annotations can be moved around, edited and deleted.

Attached is the PDF merged through the API

Here’s the code:

```javascript
const list = annotManager.getAnnotationsList();

list.forEach(item => {

item.ReadOnly = true

})

const xfdf = await annotManager.exportAnnotations({ links: false, widgets: false });

const fileData = await docViewer.getDocument().getFileData({});

const blob = new Blob([fileData], {type: 'application/pdf'});

const data = new FormData();

data.append('xfdf', xfdf);

data.append('file', blob);

data.append('license', xxxx);

// Process the file

const response = await fetch('https://api.pdfjs.express/xfdf/merge', {

method: 'post',

body: data

}).then(resp => resp.json());

const { url, key, id } = response;

// Download the file

const mergedFileBlob = await fetch(url, {

headers: {

Authorization: key

}

}).then(resp => resp.blob())

```

Regards,

Erhard

[7579febd-31ff-31c9-79e4-5804f7ad5b98.pdf](https://pdfjs.community/uploads/short-url/pXiC9C8Zbu9QGx0lIMqsDChfEKV.pdf) (200 KB)

---

<div class="post-metadata">

**Author:** ![appdevsa](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@appdevsa](https://pdfjs.community/u/appdevsa)\
**Post date:** [October 15, 2020, 8:59am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/6 "2020-10-15T08:59:01Z")

</div>

I also tried this, no luck:

item.ReadOnly = true;

item.Locked = true;

item.LockedContents = true;

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [October 15, 2020, 7:54pm UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/7 "2020-10-15T19:54:45Z")

</div>

Hi!

This is pretty much the exact same code I have.

How are you opening the document afterwards? Just with `loadDocument` ?

---

<div class="post-metadata">

**Author:** ![appdevsa](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@appdevsa](https://pdfjs.community/u/appdevsa)\
**Post date:** [October 16, 2020, 3:59am UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/8 "2020-10-16T03:59:38Z")

</div>

After I got the saved PDF from the web services I just open the PDF with a normal PDF viewer (default one on the mac) and it’s in this editor where I can still edit and move things around.

Please see attached PDF as retrieved from the Merge PDF.JS Express Web service call.

If you open this up in the default PDF viewer on a mac you can edit/delete the annotations.

[vnAo9Iidce4Q6RcgvQcHL8QZIL2Kym54cRFYXAw8.pdf](https://pdfjs.community/uploads/short-url/4ALs0UlXJjciZWDH56e6RyWot4t.pdf) (163 KB)

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [October 16, 2020, 3:12pm UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/9 "2020-10-16T15:12:22Z")

</div>

I think this may have something to do with certain viewers not honouring the readonly flag. When I open in some viewers I am able to move the annotation, and in others I am not.

We usually try to mimic Adobe Acrobats functionality when it comes to things like this, and when opening the document in that viewer, you cannot move or edit the annotation.

Thanks!  
Logan

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [April 5, 2021, 2:17pm UTC](https://pdfjs.community/t/merge-rest-api-how-to-flatten-pdf/328/11 "2021-04-05T14:17:20Z")

</div>


