Merge Rest API - how to flatten PDF?

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.

Hi appdevsa,

Can you tell me how you are outputting the pdf?

Hi,

I am using the PDF.JS Express API with 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.

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:

  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

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:

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 (200 KB)

I also tried this, no luck:

item.ReadOnly = true;

item.Locked = true;

item.LockedContents = true;

Hi!

This is pretty much the exact same code I have.

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

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 (163 KB)

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