Question

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();
}
}
Was it helpful?

Solution

I'm not an expert of iText and the PDF specification, but I think you cannot allow printing and disable copy&paste at the same time. You can find additional info here.

An alternative option would be to put images in the PDF, but OCR is kinda advanced to evade that.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top