Remove password tutorial

WebViewer Version: 8.9.0

Do you have an issue with a specific file(s)? no
Can you reproduce using one of our samples or online demos? no
Are you using the WebViewer server? no
Does the issue only happen on certain browsers? no
Is your issue related to a front-end framework? no
Is your issue related to annotations? no

Please give a brief summary of your issue:
(Think of this as an email subject)
Need to remove password protection from a file and then save it with some signatures

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

Here is the situation I’m using a function to check whether a pdf file is password protected or not and i’m coding with javascript, now the problem comes when saving signatures to the document, because i can save all annotations i put into the document using the code provided by the docs which is this

let annotationList = [...that.annotationManager.getAnnotationsList()]
                            const doc = documentViewer.getDocument();

const xfdfString = await that.annotationManager.exportAnnotations({annotList: that.existingAnnotations});
                                    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 for both protected and unprotected files by sending the blob to the backend to do some stuff but the issue for me is that protected file are still protected after saving the signatures into the document, and that’s something i need to remove.

Now, i have a way to remove the password and do what i need to do but i believe there are better ways to do it and since there is an example section to encrypt/decrypt files i though of giving this functionality a shot and see if it is better than my current way, problem is that i haven’t been able to make it work, so what i need is a tutorial that allows me to save the signatures added into the document and unprotect the file so a password free version is delivered to the user.

PD: i don’t know in what order these operations should be done and i have no problem with changing the order i just need to be able to save signature and remove password, thank you for your time and have a good day

Hi renkoha,

Please follow this guide to remove the password from PDF files: removing-security-from-a-document
Once you have removed the security handler you can use getFileData to save it to your server with annotations.

Best Regards,
Ahmad Moaaz
Software Developer
PDFTron Systems, Inc.
www.pdftron.com

Hi Ahmad, thank you for responding, i tried using the code on the page you suggested but i’m still not able to get the result i want, also from what i saw in the docs the createrfromUrl command needs a file path string which i have but then the result of the functions in the tutorial are not something that i can use with getFileData, maybe i didn’t understand the concept so could you pull the full example where password is removed and document is saved with added annotations please?

Hi Ahmad i tried othe possiblities and i’m still unable to fix my issue, one og those possibilities is this

const docProtected = await that.pdfNet.PDFDoc.createFromURL(that.initialDoc);
                                    await docProtected.initSecurityHandler();
                                    await docProtected.removeSecurity();
                                    const docbuf = await docProtected.saveMemoryBuffer(that.pdfNet.SDFDoc.SaveOptions.e_linearized);
                                    console.log(docbuf)

this code offrers me a unit8 array that i can use to save a document to the server but is all black no annotations nor original content is just blank, also i can only use this just before initiating saving process, if i try to use this when i checked the file is password protected it throws an error saying pdfnet initialize must be called and resolved before calling any other function, so i really need a code snipet for the whole process, and just to clarify i know if a pdf is password protected the moment i enter the page before webviewer is declared or loaded so if i need to know if the file is protected before hand that’s not an issue, just provide me a code template for the process and i’ll try it, thank you and have a good day

Hi Renkoha,

You can get the underlying PDFDoc from a WebViewer document object using this method: Document.getPDFDoc

Putting all your code snippets together with the function above would look something like this:

const doc = documentViewer.getDocument();
const docProtected = await doc.getPDFDoc()
await docProtected.initSecurityHandler();
await docProtected.removeSecurity();

let annotationList = that.annotationManager.getAnnotationsList()

const xfdfString = await that.annotationManager.exportAnnotations({annotList: annotationList });
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'});

Best Regards,
Ahmad Moaaz
Software Developer
PDFTron Systems, Inc.
www.pdftron.com

Hi ahmad, thank you very much for this template is exactly what i needed and my issue is resolved, however i wish it had arrived earlier since i implemented other solution in the time i was expecting a response which i’ll have to scrap since this is working way faster and simpler than my other solution, thank you again and have a good day