trying to protect PDF (iText) trying to disable users to do copy on pdf's content

adhg picture adhg · Jun 16, 2012 · Viewed 8.9k times · Source

Below is my code. My objective is to create a PDFs where endusers can do whatever they want EXCEPT copying the text (select the text and COPY to a notepad). Can anyone explain what code should come in line 18? I allow printing but not ALLOW_COPY)

I was under the impression that the code below is sufficient to restrict users to do so but 'de facto' they are able to copy the selected text and paste the content to a notepad.

Many thanks!

package com.itext;

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

public class ProtectMePdf 
{ 
public static void main(String[] args) throws IOException, DocumentException 
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/Users/adhg/protectMe.pdf"));

    //LINE 18: what's wrong with this line? - if you run the code you will be able to copy the selected text. 
    writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);


    writer.createXmpMetadata();
    document.open();
    document.add(new Paragraph("Protect me! if you can do copy-paste of this message to a notepad = NO GOOD :-(")); 
    document.close();
}
}

Answer

Nestor Hernandez Loli picture Nestor Hernandez Loli · Apr 20, 2015

The accepted answer is wrong. In fact you can disable copying and allow printing. It's very easy, you just have to negate the permissions:

writer.setEncryption(null, null,  ~(PdfWriter.ALLOW_COPY), PdfWriter.STANDARD_ENCRYPTION_128);