Tutorials

How to Display Password Protected PDFs in iOS Applications

By ComPDFKit | Fri. 12 May. 2023
iOSObjective-CSecurity

People can limit access to a PDF by encrypting PDFs with passwords. It’s an effective tool for us to protect our content. But what’s the original method to open a protected PDF file in an iOS application? That’s our topic today. 

 

With ComPDFKit PDF SDK, we provide developers with the simplest method to open password-protected PDFs with Objective-C. We will show you how to open a password-protected PDF in an iOS application from the beginning: How to build an iOS application with ComPDFKit PDF SDK.

 

Build an iOS Application to Display Password Protected PDFs

 

Requirements

 

ComPDFKit PDF SDK requires the latest stable version of Xcode available at the time the release was made. This is a hard requirement, as each version of Xcode is bundled with a specific version of the iOS Base SDK, which often defines how UIKit and various other frameworks behave. 

 

         - iOS 10.0 or higher.

         - Xcode 12.0 or newer for Objective-C or Swift.

 

Steps

 

Here are the whole steps to create an iOS app. Please refer to our developer guides for the details. In our guides, we take Xcode 12.4 as an example.

 

         - Create a new iOS project in Objective-C.

         - Integrate ComPDFKit into your apps.

         - Apply the license key.

         - Display a PDF document.

 

 

Decrypt

 

ComPDFKit PDF SDK fully supports the reading of protected and encrypted PDF documents. 

 

A PDF file can have two different passwords set, an owner password and a user password. An owner password (also known as a permissions password) requires a password to change permission settings, and we are not going to talk about it here. 

 

A user password (also known as an open password) requires a user to type a password to open the PDF. If you want to open a document with a user password programmatically, you can use the `CPDFDocument.UnlockWithPassword(string password)` API.

 

To check whether a document requires a password:

 

  NSURL *url = [NSURL fileURLWithPath:@""];
  CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];

  if (document.error && 
      document.error.code == CPDFDocumentPasswordError) {
      // Password required
  }

 

To read a PDF document with password protection, use function CPDFDocument::unlockWithPassword:. If the password is correct, this method returns YES, a CPDFDocumentDidUnlockNotification notification is sent. Once unlocked, you don’t need to type the password again if you don’t close this document. 

 

 

Unlock PDFs VS Remove the Password

 

It’s a little bit confusing about the word "unlock". The method we mentioned above is  about unlocking PDFs. We can also call it decrypting. It is used to open an encrypted PDF for further operations. But if we don’t need to secure the PDF, we can remove the password.

 

To remove PDF security, call the CPDFDocument::writeDecryptToURL: method:

  NSURL *url = [NSURL fileURLWithPath:@""];
  CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelea];

  NSURL *surl = [NSURL fileURLWithPath:@""];
  [document writeDecryptToURL:surl];

 

 

What Can ComPDFKit Do for You?

 

With ComPDFKit PDF SDK, you can take the complete PDF technologies to your app more than displaying a password-protected PDF document. We support viewing, editing, annotating, converting, form filling, digital signing, redacting, OCR, etc. Please turn to our website for more information.

 

For companies and developers, you can integrate our PDF SDK into your applications or devices with just a few lines of code. It’s a waste of time and costs for you to build this functionality yourself, and it may not be on the roadmap for your team. The faster you get to the market, the more competitiveness your companies will have. If you want to know more about our ComPDFKit, you can connect to our website or contact our support team.

 

 

Conclusion


You can learn how to decrypt a password-protected PDF file. ComPDFKit could provide the advanced and complete PDF technologies. We will keep sharing the knowledge of PDF SDK, and we have a complete description about our ComPDFKit, for more information, please click to our website.