Password Protect PDF in COM/ActiveX
Before opening a PDF document, if a document is password protected then a PDF viewer will require a password before opening the PDF document. Password protection can be added to new PDF documents or to existing PDF document when using DynamicPDF Core Suite. The steps and sample codes below illustrates adding password protection to both situations.
How to Password Protect a PDF
Depending upon your desired security settings, DynamicPDF Core Suite supports the following different security algorithms.
Here we illustrate using Aes256Security in the following example.
Steps to Password Protect PDF
- Create a
Document
object. - Create a
Page
object and add it toDocument
instance. - Create a
Aes256Security
object and pass the owner and user password to the instance's constructor. - Set the Document instance's security.
- Output the Document instance to the browser.
Sample code - ASP
Dim MyDocument
Set MyDocument = Server.CreateObject("DynamicPDF.Document")
Dim MyPage
Set MyPage = MyDocument.AddPage()
Dim MySecurity
Set MySecurity = MyDocument.SetAes256Security("owner", "user")
MyDocument.DrawToWeb
How to Add Password to Existing PDF
The above security classes were used to add password protection to a new PDF document. You can also add password protection to an existing PDF document, as the following example illustrates.
Steps to Add Password Protection to a PDF
- Create a
Document
object and load the PDF file. - Create a
Aes256Security
object instance and in the constructor, set the owner and user password. - Set the Document instance's security.
- Output the Document instance to the Browser.
Sample Code - ASP
Dim MyDocument
Set MyDocument = Server.CreateObject("DynamicPDF.Document")
MyDocument.LoadPdf Server.MapPath("pdf-a.pdf")
Dim MySecurity
Set MySecurity = MyDocument.SetAes256Security("owner", "user")
MyDocument.DrawToWeb
Getting Started
DynamicPDF Generator Information
More information on DynamicPDF Generator can be found from its webpage.
Available on Other Platforms
DynamicPDF Generator is also available for the .NET and Java platforms. Refer to the respective product pages for more details.
- .NET - DynamicPDF Generator for .NET
- Java - DynamicPDF Generator for Java