1

I want to display a PDF that does not allow users to copy, print or save. I have look around and it seems that iText 7 can do that but there is no code that I can take a look

  • Does this answer your question? [iText disable printing/Copying/Saving](https://stackoverflow.com/questions/1936189/itext-disable-printing-copying-saving) – derloopkat Feb 02 '21 at 02:01
  • @Daniel the question you point to has a solution for itext 5 but the op mentions itext 7. – mkl Feb 02 '21 at 06:23
  • @Daniel I saw this but in iText 7 there is no PDFStamper – user2493442 Feb 04 '21 at 01:21

1 Answers1

0

PdfDocument works in both read, create and stamp modes in iText 7. So basically you need to open your document in stamping mode (with reader and writer), and configure the writer to create an encrypted document for you. If you want the document to be viewable without a password prompt then you need to set the user password to empty one. The owner password will be needed in case you want to unlock access to the document in the future.

Here is sample code:

PdfDocument pdfDocument = new PdfDocument(
        new PdfReader("in.pdf"),
        new PdfWriter("encrypted.pdf",
                new WriterProperties().setStandardEncryption(new byte[0], password,
                        0, EncryptionConstants.ENCRYPTION_AES_256)));
pdfDocument.close();
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60