pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: PDDocument.load, password and InputStream
Date Mon, 26 Dec 2016 12:52:30 GMT
Am 26.12.2016 um 12:23 schrieb Maxime GUERREIRO:
> Hello everyone,
>
> I am using PDFBox 2.0.4 and I couldn't find the best way to do it.
> I hope you can help me :-)
>
> I have a simple application that accepts PDF files (among other formats),
> and I currently pass it along (as argument to methods) as a File. In order
> to cleanup my code, I wanted to pass it as an InputStream instead... but I

... which is not a good idea as it will make everything slower. Because 
PDFBox makes an internal copy to allow random access.


> miss one feature in PDFBox 2.0 detecting if the file is password protected.
> I think it was possible using the 1.8 version, as we instantiated the doc and
> *then* we'd give it the password.
>
> Sample code:
> ```
> try (PDDocument ignore = PDDocument.load(inFile)) {
>      return null; // not password protected
> } catch (InvalidPasswordException ignore) {
> }
> ```

That code has a memory leak as "ignore" isn't closed

>
> The issue is that when I try to reuse inFile, it tells me it has been closed.

I doubt that. Unless "inFile" is an input stream and not a file, as the 
name would suggest.

> When wrapping it in a CloseShieldInputStream, the whole file is read... leading
> to an unwanted memory consumption and/or a temporary file.
>
> This was not an issue with File-s because I could re-recreate an InputStream
> when I needed to.
>
>
> ~> Is there a way to check if a file is password-protected *and* if the given
> password is the right one, without PDFBox reading it as a whole?

|document.isEncrypted() == true|


then it is encrypted and you gave the correct password (which can be the 
empty password!!!).

If it is encrypted and you gave the wrong password then you'll get an 
InvalidPasswordException

Tilman

>
> Thanks,
> Maxime
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message