Shwan
Shwan

Reputation: 49

How to check the pdf file is editable or not(protected) in java

Given a pdf file, is it possible to determine if that file is editable or not in Java? PDDocument.getCurrentAccessPermission().isReadOnly() does not work.

Upvotes: 0

Views: 3166

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18851

In the 1.8 versions, when using the "old" parser, you need to call openProtection() first, because such files are "encrypted" with the empty password:

if (doc.isEncrypted()
{
    doc.openProtection(new StandardDecryptionMaterial(""));
}

and then you can call

doc.getCurrentAccessPermission().isReadOnly()

or any other method from the AccessPermission class.

Alternatively, load your PDF with the "new" non-sequential parser:

PDDocument doc = PDDocument.loadNonSeq(new File(...), null);

and then doc.getCurrentAccessPermission().isReadOnly() can also be called.

Upvotes: 1

Related Questions