Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

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
Avatar of Jotain 50
Jotain 50

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

Avatar of cofactor

ASKER

@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 ?
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

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 ?
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.
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 ?
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).
Thanks. That was very much helpful.

Which  encryption is used here  ? is it MD5 ?  Could you please tell me the pdf encryption algo name .
ASKER CERTIFIED SOLUTION
Avatar of Jotain 50
Jotain 50

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.