How to Display Password Protected PDFs in an iOS Application
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.
Here, I will take ComPDFKit PDF SDK as an example.
It provides developers with an simple method to open password-protected PDFs
with Objective-C. I 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.
- Create a
new iOS project.
- Integrate
ComPDFKit into your apps.
- Apply
the license key.
- Display
a PDF document.
Decrypt
ComPDFKit PDF SDK
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] autorelease];
NSURL
*surl = [NSURL fileURLWithPath:@""];
[document
writeDecryptToURL:surl];
PDF SDK?
With PDF SDK like ComPDFKit,
you can take the complete PDF technologies to your app more than displaying a
password-protected PDF document. They always support viewing, editing,
annotating, converting, form filling, digital signing, redacting, OCR, etc.
For companies and
developers, you can integrate 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.
Comments
Post a Comment