Search

Atalasoft Knowledge Base

Save encrypted pdf file using the Document Annotation Viewer

Anthony
DotImage

The DocumentAnnotationViewer api does not have what you need in order to to save encrypted pdfs or pdfs with passwords, this control assumes that pdf is not encrypted when exporting annotation data. As a result you will receive a "Password Incorrect" exception when attempting to save to an encrypted pdf file. However, with with a little help from the PdfDocument class you can password protect your pdf files. Use the code example below to encrypt your pdf files from the DocumentAnnotationViewer.

Example:

            
            using (MemoryStream ms = new MemoryStream())
            {
                PdfEncoder pdfEncoder = new PdfEncoder() { SizeMode = PdfPageSizeMode.FitToPage };
                documentAnnotationViewer1.Save(ms, pdfEncoder);

                PdfDocument doc = new PdfDocument(ms);
                PdfSaveOptions saveOptions = new PdfSaveOptions();

                // Note: createSecureString is just a helper method used to create the SecureString object containing the password.
                // it is not a part of the DotImage SDK
                
                SecureString securepassword= createSecureString("password");
                saveOptions.SetOneTimePasswords(securepassword);
                doc.Save("out.pdf", saveOptions);
            }

Details
Last Modified: 3 Years Ago
Last Modified By: Anthony
Type: HOWTO
Article not rated yet.
Article has been viewed 318 times.
Options
Also In This Category