Sunday, October 26, 2014

Make a PDF unfillable using Java


Create  a new Java project in your favorite IDE. In my case , I would be using Eclipse.

Name your Java project - pdftest.
Right click on your project in the workspace and select new-->Folder.
Name it src.
Right click on src folder and select new--> Package. Name it com.ankit.pdf  .
Right click on the new package created and select new--> Class . Name it main.java .

We would be needing these three jars itextpdf-5.5.2.jar, bcpkix-jdk15on-1.47.jar, bcprov-jdk15on-1.47.jar .

I have used the code base from the article given here on iTextpdf's website  : http://itextpdf.com/examples/iia.php?id=219

This is just for learning purposes, I have no intention to use the codebase for any commercial purposes.

Code :

package com.ankit.pdf;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;


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

// Please changes the values of RESULT1,RESULT2, RESULT3,RESULT4 while executing code in your machine.

    /** The resulting PDF file. */
    public static final String RESULT1
        = "F:/chapter12/encryption.pdf";
    /** The resulting PDF file. */
    public static final String RESULT2
        = "F:/chapter12/encryption_decrypted.pdf";
    /** The resulting PDF file. */
    public static final String RESULT3
        = "F:/chapter12/encryption_encrypted.pdf";
    public static final String RESULT4
    = "F:/sampleinp.pdf";

    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException
     * @throws IOException
     */
    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();
    }

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void decryptPdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src, OWNER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        reader.close();
    }

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    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_FILL_IN|PdfWriter.ALLOW_MODIFY_CONTENTS, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);

// Setting setFormFlattening as true helps in making PDF form unfillable

        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
     
    }

    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        main2 metadata = new main2();
        metadata.createPdf(RESULT1);
        metadata.decryptPdf(RESULT1, RESULT2);
        metadata.encryptPdf(RESULT4, RESULT3);
    }
}


Please download the sample pdf form which is in fillable state originally which is used in variable RESULT4 from here : Link
To run, right click on main.java file and run as java application.


Please note : iText PDF software is not free and you need to purchase a license to use it.

I hope this small code snippet was useful to you. Let me know in the comments if it worked for you.



No comments:

Post a Comment

 
Tweet