Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pdf file password protected?

Tags:

java

pdf

I want to make pdf file password protected. I just goolge it for the same and find a good solution given below. It's working fine But it wipe out all the data which is already there in my pdf after i secure pdf using below given code.

Used jar files for this code are:

itextpdf-5.2.1.jar

bcmail-jdk16-1.46.jar

bcprov-jdk16-1.46.jar

bctsp-jdk16-1.46.jar

Code to secure PDF :

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Secure_file {
    private static String USER_PASSWORD = "password";
    private static String OWNER_PASSWORD = "secured";
    public static void main(String[] args) throws IOException {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\sample.pdf"));
            writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
            document.open();
            document.add(new Paragraph("This is Password Protected PDF document."));
            document.close();
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

what changes i need to made in this program ?

like image 575
user3747920 Avatar asked Sep 05 '14 08:09

user3747920


2 Answers

If you look up the iText in Action keywords you'll find encryption pointing to the sample part3.chapter12.EncryptionPdf. That sample's method createPdf essentially is equivalent to your code but the method encryptPdf is what you want:

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

...

public void encryptPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER,
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}
like image 92
mkl Avatar answered Nov 12 '22 13:11

mkl


example using iText5. If use iText7 is very similar but using another class instead of stampler.

                PdfReader reader = new PdfReader(dp.getStream());
                File tempFile = File.createTempFile("someFilename", FILE_EXTENSION_PDF);
                tempFile.deleteOnExit();
                FileOutputStream os = new FileOutputStream(tempFile);
                PdfStamper stamper = new PdfStamper(reader, os);

                String pdfPassword = "1234"
                String pdfAdminPassword = "5678"
                stamper.setEncryption(
                        pdfPassword.getBytes(),
                        pdfAdminPassword.getBytes(),
                        PdfWriter.ALLOW_PRINTING,
                        PdfWriter.ENCRYPTION_AES_128);

                reader.close();
                stamper.close();

                InputStream encryptedFileIs = new FileInputStream(tempFile);

or apache lib pdfbox

                PDDocument document = PDDocument.load(dp.getStream());
                AccessPermission ap = new AccessPermission();
                StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);
                spp.setEncryptionKeyLength(128);
                spp.setPermissions(ap);
                document.protect(spp);

                File tempFile = File.createTempFile("someFilename", FILE_EXTENSION_PDF);
                tempFile.deleteOnExit();
                FileOutputStream os = new FileOutputStream(tempFile);
                document.save(os);
                document.close();
                InputStream encryptedFileIs = new FileInputStream(tempFile);

Good luck and happy coding :)

like image 33
panayot_kulchev_bg Avatar answered Nov 12 '22 13:11

panayot_kulchev_bg