password protect pdf

I use itext 1.3.1 to generate pdf file.
I want to password protect the pdf .


Is it possible ?  which class / method can help on this ?
Thanks
cofactorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jotain 50Development managerCommented:
Hi

You can use setEncryption method of PdfWriter as shown in page http://itextpdf.com/examples/iia.php?id=219

Below example of how to create password protected simple pdf file.

    /** User password. */
    public static byte[] USER = "Hello".getBytes();
    /** Owner password. */
    public static byte[] OWNER = "World".getBytes();

    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
        writer.createXmpMetadata();
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        // step 5
        document.close();
    }

Open in new window

0
cofactorAuthor Commented:
@Jotain,

are you sure your sample code works in itext 1.3.1  ?  I told you I'm using itext 1.3.1

I get compile error in these parts...

PdfWriter.ALLOW_PRINTING,
PdfWriter.STANDARD_ENCRYPTION_128

createXmpMetadata()

It seems these are not available in itext-1.3.1 ......  Could you please check ?

How do I fix it in itext-1.3.1 ?
0
Jotain 50Development managerCommented:
Ok, sorry, i didn't check if these are available in 1.3.1.

Below is modified code that should work in that version.

    /** User password. */
    public static byte[] USER = "Hello".getBytes();
    /** Owner password. */
    public static byte[] OWNER = "World".getBytes();

    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        writer.setEncryption(USER, OWNER, PdfWriter.AllowPrinting, true);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        // step 5
        document.close();
    }

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

cofactorAuthor Commented:
This works fine.  Thanks

I have one query here .

What is user password & owner password ?  

what is the use of two passwords in a pdf file ?
0
Jotain 50Development managerCommented:
With owner password you can always have unrestricted access to PDF. The limitations that you input as third parameter of setEncryption only apply when you open PDF with user password.

You can set either one password to null, in which case random string will be used. In other words if you set owner password to null, nobody can have owner access to PDF file.
0
cofactorAuthor Commented:
I am confused. I'm not talking about coding part here.

I am asking about pdf password functionality.

What I wanted to know is .... what a user password can do & what an owner password can do with pdf ?
0
Jotain 50Development managerCommented:
Yes, that is what i mean.

PDF password can do more than just prevent opening. Third parameter can be used to control that.

In that example user password is Hello and owner password is World. If you open PDF using password Hello, you can view PDF and you can print it (because third parameter is AllowPrinting). You CANNOT fill fillable fields, modify fields that normally allow modifying or save copy of PDF. If you open PDF using password World, then you can do all of those.

If you don't want to allow printing using user password, then you can set third parameter to 0. Third parameter can be AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. Or you can combine any of them by ORing them (for example AllowPrinting|allowAssembly).
0
cofactorAuthor Commented:
Thanks. That was very much helpful.

Which  encryption is used here  ? is it MD5 ?  Could you please tell me the pdf encryption algo name .
0
cofactorAuthor Commented:
0
Jotain 50Development managerCommented:
Hi

Encryption used is called ARC4 encryption. Newer version of iText also supports AES encryption, but if you have to use old version, then you can't use any other encryptions.

Please mark this question as answered.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cofactorAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.