Some test text!

Read a document with encryption

Contents

About reading a secured document

To read a document with password protection without user feedback.

PDFDoc doc = new PDFDoc(output_path + "secured.pdf");

// Initializing security handler without any user interaction
// At this point MySecurityHandler callbacks will be invoked.
// MySecurityHandler.GetAuthorizationData() should collect the password and
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
if (!doc.InitStdSecurityHandler("test"))
{
  Console.WriteLine("Document authentication error...");
  Console.WriteLine("The password is not valid.");
  return;
}
else
{
  Console.WriteLine("The password is correct! Document can now be used for reading and editing");

  // Remove the password security and save the changes to a new file.
  doc.RemoveSecurity();
  doc.Save(output_path + "secured_nomore1.pdf", 0);
  Console.WriteLine("Done. Result saved in secured_nomore1.pdf");
}

Encrypt and Decrypt PDF Files
Full sample code which illustrates some of our encryption support.

linkAbout reading a secured document

PDFTron SDK fully supports the reading of secured and encrypted PDF documents. To test whether a document requires a password, check the return value of PDFDoc.InitSecurityHandler():

// Open a potentially encrypted document
PDFDoc doc = new PDFDoc("in.pdf");
if (!doc.InitSecurityHandler())
{
  Console.WriteLine(
    "in.pdf requires a password.");
}
else
{
  Console.WriteLine(
    "in.pdf does not require a password.");
}

Because InitSecurityHandler() doesn't have any side effects on documents that are not encrypted you should always invoke this method, or InitStdSecurityHandler(), after constructing a document.

If a document doesn't require authentication data (such as a user password) in order to view its content, InitSecurityHandler() is enough to work with encrypted documents. If, on the other hand, the document requires a password, InitStdSecurityHandler allows you to provide one:

// Open a potentially encrypted document
PDFDoc doc = new PDFDoc("in.pdf");
if (!doc.InitStdSecurityHandler("test"))
{
  Console.WriteLine(
    "in.pdf's password is 'test'.");
}
else
{
  Console.WriteLine(
    "in.pdf's password is not 'test'.");
}

After the document's security handler is initialized, you can access it using the doc.GetSecurityHandler() method. You can edit permissions and authorization data on an existing handler, or set a completely new security handler using the doc.SetSecurityHandler(handler) method.

To remove PDF security, set the document's current SecurityHandler to null:

PDFDoc doc = new PDFDoc("encrypted.pdf");
doc.InitSecurityHandler();
doc.SetSecurityHandler(null);

Get the answers you need: Support

Contents

About reading a secured document