Removing password from protected document

Product: webviewer

Product Version:8.9.0

Please give a brief summary of your issue: Need to remove a password from a protected file and save added annotations

Please describe your issue and provide steps to reproduce it:

I’m using a function to check whether a pdf file is password protected or not and i’m coding it with javascript and vue 2, that function allows me to get the password that i pass to UI.loadDocument in this way

WebViewer({
                    path: '/js/WebViewerNew/lib',
                    css: '/css/pdftron.css',
                    fullAPI: that.filePassword !== ''
                }, document.getElementById('webviewer'))
                    .then((instance) => {
                        instance.UI.loadDocument(that.initialDoc, {
                            password: that.filePassword
                        });

this works and i’m able to open the document no problem, and i’m able to add and save annotations to it with this code.

const doc = documentViewer.getDocument();
xfdfString = await that.annotationManager.exportAnnotations({useDisplayAuthor: false});
const data = await doc.getFileData({
            // saves the document with existing annotations in it
            xfdfString
 });
 const arr = new Uint8Array(data);
 const blob = new Blob([arr], {type: 'application/pdf'});

this code works fine as well and i save all annotations to the document no problem, however the final document is still protected and i would like to remove the password from the document while keeping the changes, the issue is that the threads i have seen and the tutorials i have been recommended to check all use PDFDoc and PDFNet functions which doesn’t work for me, in some cases i get an empty file and in other cases i get an error saying an error happened because data is corrupt, also i noticed that those functions seem to create a new document and the purpose is to put the content of the original file into the new file without the password, if i understand correctly, which is fine since i have no problem with that as long i send the same info to the backend and can keep the annotations, original content and remove the password.

Now this is the code i’m using after reading the docs about this operation

const doc2 = await PDFNet.PDFDoc.createFromBuffer(arr);
await doc2.initSecurityHandler();
await doc2.removeSecurity();
const docbuf = await doc2.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);

so i need a hand with a code template and an explanation that tells me the following

  1. is is absolutely necessary to create a new document in order to remove password? if yes then at what point should the new document be created? in what way? from what source, url or buffer?

  2. is it possible to remove password first and then pass that document to the first and/or second code i posted?

  3. are there any other ways to remove the password?

  4. what is the recommended order for, remove password, save annotations and convert to blob, to get the result i need.

PD: please don’t direct me to other links talking about the operation without providing a working template as i have checked them and have not been helpful for me,

Hi,

I’m looking into this issue and thanks for your patience.

Wanbo

Hi,

  1. What do you mean by new document? It’s the same PDFDoc object, but yes, the file needs to be saved. Password protection is not just a password, it’s an encryption, and removing the password means completely re-saving the document, decrypting every string and every stream. Virtually everything that isn’t a plain number is re-processed while saving. It shouldn’t matter where you save it, file or memory.
  2. You can call RemoveSecurity, but that doesn’t decrypt anything on the drive (in the PDF file itself). Only when you save it will the decryption happen.
  3. RemoveSecurity removes the security handler. Saving removes the encryption.
  4. The order doesn’t matter, RemoveSecurity merely signals the intention that you would like to decrypt the entire document the next time you save it.

You can use the tested code below to remove the password and save the file:

const doc = documentViewer.getDocument();
const pdfDoc = await doc.getPDFDoc();
await pdfDoc.removeSecurity();
const data = await pdfDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
const arr = new Uint8Array(data);
const blob = new Blob([arr], { type: 'application/pdf' });
saveAs(blob, 'no-password-anymore.pdf');

Thanks.

Wanbo

Hi Wanbo, thank you for this code, it works as expected although i removed one after some experiments and it kept working for me for all good there, about the questions, i asked because i thought that the only way to remove the password from a document was to create a new version of the document instead of modifying the existing one, but is ok, i already have what i need so no need to worry about details too much, thank you again and have a good day

No problem, glad to help!

Wanbo